api/twitter: support proxying videos & images in a picker

This commit is contained in:
wukko 2024-08-31 14:23:18 +06:00
parent 00da2a9339
commit 10717c69f6
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
2 changed files with 18 additions and 9 deletions

View file

@ -69,6 +69,7 @@ export default async function(host, patternMatch, obj) {
id: patternMatch.id, id: patternMatch.id,
index: patternMatch.index - 1, index: patternMatch.index - 1,
toGif: !!obj.twitterGif, toGif: !!obj.twitterGif,
alwaysProxy: obj.alwaysProxy,
dispatcher dispatcher
}); });
break; break;

View file

@ -101,7 +101,7 @@ const requestTweet = async(dispatcher, tweetId, token, cookie) => {
return result return result
} }
export default async function({ id, index, toGif, dispatcher }) { export default async function({ id, index, toGif, dispatcher, alwaysProxy }) {
const cookie = await getCookie('twitter'); const cookie = await getCookie('twitter');
let guestToken = await getGuestToken(dispatcher); let guestToken = await getGuestToken(dispatcher);
@ -159,6 +159,12 @@ export default async function({ id, index, toGif, dispatcher }) {
const getFileExt = (url) => new URL(url).pathname.split(".", 2)[1]; const getFileExt = (url) => new URL(url).pathname.split(".", 2)[1];
const proxyMedia = (u, filename) => createStream({
service: "twitter",
type: "proxy",
u, filename,
})
switch (media?.length) { switch (media?.length) {
case undefined: case undefined:
case 0: case 0:
@ -184,25 +190,25 @@ export default async function({ id, index, toGif, dispatcher }) {
} }
default: default:
const proxyThumb = (url, i) => const proxyThumb = (url, i) =>
createStream({ proxyMedia(url, `twitter_${id}_${i + 1}.${getFileExt(url)}`);
service: "twitter",
type: "proxy",
u: url,
filename: `twitter_${id}_${i + 1}.${getFileExt(url)}`
})
const picker = media.map((content, i) => { const picker = media.map((content, i) => {
if (content.type === "photo") { if (content.type === "photo") {
let url = `${content.media_url_https}?name=4096x4096`; let url = `${content.media_url_https}?name=4096x4096`;
let proxiedImage = proxyThumb(url, i);
if (alwaysProxy) url = proxiedImage;
return { return {
type: "photo", type: "photo",
url, url,
thumb: proxyThumb(url, i), thumb: proxiedImage,
} }
} }
let url = bestQuality(content.video_info.variants); let url = bestQuality(content.video_info.variants);
const shouldRenderGif = content.type === "animated_gif" && toGif; const shouldRenderGif = content.type === "animated_gif" && toGif;
const videoFilename = `twitter_${id}_${i + 1}.mp4`;
let type = "video"; let type = "video";
if (shouldRenderGif) type = "gif"; if (shouldRenderGif) type = "gif";
@ -212,8 +218,10 @@ export default async function({ id, index, toGif, dispatcher }) {
service: "twitter", service: "twitter",
type: shouldRenderGif ? "gif" : "remux", type: shouldRenderGif ? "gif" : "remux",
u: url, u: url,
filename: `twitter_${id}_${i + 1}.mp4` filename: videoFilename,
}) })
} else if (alwaysProxy) {
url = proxyMedia(url, videoFilename);
} }
return { return {