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