instagram: clean up

This commit is contained in:
wukko 2024-04-29 14:32:05 +06:00
parent b6fa3c920f
commit 3b00be1c43
No known key found for this signature in database
GPG Key ID: 3E30B3F26C7B4AA2

View File

@ -96,23 +96,17 @@ async function requestMobileApi(id, cookie) {
...mobileHeaders, ...mobileHeaders,
cookie cookie
} }
}).then(r => r.text()); }).then(r => r.json()).catch(() => {});
let parsedOembed;
try {
parsedOembed = JSON.parse(oembed)
} catch {
return false;
}
const mediaId = parsedOembed?.media_id;
const mediaId = oembed?.media_id;
if (!mediaId) return false; if (!mediaId) return false;
const mediaInfo = await fetch(`https://i.instagram.com/api/v1/media/${mediaId}/info/`, { const mediaInfo = await fetch(`https://i.instagram.com/api/v1/media/${mediaId}/info/`, {
headers: { headers: {
...mobileHeaders, ...mobileHeaders,
cookie cookie
} }
}).then(r => r.json()); }).then(r => r.json()).catch(() => {});
return mediaInfo?.items?.[0]; return mediaInfo?.items?.[0];
} }
@ -122,7 +116,7 @@ async function requestHTML(id, cookie) {
...embedHeaders, ...embedHeaders,
cookie cookie
} }
}).then(r => r.text()); }).then(r => r.text()).catch(() => {});
let embedData = JSON.parse(data?.match(/"init",\[\],\[(.*?)\]\],/)[1]); let embedData = JSON.parse(data?.match(/"init",\[\],\[(.*?)\]\],/)[1]);
@ -239,7 +233,7 @@ function extractNewPost(data, id) {
} }
async function getPost(id) { async function getPost(id) {
let data, result, dataType = 'new'; let data, result;
try { try {
const cookie = getCookie('instagram'); const cookie = getCookie('instagram');
@ -259,13 +253,9 @@ async function getPost(id) {
if (!data) return { error: 'ErrorCouldntFetch' }; if (!data) return { error: 'ErrorCouldntFetch' };
if (data?.gql_data) { if (data?.gql_data) {
dataType = 'old'
}
if (dataType === 'new') {
result = extractNewPost(data, id)
} else {
result = extractOldPost(data, id) result = extractOldPost(data, id)
} else {
result = extractNewPost(data, id)
} }
if (result) return result; if (result) return result;