mirror of
https://github.com/imputnet/cobalt.git
synced 2025-01-01 04:36:02 +00:00
api/instagram: add support for proxying everything in a picker
This commit is contained in:
parent
4476ae0672
commit
d026eb75a5
|
@ -165,6 +165,7 @@ export default async function(host, patternMatch, obj) {
|
||||||
r = await instagram({
|
r = await instagram({
|
||||||
...patternMatch,
|
...patternMatch,
|
||||||
quality: obj.videoQuality,
|
quality: obj.videoQuality,
|
||||||
|
alwaysProxy: obj.alwaysProxy,
|
||||||
dispatcher
|
dispatcher
|
||||||
})
|
})
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -163,7 +163,7 @@ export default function(obj) {
|
||||||
?.[0];
|
?.[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractOldPost(data, id) {
|
function extractOldPost(data, id, alwaysProxy) {
|
||||||
const sidecar = data?.gql_data?.shortcode_media?.edge_sidecar_to_children;
|
const sidecar = data?.gql_data?.shortcode_media?.edge_sidecar_to_children;
|
||||||
if (sidecar) {
|
if (sidecar) {
|
||||||
const picker = sidecar.edges.filter(e => e.node?.display_url)
|
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 type = e.node?.is_video ? "video" : "photo";
|
||||||
const url = type === "video" ? e.node?.video_url : e.node?.display_url;
|
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 {
|
return {
|
||||||
type, url,
|
type,
|
||||||
|
url: proxyFile || url,
|
||||||
/* thumbnails have `Cross-Origin-Resource-Policy`
|
/* thumbnails have `Cross-Origin-Resource-Policy`
|
||||||
** set to `same-origin`, so we need to proxy them */
|
** set to `same-origin`, so we need to proxy them */
|
||||||
thumb: createStream({
|
thumb: createStream({
|
||||||
|
@ -199,7 +210,7 @@ export default function(obj) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractNewPost(data, id) {
|
function extractNewPost(data, id, alwaysProxy) {
|
||||||
const carousel = data.carousel_media;
|
const carousel = data.carousel_media;
|
||||||
if (carousel) {
|
if (carousel) {
|
||||||
const picker = carousel.filter(e => e?.image_versions2)
|
const picker = carousel.filter(e => e?.image_versions2)
|
||||||
|
@ -208,15 +219,26 @@ export default function(obj) {
|
||||||
const imageUrl = e.image_versions2.candidates[0].url;
|
const imageUrl = e.image_versions2.candidates[0].url;
|
||||||
|
|
||||||
let url = imageUrl;
|
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);
|
const video = e.video_versions.reduce((a, b) => a.width * a.height < b.width * b.height ? b : a);
|
||||||
url = video.url;
|
url = video.url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let proxyFile;
|
||||||
|
if (alwaysProxy) proxyFile = createStream({
|
||||||
|
service: "instagram",
|
||||||
|
type: "proxy",
|
||||||
|
u: url,
|
||||||
|
filename: `instagram_${id}_${i + 1}.${itemExt}`
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type, url,
|
type,
|
||||||
|
url: proxyFile || url,
|
||||||
/* thumbnails have `Cross-Origin-Resource-Policy`
|
/* 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({
|
thumb: createStream({
|
||||||
service: "instagram",
|
service: "instagram",
|
||||||
type: "proxy",
|
type: "proxy",
|
||||||
|
@ -237,12 +259,13 @@ export default function(obj) {
|
||||||
} else if (data.image_versions2?.candidates) {
|
} else if (data.image_versions2?.candidates) {
|
||||||
return {
|
return {
|
||||||
urls: data.image_versions2.candidates[0].url,
|
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;
|
let data, result;
|
||||||
try {
|
try {
|
||||||
const cookie = getCookie('instagram');
|
const cookie = getCookie('instagram');
|
||||||
|
@ -274,9 +297,9 @@ export default function(obj) {
|
||||||
if (!data) return { error: "fetch.fail" };
|
if (!data) return { error: "fetch.fail" };
|
||||||
|
|
||||||
if (data?.gql_data) {
|
if (data?.gql_data) {
|
||||||
result = extractOldPost(data, id)
|
result = extractOldPost(data, id, alwaysProxy)
|
||||||
} else {
|
} else {
|
||||||
result = extractNewPost(data, id)
|
result = extractNewPost(data, id, alwaysProxy)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result) return result;
|
if (result) return result;
|
||||||
|
@ -341,9 +364,9 @@ export default function(obj) {
|
||||||
return { error: "link.unsupported" };
|
return { error: "link.unsupported" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const { postId, storyId, username } = obj;
|
const { postId, storyId, username, alwaysProxy } = obj;
|
||||||
if (postId) return getPost(postId);
|
if (postId) return getPost(postId, alwaysProxy);
|
||||||
if (username && storyId) return getStory(username, storyId);
|
if (username && storyId) return getStory(username, storyId);
|
||||||
|
|
||||||
return { error: "fetch.fail" }
|
return { error: "fetch.empty" }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue