From 6403cc8c17ab4efcf0b78e449824cad4847f2d3d Mon Sep 17 00:00:00 2001
From: wukko <me@wukko.me>
Date: Fri, 3 May 2024 19:54:06 +0600
Subject: [PATCH] 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
---
 docs/examples/cookies.example.json           |  3 +++
 src/modules/processing/services/instagram.js | 25 +++++++++++++++-----
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/docs/examples/cookies.example.json b/docs/examples/cookies.example.json
index ab3b0c44..73f3378d 100644
--- a/docs/examples/cookies.example.json
+++ b/docs/examples/cookies.example.json
@@ -2,6 +2,9 @@
     "instagram": [
         "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": [
         "client_id=<replace_this>; client_secret=<replace_this>; refresh_token=<replace_this>"
     ],
diff --git a/src/modules/processing/services/instagram.js b/src/modules/processing/services/instagram.js
index 405086de..68420a5d 100644
--- a/src/modules/processing/services/instagram.js
+++ b/src/modules/processing/services/instagram.js
@@ -86,24 +86,26 @@ async function request(url, cookie, method = 'GET', requestData) {
     updateCookie(cookie, data.headers);
     return data.json();
 }
-
-async function requestMobileApi(id, cookie) {
+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}/`);
 
     const oembed = await fetch(oembedURL, {
         headers: {
             ...mobileHeaders,
+            ...( token && { authorization: `Bearer ${token}` } ),
             cookie
         }
     }).then(r => r.json()).catch(() => {});
 
-    const mediaId = oembed?.media_id;
-    if (!mediaId) return false;
+    return oembed?.media_id;
+}
 
+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
         }
     }).then(r => r.json()).catch(() => {});
@@ -236,10 +238,21 @@ async function getPost(id) {
     let data, result;
     try {
         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)
-        data = await requestMobileApi(id);
-        if (!data && cookie) data = await requestMobileApi(id, cookie);
+        if (!data && media_id) data = await requestMobileApi(id);
+        if (!data && media_id && cookie) data = await requestMobileApi(id, { cookie });
 
         // html embed (no cookie, cookie)
         if (!data) data = await requestHTML(id);