2020-01-27 06:03:28 +00:00
|
|
|
const constants = require("../constants")
|
2020-01-12 12:50:21 +00:00
|
|
|
const {Parser} = require("./parser/parser")
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} text
|
2020-04-13 15:46:23 +00:00
|
|
|
* @returns {{status: symbol, value: any}}
|
2020-01-12 12:50:21 +00:00
|
|
|
*/
|
|
|
|
function extractSharedData(text) {
|
|
|
|
const parser = new Parser(text)
|
2020-01-27 06:03:28 +00:00
|
|
|
const index = parser.seek("window._sharedData = ", {moveToMatch: true, useEnd: true})
|
2020-04-13 15:46:23 +00:00
|
|
|
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}
|
|
|
|
}
|
2020-01-12 12:50:21 +00:00
|
|
|
parser.store()
|
|
|
|
const end = parser.seek(";</script>")
|
|
|
|
parser.restore()
|
|
|
|
const sharedDataString = parser.slice(end - parser.cursor)
|
|
|
|
const sharedData = JSON.parse(sharedDataString)
|
2020-04-13 15:46:23 +00:00
|
|
|
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
|
2020-01-12 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.extractSharedData = extractSharedData
|