From d026eb75a5ba3cfe5434e27d9072a9d937924187 Mon Sep 17 00:00:00 2001 From: wukko Date: Sat, 31 Aug 2024 14:10:03 +0600 Subject: [PATCH] api/instagram: add support for proxying everything in a picker --- api/src/processing/match.js | 1 + api/src/processing/services/instagram.js | 49 +++++++++++++++++------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/api/src/processing/match.js b/api/src/processing/match.js index 4d3f7b99..1266dcb5 100644 --- a/api/src/processing/match.js +++ b/api/src/processing/match.js @@ -165,6 +165,7 @@ export default async function(host, patternMatch, obj) { r = await instagram({ ...patternMatch, quality: obj.videoQuality, + alwaysProxy: obj.alwaysProxy, dispatcher }) break; diff --git a/api/src/processing/services/instagram.js b/api/src/processing/services/instagram.js index ec225372..17e78ec4 100644 --- a/api/src/processing/services/instagram.js +++ b/api/src/processing/services/instagram.js @@ -163,7 +163,7 @@ export default function(obj) { ?.[0]; } - function extractOldPost(data, id) { + function extractOldPost(data, id, alwaysProxy) { const sidecar = data?.gql_data?.shortcode_media?.edge_sidecar_to_children; if (sidecar) { const picker = sidecar.edges.filter(e => e.node?.display_url) @@ -171,8 +171,19 @@ export default function(obj) { const type = e.node?.is_video ? "video" : "photo"; const url = type === "video" ? e.node?.video_url : e.node?.display_url; + let itemExt = type === "video" ? "mp4" : "jpg"; + + let proxyFile; + if (alwaysProxy) proxyFile = createStream({ + service: "instagram", + type: "proxy", + u: url, + filename: `instagram_${id}_${i + 1}.${itemExt}` + }); + return { - type, url, + type, + url: proxyFile || url, /* thumbnails have `Cross-Origin-Resource-Policy` ** set to `same-origin`, so we need to proxy them */ thumb: createStream({ @@ -199,7 +210,7 @@ export default function(obj) { } } - function extractNewPost(data, id) { + function extractNewPost(data, id, alwaysProxy) { const carousel = data.carousel_media; if (carousel) { const picker = carousel.filter(e => e?.image_versions2) @@ -208,15 +219,26 @@ export default function(obj) { const imageUrl = e.image_versions2.candidates[0].url; let url = imageUrl; - if (type === 'video') { + let itemExt = type === "video" ? "mp4" : "jpg"; + + if (type === "video") { const video = e.video_versions.reduce((a, b) => a.width * a.height < b.width * b.height ? b : a); url = video.url; } + let proxyFile; + if (alwaysProxy) proxyFile = createStream({ + service: "instagram", + type: "proxy", + u: url, + filename: `instagram_${id}_${i + 1}.${itemExt}` + }); + return { - type, url, + type, + url: proxyFile || url, /* thumbnails have `Cross-Origin-Resource-Policy` - ** set to `same-origin`, so we need to proxy them */ + ** set to `same-origin`, so we need to always proxy them */ thumb: createStream({ service: "instagram", type: "proxy", @@ -237,12 +259,13 @@ export default function(obj) { } else if (data.image_versions2?.candidates) { return { urls: data.image_versions2.candidates[0].url, - isPhoto: true + isPhoto: true, + filename: `instagram_${id}.jpg`, } } } - async function getPost(id) { + async function getPost(id, alwaysProxy) { let data, result; try { const cookie = getCookie('instagram'); @@ -274,9 +297,9 @@ export default function(obj) { if (!data) return { error: "fetch.fail" }; if (data?.gql_data) { - result = extractOldPost(data, id) + result = extractOldPost(data, id, alwaysProxy) } else { - result = extractNewPost(data, id) + result = extractNewPost(data, id, alwaysProxy) } if (result) return result; @@ -341,9 +364,9 @@ export default function(obj) { return { error: "link.unsupported" }; } - const { postId, storyId, username } = obj; - if (postId) return getPost(postId); + const { postId, storyId, username, alwaysProxy } = obj; + if (postId) return getPost(postId, alwaysProxy); if (username && storyId) return getStory(username, storyId); - return { error: "fetch.fail" } + return { error: "fetch.empty" } }