mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2025-12-15 19:05:09 +00:00
Basic error checking
This commit is contained in:
@@ -10,13 +10,16 @@ const timelineEntryCache = new TtlCache(constants.resource_cache_time)
|
||||
|
||||
function fetchUser(username) {
|
||||
return requestCache.getOrFetch("user/"+username, () => {
|
||||
return request(`https://www.instagram.com/${username}/`).then(res => res.text()).then(text => {
|
||||
// require down here or have to deal with require loop. require cache will take care of it anyway.
|
||||
// User -> Timeline -> TimelineImage -> collectors -/> User
|
||||
const User = require("./structures/User")
|
||||
const sharedData = extractSharedData(text)
|
||||
const user = new User(sharedData.entry_data.ProfilePage[0].graphql.user)
|
||||
return user
|
||||
return request(`https://www.instagram.com/${username}/`).then(res => {
|
||||
if (res.status === 404) throw constants.symbols.NOT_FOUND
|
||||
else return res.text().then(text => {
|
||||
// require down here or have to deal with require loop. require cache will take care of it anyway.
|
||||
// User -> Timeline -> TimelineImage -> collectors -/> User
|
||||
const User = require("./structures/User")
|
||||
const sharedData = extractSharedData(text)
|
||||
const user = new User(sharedData.entry_data.ProfilePage[0].graphql.user)
|
||||
return user
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -24,7 +27,7 @@ function fetchUser(username) {
|
||||
/**
|
||||
* @param {string} userID
|
||||
* @param {string} after
|
||||
* @returns {Promise<import("./types").PagedEdges<import("./types").GraphImage>>}
|
||||
* @returns {Promise<import("./types").PagedEdges<import("./types").TimelineEntryN2>>}
|
||||
*/
|
||||
function fetchTimelinePage(userID, after) {
|
||||
const p = new URLSearchParams()
|
||||
@@ -36,7 +39,7 @@ function fetchTimelinePage(userID, after) {
|
||||
}))
|
||||
return requestCache.getOrFetchPromise("page/"+after, () => {
|
||||
return request(`https://www.instagram.com/graphql/query/?${p.toString()}`).then(res => res.json()).then(root => {
|
||||
/** @type {import("./types").PagedEdges<import("./types").GraphImage>} */
|
||||
/** @type {import("./types").PagedEdges<import("./types").TimelineEntryN2>} */
|
||||
const timeline = root.data.user.edge_owner_to_timeline_media
|
||||
return timeline
|
||||
})
|
||||
@@ -86,7 +89,12 @@ function fetchShortcodeData(shortcode) {
|
||||
return request(`https://www.instagram.com/graphql/query/?${p.toString()}`).then(res => res.json()).then(root => {
|
||||
/** @type {import("./types").TimelineEntryN3} */
|
||||
const data = root.data.shortcode_media
|
||||
return data
|
||||
if (data == null) {
|
||||
// the thing doesn't exist
|
||||
throw constants.symbols.NOT_FOUND
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ module.exports = {
|
||||
TYPE_VIDEO: Symbol("TYPE_VIDEO"),
|
||||
TYPE_GALLERY: Symbol("TYPE_GALLERY"),
|
||||
TYPE_GALLERY_IMAGE: Symbol("TYPE_GALLERY_IMAGE"),
|
||||
TYPE_GALLERY_VIDEO: Symbol("TYPE_GALLERY_VIDEO")
|
||||
TYPE_GALLERY_VIDEO: Symbol("TYPE_GALLERY_VIDEO"),
|
||||
NOT_FOUND: Symbol("NOT_FOUND"),
|
||||
NO_SHARED_DATA: Symbol("NO_SHARED_DATA")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,9 @@ class TimelineEntry extends TimelineBaseMethods {
|
||||
/** @type {import("../types").TimelineEntryAll} some properties may not be available yet! */
|
||||
// @ts-ignore
|
||||
this.data = {}
|
||||
const error = new Error("TimelineEntry data was not initalised in same event loop (missing __typename)") // initialise here for a useful stack trace
|
||||
setImmediate(() => { // next event loop
|
||||
if (!this.data.__typename) throw new Error("TimelineEntry data was not initalised in same event loop (missing __typename)")
|
||||
if (!this.data.__typename) throw error
|
||||
})
|
||||
/** @type {string} Not available until fetchExtendedOwnerP is called */
|
||||
this.ownerPfpCacheP = null
|
||||
@@ -29,8 +30,12 @@ class TimelineEntry extends TimelineBaseMethods {
|
||||
}
|
||||
|
||||
async update() {
|
||||
const data = await collectors.fetchShortcodeData(this.data.shortcode)
|
||||
this.applyN3(data)
|
||||
return collectors.fetchShortcodeData(this.data.shortcode).then(data => {
|
||||
this.applyN3(data)
|
||||
}).catch(error => {
|
||||
console.error("TimelineEntry could not self-update; trying to continue anyway...")
|
||||
console.error("E:", error)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
const constants = require("../constants")
|
||||
const {Parser} = require("./parser/parser")
|
||||
|
||||
/**
|
||||
@@ -5,7 +6,8 @@ const {Parser} = require("./parser/parser")
|
||||
*/
|
||||
function extractSharedData(text) {
|
||||
const parser = new Parser(text)
|
||||
parser.seek("window._sharedData = ", {moveToMatch: true, useEnd: true})
|
||||
const index = parser.seek("window._sharedData = ", {moveToMatch: true, useEnd: true})
|
||||
if (index === -1) throw constants.symbols.NO_SHARED_DATA
|
||||
parser.store()
|
||||
const end = parser.seek(";</script>")
|
||||
parser.restore()
|
||||
|
||||
Reference in New Issue
Block a user