fix instagram downloads

merge pull request #169 from dumbmoron/fix-instagram
This commit is contained in:
wukko 2023-08-15 16:10:19 +06:00 committed by GitHub
commit 47924b9273
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,35 +1,69 @@
import got from "got"; import { createStream } from "../../stream/manage.js";
import { genericUserAgent } from "../../config.js";
export default async function(obj) { export default async function(obj) {
// i hate this implementation but fetch doesn't work here for some reason (i personally blame facebook) let data;
let html;
try { try {
html = await got.get(`https://www.instagram.com/p/${obj.id}/`) const url = new URL('https://www.instagram.com/graphql/query/');
html.on('error', () => { url.searchParams.set('query_hash', 'b3055c01b4b222b8a47dc12b090e4e64')
html = false; url.searchParams.set('variables', JSON.stringify({
}); child_comment_count: 3,
html = html ? html.body : false; fetch_comment_count: 40,
has_threaded_comments: true,
parent_comment_count: 24,
shortcode: obj.id
}))
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',
'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'
}
})
data = (await data.json()).data;
} catch (e) { } catch (e) {
html = false; data = false;
} }
if (!html) return { error: 'ErrorCouldntFetch' };
if (!html.includes('application/ld+json')) return { error: 'ErrorEmptyDownload' };
let single, multiple = [], postInfo = JSON.parse(html.split('script type="application/ld+json"')[1].split('">')[1].split('</script>')[0]); if (!data) return { error: 'ErrorCouldntFetch' };
if (Array.isArray(postInfo)) postInfo = postInfo[0];
if (postInfo.video.length > 1) { let single, multiple = [];
for (let i in postInfo.video) { multiple.push({type: "video", thumb: postInfo.video[i]["thumbnailUrl"], url: postInfo.video[i]["contentUrl"]}) } const sidecar = data?.shortcode_media?.edge_sidecar_to_children;
} else if (postInfo.video.length === 1) { if (sidecar) {
single = postInfo.video[0]["contentUrl"] sidecar.edges.forEach(e => {
// todo: allow downloading images once frontend supports it
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 if (data?.shortcode_media?.video_url) {
single = data.shortcode_media.video_url
} else { } else {
return { error: 'ErrorEmptyDownload' } return { error: 'ErrorEmptyDownload' }
} }
if (single) { if (single) {
return { urls: single, filename: `instagram_${obj.id}.mp4`, audioFilename: `instagram_${obj.id}_audio` } return { urls: single, filename: `instagram_${obj.id}.mp4`, audioFilename: `instagram_${obj.id}_audio` }
} else if (multiple) { } else if (multiple.length) {
return { picker: multiple } return { picker: multiple }
} else { } else {
return { error: 'ErrorEmptyDownload' } return { error: 'ErrorEmptyDownload' }