Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make getMedia private? #78

Open
Ryuno-Ki opened this issue Mar 1, 2023 · 0 comments
Open

Make getMedia private? #78

Ryuno-Ki opened this issue Mar 1, 2023 · 0 comments

Comments

@Ryuno-Ki
Copy link

Ryuno-Ki commented Mar 1, 2023

Looking at

tweetback/src/twitter.js

Lines 168 to 253 in d03e289

async getMedia(tweet) {
let {transform: twitterLink} = await import("@tweetback/canonical");
let medias = [];
let textReplacements = new Map();
// linkify urls
if( tweet.entities ) {
for(let url of tweet.entities.urls) {
// Remove photo URLs
if(url.expanded_url && url.expanded_url.indexOf(`/${tweet.id}/photo/`) > -1) {
textReplacements.set(url.url, { html: "" });
} else {
let {targetUrl, className, displayUrl} = this.getUrlObject(url);
targetUrl = twitterLink(targetUrl);
textReplacements.set(url.url, { html: `<a href="${targetUrl}" class="${className}" data-pagefind-index-attrs="href">${displayUrl}</a>` });
// Add opengraph preview
if(targetUrl.startsWith("https://") && !targetUrl.startsWith("https://twitter.com/")) {
medias.push(`<template data-island><a href="${targetUrl}"><img src="https://v1.opengraph.11ty.dev/${encodeURIComponent(targetUrl)}/small/onerror/" alt="OpenGraph image for ${displayUrl}" loading="lazy" decoding="async" width="375" height="197" class="tweet-media tweet-media-og" onerror="this.parentNode.remove()"></a></template>`);
}
}
}
for(let mention of tweet.entities.user_mentions) {
textReplacements.set(mention.screen_name, {
regex: new RegExp(`@${mention.screen_name}`, "i"),
html: `<a href="${twitterLink(`https://twitter.com/${mention.screen_name}/`)}" class="tweet-username h-card">@<span class="p-nickname">${mention.screen_name}</span></a>`,
});
}
}
if( tweet.extended_entities ) {
for(let media of tweet.extended_entities.media ) {
if(media.type === "photo") {
// remove photo URL
textReplacements.set(media.url, { html: "" });
try {
let html = await this.getImage(media.media_url_https, media.alt_text || "");
medias.push(html);
} catch(e) {
console.log("Image request error", e.message);
medias.push(`<a href="${media.media_url_https}">${media.media_url_https}</a>`);
}
} else if(media.type === "animated_gif" || media.type === "video") {
if(media.video_info && media.video_info.variants) {
textReplacements.set(media.url, { html: "" });
let videoResults = media.video_info.variants.filter(video => {
return video.content_type === "video/mp4" && video.url;
}).sort((a, b) => {
return parseInt(b.bitrate) - parseInt(a.bitrate);
});
if(videoResults.length === 0) {
continue;
}
let remoteVideoUrl = videoResults[0].url;
try {
let videoUrl = remoteVideoUrl;
let posterStats = await eleventyImg(media.media_url_https, ELEVENTY_IMG_OPTIONS);
if(!this.isRetweet(tweet)) {
videoUrl = `/video/${tweet.id}.mp4`;
await this.saveVideo(remoteVideoUrl, `.${videoUrl}`)
}
let imgRef = posterStats.jpeg[0];
medias.push(`<video muted controls ${media.type === "animated_gif" ? "loop" : ""} src="${videoUrl}" poster="${imgRef.url}" class="tweet-media u-video"></video>`);
} catch(e) {
console.log("Video request error", e.message);
medias.push(`<a href="${remoteVideoUrl}">${remoteVideoUrl}</a>`);
}
}
}
}
}
return {
medias,
textReplacements,
}
}

it appears to be used only within this class. Therefore I suggest to declare it as a private method (by prefixing it with an underscore or using private class feature).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant