From d660c849411808d12f8ab75beac87d633491c6e2 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 2 Nov 2021 01:58:03 +1300 Subject: [PATCH] Fix shortcode fetching --- src/lib/collectors.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/lib/collectors.js b/src/lib/collectors.js index 2d7da78..a1de5fb 100644 --- a/src/lib/collectors.js +++ b/src/lib/collectors.js @@ -407,23 +407,18 @@ async function getOrFetchShortcode(shortcode) { * @returns {Promise<{result: import("./types").TimelineEntryN3, fromCache: boolean}>} */ function fetchShortcodeData(shortcode) { - // example actual query from web: - // query_hash=2b0673e0dc4580674a88d426fe00ea90&variables={"shortcode":"xxxxxxxxxxx","child_comment_count":3,"fetch_comment_count":40,"parent_comment_count":24,"has_threaded_comments":true} - // we will not include params about comments, which means we will not receive comments, but everything else should still work fine - const p = new URLSearchParams() - p.set("query_hash", constants.external.shortcode_query_hash) - p.set("variables", JSON.stringify({shortcode})) + // embed endpoint unfortunately only returns a single image, or a single video thumbnail return requestCache.getOrFetchPromise("shortcode/"+shortcode, () => { - return switcher.request("post_graphql", `https://www.instagram.com/graphql/query/?${p.toString()}`, async res => { - if (res.status === 302) throw constants.symbols.INSTAGRAM_BLOCK_TYPE_DECEMBER + return switcher.request("post_graphql", `https://www.instagram.com/p/${shortcode}/embed/captioned/`, async res => { if (res.status === 429) throw constants.symbols.RATE_LIMITED - }).then(res => res.json()).then(root => { - /** @type {import("./types").TimelineEntryN3} */ - const data = root.data.shortcode_media + }).then(res => res.text()).then(text => { + const textData = text.match(/window\.__additionalDataLoaded\('extra',(.*)\);<\/script>/)[1] + let data = JSON.parse(textData) if (data == null) { // the thing doesn't exist throw constants.symbols.NOT_FOUND } else { + data = data.shortcode_media history.report("post", true) if (constants.caching.db_post_n3) { db.prepare("REPLACE INTO Posts (shortcode, id, id_as_numeric, username, json) VALUES (@shortcode, @id, @id_as_numeric, @username, @json)") @@ -440,7 +435,7 @@ function fetchShortcodeData(shortcode) { return data } }).catch(error => { - if (error === constants.symbols.RATE_LIMITED || error === constants.symbols.INSTAGRAM_BLOCK_TYPE_DECEMBER) { + if (error === constants.symbols.RATE_LIMITED) { history.report("post", false, error) } throw error