mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2025-12-18 03:58:49 +00:00
Provide error page for age gated profiles
This commit is contained in:
@@ -3,17 +3,40 @@ const {Parser} = require("./parser/parser")
|
||||
|
||||
/**
|
||||
* @param {string} text
|
||||
* @returns {{status: symbol, value: any}}
|
||||
*/
|
||||
function extractSharedData(text) {
|
||||
const parser = new Parser(text)
|
||||
const index = parser.seek("window._sharedData = ", {moveToMatch: true, useEnd: true})
|
||||
if (index === -1) throw constants.symbols.NO_SHARED_DATA
|
||||
if (index === -1) {
|
||||
// Maybe the profile is age restricted?
|
||||
const age = getRestrictedAge(text)
|
||||
if (age !== null) { // Correct.
|
||||
return {status: constants.symbols.extractor_results.AGE_RESTRICTED, value: age}
|
||||
}
|
||||
return {status: constants.symbols.extractor_results.NO_SHARED_DATA, value: null}
|
||||
}
|
||||
parser.store()
|
||||
const end = parser.seek(";</script>")
|
||||
parser.restore()
|
||||
const sharedDataString = parser.slice(end - parser.cursor)
|
||||
const sharedData = JSON.parse(sharedDataString)
|
||||
return sharedData
|
||||
return {status: constants.symbols.extractor_results.SUCCESS, value: sharedData}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} text
|
||||
*/
|
||||
function getRestrictedAge(text) {
|
||||
const parser = new Parser(text)
|
||||
let index = parser.seek("<h2>Restricted profile</h2>", {moveToMatch: true, useEnd: true})
|
||||
if (index === -1) return null
|
||||
index = parser.seek("<p>", {moveToMatch: true, useEnd: true})
|
||||
if (index === -1) return null
|
||||
const explanation = parser.get({split: "</p>"}).trim()
|
||||
const match = explanation.match(/You must be (\d+?) years? old or over to see this profile/)
|
||||
if (!match) return null
|
||||
return +match[1] // the age
|
||||
}
|
||||
|
||||
module.exports.extractSharedData = extractSharedData
|
||||
|
||||
@@ -37,6 +37,7 @@ class Parser {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next element from the buffer, either up to a token or between two tokens, and update the cursor.
|
||||
* @param {GetOptions} [options]
|
||||
* @returns {String}
|
||||
*/
|
||||
@@ -123,7 +124,7 @@ class Parser {
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek past the next occurance of the string.
|
||||
* Seek to or past the next occurance of the string.
|
||||
* @param {string} toFind
|
||||
* @param {{moveToMatch?: boolean, useEnd?: boolean}} options both default to false
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user