1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-23 16:37:30 +00:00

Compare commits

..

No commits in common. "fd65ef664610a9d78d4f5f06bce7d358bb3e5b7d" and "1872391a8ea1d5341f35e9458d98bf4d2b155d91" have entirely different histories.

5 changed files with 21 additions and 37 deletions

View File

@ -1,12 +1,12 @@
const constants = require("./constants") const constants = require("./constants")
const {request} = require("./utils/request") const {request} = require("./utils/request")
const switcher = require("./utils/torswitcher") const switcher = require("./utils/torswitcher")
const {selectExtractor} = require("./utils/body") const {extractPreloader} = require("./utils/body")
const {TtlCache, RequestCache, UserRequestCache} = require("./cache") const {TtlCache, RequestCache, UserRequestCache} = require("./cache")
const RequestHistory = require("./structures/RequestHistory") const RequestHistory = require("./structures/RequestHistory")
const fhp = require("fast-html-parser") const fhp = require("fast-html-parser")
const db = require("./db") const db = require("./db")
require("./testimports")(constants, request, selectExtractor, UserRequestCache, RequestHistory, db) require("./testimports")(constants, request, extractPreloader, UserRequestCache, RequestHistory, db)
const requestCache = new RequestCache(constants.caching.resource_cache_time) const requestCache = new RequestCache(constants.caching.resource_cache_time)
/** @type {import("./cache").UserRequestCache<import("./structures/User")|import("./structures/ReelUser")>} */ /** @type {import("./cache").UserRequestCache<import("./structures/User")|import("./structures/ReelUser")>} */
@ -34,7 +34,7 @@ async function fetchUser(username, context) {
return fetchUserFromHTML(username) return fetchUserFromHTML(username)
} }
throw new Error(`Your instance admin selected fetch mode ${mode}, which is now unsupported. Please ask them to use the default fetch mode by omitting that setting.`) throw new Error(`Your instance admin selected fetch mode ${mode}, which is now unsupported. Please use "iweb" instead (the default).`)
} }
/** /**
@ -68,8 +68,12 @@ function fetchUserFromHTML(username) {
// require down here or have to deal with require loop. require cache will take care of it anyway. // require down here or have to deal with require loop. require cache will take care of it anyway.
// User -> Timeline -> TimelineEntry -> collectors -/> User // User -> Timeline -> TimelineEntry -> collectors -/> User
const User = require("./structures/User") const User = require("./structures/User")
const userData = selectExtractor(text) const preloader = extractPreloader(text)
const user = new User(userData) const profileInfoResponse = preloader.find(x => x.request.url === "/api/v1/users/web_profile_info/")
if (!profileInfoResponse) {
throw new Error("No profile info in the preloader.")
}
const user = new User(JSON.parse(profileInfoResponse.result.response).data.user)
history.report("user", true) history.report("user", true)
if (constants.caching.db_user_id) { if (constants.caching.db_user_id) {
const existing = db.prepare("SELECT created, updated_version FROM Users WHERE username = ?").get(user.data.username) const existing = db.prepare("SELECT created, updated_version FROM Users WHERE username = ?").get(user.data.username)

View File

@ -30,7 +30,7 @@ let constants = {
password: null, // If `null`, Bibliogram will run its own Tor process instead. password: null, // If `null`, Bibliogram will run its own Tor process instead.
port: 9051, // If a password is provided, Bibliogram will connect to Tor on this port. (This is ignored when running its own Tor process.) port: 9051, // If a password is provided, Bibliogram will connect to Tor on this port. (This is ignored when running its own Tor process.)
for: { for: {
user_html: true, user_html: false,
timeline_graphql: false, timeline_graphql: false,
post_graphql: false, post_graphql: false,
reel_graphql: false reel_graphql: false

View File

@ -1,16 +1,6 @@
const constants = require("../constants") const constants = require("../constants")
const {Parser} = require("./parser/parser") const {Parser} = require("./parser/parser")
function selectExtractor(text) {
if (text.includes("window._sharedData = ")) {
return extractSharedData(text)
} else if (text.includes("PolarisQueryPreloaderCache")) {
return extractPreloader(text)
} else {
throw constants.symbols.extractor_results.NO_SHARED_DATA
}
}
/** /**
* @param {string} text * @param {string} text
* @returns {{status: symbol, value: any}} * @returns {{status: symbol, value: any}}
@ -22,22 +12,21 @@ function extractSharedData(text) {
// Maybe the profile is age restricted? // Maybe the profile is age restricted?
const age = getRestrictedAge(text) const age = getRestrictedAge(text)
if (age !== null) { // Correct. if (age !== null) { // Correct.
throw constants.symbols.extractor_results.AGE_RESTRICTED return {status: constants.symbols.extractor_results.AGE_RESTRICTED, value: age}
} }
throw constants.symbols.extractor_results.NO_SHARED_DATA return {status: constants.symbols.extractor_results.NO_SHARED_DATA, value: null}
} }
parser.store() parser.store()
const end = parser.seek(";</script>") const end = parser.seek(";</script>")
parser.restore() parser.restore()
const sharedDataString = parser.slice(end - parser.cursor) const sharedDataString = parser.slice(end - parser.cursor)
const sharedData = JSON.parse(sharedDataString) const sharedData = JSON.parse(sharedDataString)
console.log(sharedData)
// check for alternate form of age restrictions // check for alternate form of age restrictions
if (sharedData.entry_data && sharedData.entry_data.HttpGatedContentPage) { if (sharedData.entry_data && sharedData.entry_data.HttpGatedContentPage) {
// ideally extracting the age should be done here, but for the web ui it doesn't matter // lazy fix; ideally extracting the age should be done here, but for the web ui it doesn't matter
throw constants.symbols.extractor_results.AGE_RESTRICTED return {status: constants.symbols.extractor_results.AGE_RESTRICTED, value: null}
} }
return sharedData.entry_data.ProfilePage[0].graphql.user return {status: constants.symbols.extractor_results.SUCCESS, value: sharedData}
} }
/** /**
@ -54,12 +43,7 @@ function extractPreloader(text) {
entries.push(data) entries.push(data)
} }
} }
// entries now has the things return entries
const profileInfoResponse = entries.find(x => x.request.url === "/api/v1/users/web_profile_info/")
if (!profileInfoResponse) {
throw new Error("No profile info in the preloader.")
}
return JSON.parse(profileInfoResponse.result.response).data.user
} }
/** /**
@ -77,6 +61,5 @@ function getRestrictedAge(text) {
return +match[1] // the age return +match[1] // the age
} }
module.exports.selectExtractor = selectExtractor
module.exports.extractSharedData = extractSharedData module.exports.extractSharedData = extractSharedData
module.exports.extractPreloader = extractPreloader module.exports.extractPreloader = extractPreloader

View File

@ -4,8 +4,6 @@ const constants = require("../constants")
const {request} = require("./request") const {request} = require("./request")
const {RequestCache} = require("../cache") const {RequestCache} = require("../cache")
let circuitIndex = 0
class TorManager { class TorManager {
/** /**
* @param {import("@deadcanaries/granax/lib/controller")} tor * @param {import("@deadcanaries/granax/lib/controller")} tor
@ -22,16 +20,12 @@ class TorManager {
let done = false let done = false
let g let g
while (!done) { while (!done) {
const circuitIndexUsed = circuitIndex
g = await request(url, {agent: this.agent}, {log: true, statusLine: "TOR"}) g = await request(url, {agent: this.agent}, {log: true, statusLine: "TOR"})
try { try {
await g.check(test) await g.check(test)
break break
} catch (e) { } catch (e) {
if (circuitIndexUsed === circuitIndex) { await this.newCircuit()
circuitIndex++
await this.newCircuit()
}
} }
} }
return g return g

View File

@ -15,7 +15,10 @@ $theme: () !default
src: url(/static/fonts/cantarell-#{$weight}.otf) format("opentype") src: url(/static/fonts/cantarell-#{$weight}.otf) format("opentype")
font-display: swap // prefer a fallback font until the font file is loaded font-display: swap // prefer a fallback font until the font file is loaded
body, body.use-boring-font body
font-family: "Bariol", sans-serif
body.use-boring-font
// this is useful so that cyrillic and other scripts don't look conspicuous compared to nearby latin letters. // this is useful so that cyrillic and other scripts don't look conspicuous compared to nearby latin letters.
// the use-boring-font class is activated based on the page and heuristics of its contents // the use-boring-font class is activated based on the page and heuristics of its contents
font-family: "Cantarell", sans-serif font-family: "Cantarell", sans-serif