mirror of
https://github.com/imputnet/cobalt.git
synced 2025-01-21 06:01:40 +00:00
clean up posts/reels code
This commit is contained in:
parent
8dcb6d8ea7
commit
b54efb968f
|
@ -1,6 +1,20 @@
|
|||
import { createStream } from "../../stream/manage.js";
|
||||
import { genericUserAgent } from "../../config.js";
|
||||
import { getCookie, updateCookie } from '../cookie/manager.js';
|
||||
import { getCookie, updateCookie } from "../cookie/manager.js";
|
||||
|
||||
const commonInstagramHeaders = {
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
|
||||
'User-Agent': genericUserAgent,
|
||||
'X-Ig-App-Id': '936619743392459',
|
||||
'X-Asbd-Id': '129477',
|
||||
'x-requested-with': 'XMLHttpRequest',
|
||||
'Sec-Fetch-Dest': 'empty',
|
||||
'Sec-Fetch-Mode': 'cors',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
'upgrade-insecure-requests': '1',
|
||||
'accept-encoding': 'gzip, deflate, br',
|
||||
'accept-language': 'en-US,en;q=0.9,en;q=0.8',
|
||||
}
|
||||
|
||||
export default async function(obj) {
|
||||
let data;
|
||||
|
@ -19,84 +33,56 @@ export default async function(obj) {
|
|||
|
||||
data = await fetch(url, {
|
||||
headers: {
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
|
||||
'User-Agent': genericUserAgent,
|
||||
'X-Ig-App-Id': '936619743392459',
|
||||
'X-Asbd-Id': '129477',
|
||||
...commonInstagramHeaders,
|
||||
'x-ig-www-claim': cookie?._wwwClaim || '0',
|
||||
'x-csrftoken': cookie?.values()?.csrftoken,
|
||||
'x-requested-with': 'XMLHttpRequest',
|
||||
'Sec-Fetch-Dest': 'empty',
|
||||
'Sec-Fetch-Mode': 'cors',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
'upgrade-insecure-requests': '1',
|
||||
'accept-encoding': 'gzip, deflate, br',
|
||||
'accept-language': 'en-US,en;q=0.9,en;q=0.8',
|
||||
cookie
|
||||
}
|
||||
})
|
||||
|
||||
if (data.headers.get('X-Ig-Set-Www-Claim') && cookie) {
|
||||
if (data.headers.get('X-Ig-Set-Www-Claim') && cookie)
|
||||
cookie._wwwClaim = data.headers.get('X-Ig-Set-Www-Claim');
|
||||
}
|
||||
|
||||
updateCookie(cookie, data.headers);
|
||||
data = (await data.json()).data;
|
||||
} catch (e) {
|
||||
data = false;
|
||||
}
|
||||
} catch {}
|
||||
|
||||
if (!data) return { error: 'ErrorCouldntFetch' };
|
||||
|
||||
let single, multiple = [];
|
||||
const sidecar = data?.shortcode_media?.edge_sidecar_to_children;
|
||||
if (sidecar) {
|
||||
sidecar.edges.forEach(e => {
|
||||
if (e.node?.is_video) {
|
||||
multiple.push({
|
||||
type: "video",
|
||||
// thumbnails have `Cross-Origin-Resource-Policy` set to `same-origin`, so we need to proxy them
|
||||
thumb: createStream({
|
||||
service: "instagram",
|
||||
type: "default",
|
||||
u: e.node?.display_url,
|
||||
filename: "image.jpg"
|
||||
}),
|
||||
url: e.node?.video_url
|
||||
})
|
||||
} else {
|
||||
multiple.push({
|
||||
type: "photo",
|
||||
thumb: createStream({
|
||||
service: "instagram",
|
||||
type: "default",
|
||||
u: e.node?.display_url,
|
||||
filename: "image.jpg"
|
||||
}),
|
||||
url: e.node?.display_url
|
||||
})
|
||||
}
|
||||
})
|
||||
} else if (data?.shortcode_media?.video_url) {
|
||||
single = data.shortcode_media.video_url
|
||||
} else if (data?.shortcode_media?.display_url) {
|
||||
return {
|
||||
urls: data?.shortcode_media?.display_url,
|
||||
isPhoto: true
|
||||
}
|
||||
} else {
|
||||
return { error: 'ErrorEmptyDownload' }
|
||||
}
|
||||
const picker = sidecar.edges
|
||||
.filter(e => e.node?.display_url)
|
||||
.map(e => {
|
||||
const type = e.node?.is_video ? "video" : "photo";
|
||||
const url = type === "video" ? e.node?.video_url : e.node?.display_url;
|
||||
|
||||
if (single) {
|
||||
return {
|
||||
type, url,
|
||||
/* thumbnails have `Cross-Origin-Resource-Policy`
|
||||
** set to `same-origin`, so we need to proxy them */
|
||||
thumb: createStream({
|
||||
service: "instagram",
|
||||
type: "default",
|
||||
u: e.node?.display_url,
|
||||
filename: "image.jpg"
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
if (picker.length) return { picker }
|
||||
} else if (data?.shortcode_media?.video_url) {
|
||||
return {
|
||||
urls: single,
|
||||
urls: data.shortcode_media.video_url,
|
||||
filename: `instagram_${obj.id}.mp4`,
|
||||
audioFilename: `instagram_${obj.id}_audio`
|
||||
}
|
||||
} else if (multiple.length) {
|
||||
return { picker: multiple }
|
||||
} else {
|
||||
return { error: 'ErrorEmptyDownload' }
|
||||
} else if (data?.shortcode_media?.display_url) {
|
||||
return {
|
||||
urls: data.shortcode_media.display_url,
|
||||
isPhoto: true
|
||||
}
|
||||
}
|
||||
|
||||
return { error: 'ErrorEmptyDownload' }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue