instagram: add fetching using bearer token (#487)

for total of SEVEN methods of getting post info, i cannot bear this anymore

also prevent repetitive oembed pulling
This commit is contained in:
wukko 2024-05-03 19:54:06 +06:00 committed by GitHub
parent 182e32d5c3
commit 6403cc8c17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 6 deletions

View file

@ -2,6 +2,9 @@
"instagram": [ "instagram": [
"mid=<replace>; ig_did=<with>; csrftoken=<your>; ds_user_id=<own>; sessionid=<cookies>" "mid=<replace>; ig_did=<with>; csrftoken=<your>; ds_user_id=<own>; sessionid=<cookies>"
], ],
"instagram_bearer": [
"token=<token_with_no_bearer_in_front>", "token=IGT:2:<looks_like_this>"
],
"reddit": [ "reddit": [
"client_id=<replace_this>; client_secret=<replace_this>; refresh_token=<replace_this>" "client_id=<replace_this>; client_secret=<replace_this>; refresh_token=<replace_this>"
], ],

View file

@ -86,24 +86,26 @@ 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 requestMobileApi(id, cookie) {
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}/`);
const oembed = await fetch(oembedURL, { const oembed = await fetch(oembedURL, {
headers: { headers: {
...mobileHeaders, ...mobileHeaders,
...( token && { authorization: `Bearer ${token}` } ),
cookie cookie
} }
}).then(r => r.json()).catch(() => {}); }).then(r => r.json()).catch(() => {});
const mediaId = oembed?.media_id; return oembed?.media_id;
if (!mediaId) return false; }
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}` } ),
cookie cookie
} }
}).then(r => r.json()).catch(() => {}); }).then(r => r.json()).catch(() => {});
@ -236,10 +238,21 @@ async function getPost(id) {
let data, result; let data, result;
try { try {
const cookie = getCookie('instagram'); const cookie = getCookie('instagram');
const bearer = getCookie('instagram_bearer');
const token = bearer?.values()?.token;
// get media_id for mobile api, three methods
let media_id = await getMediaId(id);
if (!media_id && token) media_id = await getMediaId(id, { token });
if (!media_id && cookie) media_id = await getMediaId(id, { cookie });
// mobile api (bearer)
if (media_id && token) data = await requestMobileApi(id, { token });
// mobile api (no cookie, cookie) // mobile api (no cookie, cookie)
data = await requestMobileApi(id); if (!data && media_id) data = await requestMobileApi(id);
if (!data && cookie) data = await requestMobileApi(id, cookie); if (!data && media_id && cookie) data = await requestMobileApi(id, { cookie });
// html embed (no cookie, cookie) // html embed (no cookie, cookie)
if (!data) data = await requestHTML(id); if (!data) data = await requestHTML(id);