Auto-refresh expired profile pictures

This commit is contained in:
Cadence Ember
2020-04-05 02:57:31 +12:00
parent f4969f86db
commit 9fd9a00932
7 changed files with 143 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ const db = require("./db")
require("./testimports")(constants, request, extractSharedData, UserRequestCache, RequestHistory, db)
const requestCache = new RequestCache(constants.caching.resource_cache_time)
/** @type {import("./cache").UserRequestCache<import("./structures/User")|import("./structures/ReelUser")>} */
const userRequestCache = new UserRequestCache(constants.caching.resource_cache_time)
/** @type {import("./cache").TtlCache<import("./structures/TimelineEntry")>} */
const timelineEntryCache = new TtlCache(constants.caching.resource_cache_time)
@@ -116,10 +117,43 @@ function fetchUserFromHTML(username) {
})
}
/**
* @param {string} userID
*/
function updateProfilePictureFromReel(userID) {
const p = new URLSearchParams()
p.set("query_hash", constants.external.reel_query_hash)
p.set("variables", JSON.stringify({
user_id: userID,
include_reel: true
}))
return switcher.request("reel_graphql", `https://www.instagram.com/graphql/query/?${p.toString()}`, async res => {
if (res.status === 429) throw constants.symbols.RATE_LIMITED
return res
}).then(res => res.json()).then(root => {
const result = root.data.user
if (!result) throw constants.symbols.NOT_FOUND
const profilePicURL = result.reel.user.profile_pic_url
if (!profilePicURL) throw constants.symbols.NOT_FOUND
db.prepare("UPDATE Users SET profile_pic_url = ? WHERE user_id = ?").run(profilePicURL, userID)
for (const user of userRequestCache.cache.values()) {
// yes, data.data is correct.
if (user.data.data.id === userID) {
user.data.data.profile_pic_url = profilePicURL
user.data.computeProxyProfilePic()
break // stop checking entries from the cache since we won't find any more
}
}
return profilePicURL
}).catch(error => {
throw error
})
}
/**
* @param {string} userID
* @param {string} username
* @returns {Promise<import("./structures/ReelUser")>}
* @returns {Promise<import("./structures/ReelUser")|import("./structures/User")>}
*/
function fetchUserFromCombined(userID, username) {
// Fetch basic user information
@@ -296,4 +330,5 @@ module.exports.fetchShortcodeData = fetchShortcodeData
module.exports.userRequestCache = userRequestCache
module.exports.timelineEntryCache = timelineEntryCache
module.exports.getOrFetchShortcode = getOrFetchShortcode
module.exports.updateProfilePictureFromReel = updateProfilePictureFromReel
module.exports.history = history

View File

@@ -1,5 +1,5 @@
const constants = require("../constants")
const {proxyImage} = require("../utils/proxyurl")
const {proxyProfilePic} = require("../utils/proxyurl")
const {structure} = require("../utils/structuretext")
const Timeline = require("./Timeline")
require("../testimports")(constants, Timeline)
@@ -15,7 +15,11 @@ class ReelUser {
/** @type {import("./Timeline")} */
this.timeline = new Timeline(this)
this.cachedAt = Date.now()
this.proxyProfilePicture = proxyImage(this.data.profile_pic_url)
this.computeProxyProfilePic()
}
computeProxyProfilePic() {
this.proxyProfilePicture = proxyProfilePic(this.data.profile_pic_url, this.data.id)
}
getStructuredBio() {

View File

@@ -1,5 +1,5 @@
const constants = require("../constants")
const {proxyImage} = require("../utils/proxyurl")
const {proxyProfilePic} = require("../utils/proxyurl")
const {structure} = require("../utils/structuretext")
const Timeline = require("./Timeline")
require("../testimports")(constants, Timeline)
@@ -15,7 +15,11 @@ class User {
this.posts = data.edge_owner_to_timeline_media.count
this.timeline = new Timeline(this)
this.cachedAt = Date.now()
this.proxyProfilePicture = proxyImage(this.data.profile_pic_url)
this.computeProxyProfilePic()
}
computeProxyProfilePic() {
this.proxyProfilePicture = proxyProfilePic(this.data.profile_pic_url, this.data.id)
}
getStructuredBio() {

View File

@@ -5,6 +5,13 @@ function proxyImage(url, width) {
return "/imageproxy?"+params.toString()
}
function proxyProfilePic(url, userID) {
const params = new URLSearchParams()
params.set("userID", userID)
params.set("url", url)
return "/imageproxy?"+params.toString()
}
function proxyVideo(url) {
const params = new URLSearchParams()
params.set("url", url)
@@ -21,5 +28,6 @@ function proxyExtendedOwner(owner) {
}
module.exports.proxyImage = proxyImage
module.exports.proxyProfilePic = proxyProfilePic
module.exports.proxyVideo = proxyVideo
module.exports.proxyExtendedOwner = proxyExtendedOwner

View File

@@ -25,7 +25,8 @@ class Got {
*/
response() {
return this.send().instance.then(res => ({
status: res.statusCode
status: res.statusCode,
headers: new Map(Object.entries(res.headers))
}))
}

View File

@@ -1,6 +1,7 @@
/**
* @typedef GrabResponse
* @property {number} status
* @property {Map<string, string|string[]} headers
*/
// @ts-nocheck
@@ -14,7 +15,7 @@ class GrabReference {
throw new Error("This is the reference class, do not instantiate it.")
}
// Please help me type this
// Please help me write typings for stream()
/**
* @returns {Promise<any>}
*/