instagram: add freebind dispatcher support

This commit is contained in:
wukko 2024-05-14 22:08:32 +06:00 committed by dumbmoron
parent 4b0814a2ec
commit e44927e5ad
No known key found for this signature in database

View file

@ -41,7 +41,10 @@ const cachedDtsg = {
expiry: 0
}
async function findDtsgId(cookie) {
export default function(obj) {
const dispatcher = obj.dispatcher;
async function findDtsgId(cookie) {
try {
if (cachedDtsg.expiry > Date.now()) return cachedDtsg.value;
@ -49,7 +52,8 @@ async function findDtsgId(cookie) {
headers: {
...commonHeaders,
cookie
}
},
dispatcher
}).then(r => r.text());
const token = data.match(/"dtsg":{"token":"(.*?)"/)[1];
@ -61,9 +65,9 @@ async function findDtsgId(cookie) {
return false;
}
catch {}
}
}
async function request(url, cookie, method = 'GET', requestData) {
async function request(url, cookie, method = 'GET', requestData) {
let headers = {
...commonHeaders,
'x-ig-www-claim': cookie?._wwwClaim || '0',
@ -78,6 +82,7 @@ async function request(url, cookie, method = 'GET', requestData) {
method,
headers,
body: requestData && new URLSearchParams(requestData),
dispatcher
});
if (data.headers.get('X-Ig-Set-Www-Claim') && cookie)
@ -85,8 +90,8 @@ async function request(url, cookie, method = 'GET', requestData) {
updateCookie(cookie, data.headers);
return data.json();
}
async function getMediaId(id, { cookie, token } = {}) {
}
async function getMediaId(id, { cookie, token } = {}) {
const oembedURL = new URL('https://i.instagram.com/api/v1/oembed/');
oembedURL.searchParams.set('url', `https://www.instagram.com/p/${id}/`);
@ -95,29 +100,32 @@ async function getMediaId(id, { cookie, token } = {}) {
...mobileHeaders,
...( token && { authorization: `Bearer ${token}` } ),
cookie
}
},
dispatcher
}).then(r => r.json()).catch(() => {});
return oembed?.media_id;
}
}
async function requestMobileApi(mediaId, { cookie, token } = {}) {
async function requestMobileApi(mediaId, { cookie, token } = {}) {
const mediaInfo = await fetch(`https://i.instagram.com/api/v1/media/${mediaId}/info/`, {
headers: {
...mobileHeaders,
...( token && { authorization: `Bearer ${token}` } ),
cookie
}
},
dispatcher
}).then(r => r.json()).catch(() => {});
return mediaInfo?.items?.[0];
}
async function requestHTML(id, cookie) {
}
async function requestHTML(id, cookie) {
const data = await fetch(`https://www.instagram.com/p/${id}/embed/captioned/`, {
headers: {
...embedHeaders,
cookie
}
},
dispatcher
}).then(r => r.text()).catch(() => {});
let embedData = JSON.parse(data?.match(/"init",\[\],\[(.*?)\]\],/)[1]);
@ -127,8 +135,8 @@ async function requestHTML(id, cookie) {
embedData = JSON.parse(embedData.contextJSON);
return embedData;
}
async function requestGQL(id, cookie) {
}
async function requestGQL(id, cookie) {
let dtsgId;
if (cookie) {
@ -153,9 +161,9 @@ async function requestGQL(id, cookie) {
?.xdt_api__v1__media__shortcode__web_info
?.items
?.[0];
}
}
function extractOldPost(data, id) {
function extractOldPost(data, id) {
const sidecar = data?.gql_data?.shortcode_media?.edge_sidecar_to_children;
if (sidecar) {
const picker = sidecar.edges.filter(e => e.node?.display_url)
@ -189,9 +197,9 @@ function extractOldPost(data, id) {
isPhoto: true
}
}
}
}
function extractNewPost(data, id) {
function extractNewPost(data, id) {
const carousel = data.carousel_media;
if (carousel) {
const picker = carousel.filter(e => e?.image_versions2)
@ -232,9 +240,9 @@ function extractNewPost(data, id) {
isPhoto: true
}
}
}
}
async function getPost(id) {
async function getPost(id) {
let data, result;
try {
const cookie = getCookie('instagram');
@ -273,9 +281,9 @@ async function getPost(id) {
if (result) return result;
return { error: 'ErrorEmptyDownload' }
}
}
async function usernameToId(username, cookie) {
async function usernameToId(username, cookie) {
const url = new URL('https://www.instagram.com/api/v1/users/web_profile_info/');
url.searchParams.set('username', username);
@ -283,9 +291,9 @@ async function usernameToId(username, cookie) {
const data = await request(url, cookie);
return data?.data?.user?.id;
} catch {}
}
}
async function getStory(username, id) {
async function getStory(username, id) {
const cookie = getCookie('instagram');
if (!cookie) return { error: 'ErrorUnsupported' };
@ -331,9 +339,8 @@ async function getStory(username, id) {
}
return { error: 'ErrorCouldntFetch' };
}
}
export default function(obj) {
const { postId, storyId, username } = obj;
if (postId) return getPost(postId);
if (username && storyId) return getStory(username, storyId);