2021-01-12 12:43:41 +00:00
/* Imports */
import axios from "axios"
2021-10-15 09:40:05 +00:00
import { getCookie , applyIfCookieExists } from "../utils/getCookies.js"
import { Tabletojson } from "tabletojson"
2021-04-08 16:42:48 +00:00
import toMarkdown from "../utils/toMarkdown.js"
2021-10-15 09:40:05 +00:00
import { calculateStars } from "../utils/stars.js"
import { upsert } from "../utils/mongo-wrapper.js"
2021-01-12 12:43:41 +00:00
/* Definitions */
2022-02-04 19:54:04 +00:00
let htmlEndPoint = 'https://www.infer-pub.com/questions'
2021-04-08 17:32:44 +00:00
String . prototype . replaceAll = function replaceAll ( search , replace ) { return this . split ( search ) . join ( replace ) ; }
2021-10-15 09:40:05 +00:00
const DEBUG _MODE = "on" // "off"
2022-02-04 19:54:04 +00:00
const SLEEP _TIME _RANDOM = 7000 // miliseconds
const SLEEP _TIME _EXTRA = 2000
2021-01-12 12:43:41 +00:00
/* Support functions */
2021-10-15 09:40:05 +00:00
async function fetchPage ( page , cookie ) {
2021-09-22 17:29:23 +00:00
console . log ( ` Page # ${ page } ` )
2021-10-15 09:40:05 +00:00
if ( page == 1 ) {
cookie = cookie . split ( ";" ) [ 0 ] // Interesting that it otherwise doesn't work :(
2021-04-25 17:17:34 +00:00
}
2022-02-04 19:54:04 +00:00
let urlEndpoint = ` ${ htmlEndPoint } /?page= ${ page } `
2021-06-25 16:00:00 +00:00
console . log ( urlEndpoint )
2021-10-15 09:40:05 +00:00
let response = await axios ( {
2021-06-25 16:00:00 +00:00
url : urlEndpoint ,
2021-01-12 12:43:41 +00:00
method : 'GET' ,
2021-10-15 09:40:05 +00:00
headers : ( {
'Content-Type' : 'text/html' ,
'Cookie' : cookie
2021-01-12 12:43:41 +00:00
} ) ,
} )
2021-10-15 09:40:05 +00:00
. then ( res => res . data )
2021-04-08 19:32:03 +00:00
// console.log(response)
2021-01-12 12:43:41 +00:00
return response
}
2021-10-15 09:40:05 +00:00
async function fetchStats ( questionUrl , cookie ) {
let response = await axios ( {
url : questionUrl + "/stats" ,
2021-01-12 12:43:41 +00:00
method : 'GET' ,
2021-10-15 09:40:05 +00:00
headers : ( {
'Content-Type' : 'text/html' ,
'Cookie' : cookie ,
'Referer' : questionUrl ,
2021-01-12 12:43:41 +00:00
} ) ,
} )
2021-10-15 09:40:05 +00:00
. then ( res => res . data )
if ( response . includes ( "Sign up or sign in to forecast" ) ) {
2021-07-04 22:01:09 +00:00
throw Error ( "Not logged in" )
}
2021-01-12 12:43:41 +00:00
// Is binary?
let isbinary = response . includes ( "binary?":true" )
2021-05-02 14:30:02 +00:00
// console.log(`is binary? ${isbinary}`)
2021-02-18 16:12:55 +00:00
let options = [ ]
2021-10-15 09:40:05 +00:00
if ( isbinary ) {
2021-01-12 12:43:41 +00:00
// Crowd percentage
let htmlElements = response . split ( "\n" )
2021-11-05 13:45:55 +00:00
// DEBUG_MODE == "on" ? htmlLines.forEach(line => console.log(line)) : id()
2021-02-16 14:18:23 +00:00
let h3Element = htmlElements . filter ( str => str . includes ( "<h3>" ) ) [ 0 ]
2021-11-05 13:45:55 +00:00
// DEBUG_MODE == "on" ? console.log(h5elements) : id()
2021-01-12 12:43:41 +00:00
let crowdpercentage = h3Element . split ( ">" ) [ 1 ] . split ( "<" ) [ 0 ]
2021-10-15 09:40:05 +00:00
let probability = Number ( crowdpercentage . replace ( "%" , "" ) ) / 100
2021-02-18 16:12:55 +00:00
options . push ( ( {
name : "Yes" ,
probability : probability ,
type : "PROBABILITY"
} ) , ( {
name : "No" ,
2021-10-15 09:40:05 +00:00
probability : + ( 1 - probability ) . toFixed ( 2 ) , // avoids floating point shenanigans
2021-02-18 16:12:55 +00:00
type : "PROBABILITY"
} ) )
2021-10-15 09:40:05 +00:00
} else {
try {
2021-06-11 19:13:28 +00:00
let optionsBody = response . split ( "tbody" ) [ 1 ] // Previously [1], then previously [3] but they added a new table.
// console.log(optionsBody)
let optionsHtmlElement = "<table" + optionsBody + "table>"
let tablesAsJson = Tabletojson . convert ( optionsHtmlElement )
let firstTable = tablesAsJson [ 0 ]
options = firstTable . map ( element => ( {
name : element [ '0' ] ,
2021-10-15 09:40:05 +00:00
probability : Number ( element [ '1' ] . replace ( "%" , "" ) ) / 100 ,
2021-06-11 19:13:28 +00:00
type : "PROBABILITY"
} ) )
2021-10-15 09:40:05 +00:00
} catch ( error ) {
2021-06-11 19:13:28 +00:00
let optionsBody = response . split ( "tbody" ) [ 3 ] // Catch if the error is related to table position
let optionsHtmlElement = "<table" + optionsBody + "table>"
let tablesAsJson = Tabletojson . convert ( optionsHtmlElement )
let firstTable = tablesAsJson [ 0 ]
2021-10-15 09:40:05 +00:00
if ( firstTable ) {
2021-08-31 19:23:08 +00:00
options = firstTable . map ( element => ( {
name : element [ '0' ] ,
2021-10-15 09:40:05 +00:00
probability : Number ( element [ '1' ] . replace ( "%" , "" ) ) / 100 ,
2021-08-31 19:23:08 +00:00
type : "PROBABILITY"
} ) )
2021-10-15 09:40:05 +00:00
} else {
2021-08-31 19:23:08 +00:00
// New type of question, tricky to parse the options
// Just leave options = [] for now.
// https://www.cset-foretell.com/blog/rolling-question-formats
2021-10-15 09:40:05 +00:00
}
2021-06-11 19:13:28 +00:00
}
2021-10-15 09:40:05 +00:00
2021-01-12 12:43:41 +00:00
}
2021-04-25 17:17:34 +00:00
// Description
let descriptionraw = response . split ( ` <meta name="description" content=" ` ) [ 1 ]
2021-02-03 17:35:38 +00:00
let descriptionprocessed1 = descriptionraw . split ( ` "> ` ) [ 0 ]
let descriptionprocessed2 = descriptionprocessed1 . replace ( ">" , "" )
let descriptionprocessed3 = descriptionprocessed2 . replace ( "To suggest a change or clarification to this question, please select Request Clarification from the green gear-shaped dropdown button to the right of the question." , ` ` )
2021-04-08 19:32:03 +00:00
// console.log(descriptionprocessed3)
2021-10-15 09:40:05 +00:00
let descriptionprocessed4 = descriptionprocessed3 . replaceAll ( "\r\n\r\n" , "\n" )
let descriptionprocessed5 = descriptionprocessed4 . replaceAll ( "\n\n" , "\n" )
let descriptionprocessed6 = descriptionprocessed5 . replaceAll ( """ , ` " ` )
let descriptionprocessed7 = descriptionprocessed6 . replaceAll ( "'" , "'" )
let descriptionprocessed8 = toMarkdown ( descriptionprocessed7 )
2021-02-03 17:35:38 +00:00
let description = descriptionprocessed8
2021-04-25 17:17:34 +00:00
2021-01-12 12:43:41 +00:00
// Number of forecasts
2021-07-09 16:38:24 +00:00
//console.log(response)
//console.log(response.split("prediction_sets_count":")[1])
2021-01-12 12:43:41 +00:00
let numforecasts = response . split ( "prediction_sets_count":" ) [ 1 ] . split ( "," ) [ 0 ]
2021-04-08 19:32:03 +00:00
// console.log(numforecasts)
2021-04-25 17:17:34 +00:00
2021-01-12 12:43:41 +00:00
// Number of predictors
let numforecasters = response . split ( "predictors_count":" ) [ 1 ] . split ( "," ) [ 0 ]
2021-04-08 19:32:03 +00:00
// console.log(numpredictors)
2021-10-15 09:40:05 +00:00
2021-01-12 12:43:41 +00:00
let result = {
2021-10-15 09:40:05 +00:00
"description" : description ,
2021-02-18 16:12:55 +00:00
"options" : options ,
2021-04-07 20:29:21 +00:00
"timestamp" : new Date ( ) . toISOString ( ) ,
"qualityindicators" : {
2021-10-15 09:40:05 +00:00
"numforecasts" : Number ( numforecasts ) ,
"numforecasters" : Number ( numforecasters ) ,
2022-02-04 19:54:04 +00:00
"stars" : calculateStars ( "Infer" , { numforecasts } )
2021-04-07 20:29:21 +00:00
}
2021-01-12 12:43:41 +00:00
}
2021-10-15 09:40:05 +00:00
2021-01-12 12:43:41 +00:00
return result
}
2021-10-15 09:40:05 +00:00
function isSignedIn ( html ) {
let isSignedInBool = ! ( html . includes ( "You need to sign in or sign up before continuing" ) || html . includes ( "Sign up" ) )
if ( ! isSignedInBool ) {
2021-07-09 16:15:49 +00:00
console . log ( "Error: Not signed in." )
2021-07-04 21:37:08 +00:00
}
2021-08-22 18:08:04 +00:00
console . log ( ` Signed in? ${ isSignedInBool } ` )
return isSignedInBool
2021-07-09 16:15:49 +00:00
}
2021-07-04 21:37:08 +00:00
2021-10-15 09:40:05 +00:00
function isEnd ( html ) {
2021-06-25 16:00:00 +00:00
let isEndBool = html . includes ( "No questions match your filter" )
2021-10-15 09:40:05 +00:00
if ( isEndBool ) {
2021-06-25 16:00:00 +00:00
//console.log(html)
}
console . log ( ` IsEnd? ${ isEndBool } ` )
return isEndBool
2021-01-12 12:43:41 +00:00
}
function sleep ( ms ) {
return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
}
/* Body */
2022-02-04 19:54:04 +00:00
async function infer _inner ( cookie ) {
2021-10-15 09:40:05 +00:00
let i = 1
2021-01-12 12:43:41 +00:00
let response = await fetchPage ( i , cookie )
2021-04-08 16:42:48 +00:00
let results = [ ]
2021-01-12 12:43:41 +00:00
let init = Date . now ( )
2021-04-08 19:32:03 +00:00
// console.log("Downloading... This might take a couple of minutes. Results will be shown.")
2021-10-15 09:40:05 +00:00
while ( ! isEnd ( response ) && isSignedIn ( response ) ) {
2021-01-12 12:43:41 +00:00
let htmlLines = response . split ( "\n" )
2022-01-11 21:55:46 +00:00
// let h4elements = htmlLines.filter(str => str.includes("<h5> <a href=") || str.includes("<h4> <a href="))
2022-02-04 19:54:04 +00:00
let questionHrefs = htmlLines . filter ( str => str . includes ( "https://www.infer-pub.com/questions/" ) )
2021-11-05 13:45:55 +00:00
// console.log(questionHrefs)
2022-02-04 19:54:04 +00:00
2021-05-10 14:54:27 +00:00
2021-10-15 09:40:05 +00:00
if ( process . env . DEBUG _MODE == "on" || DEBUG _MODE == "on" ) {
2021-06-25 16:00:00 +00:00
//console.log(response)
2022-01-11 21:55:46 +00:00
console . log ( "questionHrefs: " )
console . log ( questionHrefs )
2021-04-25 17:17:34 +00:00
}
//console.log("")
//console.log("")
//console.log(h4elements)
2021-10-15 09:40:05 +00:00
2022-01-11 21:55:46 +00:00
for ( let questionHref of questionHrefs ) {
2021-06-25 16:00:00 +00:00
//console.log(h4element)
2021-06-11 19:13:28 +00:00
2022-01-11 21:55:46 +00:00
let elementSplit = questionHref . split ( '"><span>' )
let url = elementSplit [ 0 ] . split ( '<a href="' ) [ 1 ]
let title = elementSplit [ 1 ] . replace ( '</h4>' , "" ) . replace ( '</h5>' , "" ) . replace ( "</span></a>" , "" )
await sleep ( Math . random ( ) * SLEEP _TIME _RANDOM + SLEEP _TIME _EXTRA ) // don't be as noticeable
2021-06-11 19:13:28 +00:00
2021-10-15 09:40:05 +00:00
try {
2021-01-12 12:43:41 +00:00
let moreinfo = await fetchStats ( url , cookie )
let question = ( {
2021-10-15 09:40:05 +00:00
"title" : title ,
"url" : url ,
2022-02-04 19:54:04 +00:00
"platform" : "Infer" ,
2021-10-15 09:40:05 +00:00
... moreinfo
} )
if ( i % 30 == 0 && ! ( process . env . DEBUG _MODE == "on" || DEBUG _MODE == "on" ) ) {
console . log ( ` Page # ${ i } ` && ! ( process . env . DEBUG _MODE == "on" || DEBUG _MODE == "on" ) )
console . log ( question )
}
results . push ( question )
if ( process . env . DEBUG _MODE == "on" || DEBUG _MODE == "on" ) {
console . log ( url )
console . log ( question )
}
} catch ( error ) {
2021-04-08 17:19:56 +00:00
console . log ( error )
2021-01-12 12:43:41 +00:00
console . log ( ` We encountered some error when fetching the URL: ${ url } , so it won't appear on the final json ` )
}
}
2021-10-15 09:40:05 +00:00
2021-06-25 16:00:00 +00:00
i ++
//i=Number(i)+1
2022-02-04 19:54:04 +00:00
console . log ( "Sleeping for ~5secs so as to not be as noticeable to the infer servers" )
2022-01-11 21:55:46 +00:00
await sleep ( Math . random ( ) * SLEEP _TIME _RANDOM + SLEEP _TIME _EXTRA ) // don't be as noticeable
2021-10-15 09:40:05 +00:00
try {
2021-01-12 12:43:41 +00:00
response = await fetchPage ( i , cookie )
2021-10-15 09:40:05 +00:00
} catch ( error ) {
2021-04-08 19:32:03 +00:00
console . log ( error )
2021-01-12 12:43:41 +00:00
console . log ( ` The program encountered some error when fetching page # ${ i } , so it won't appear on the final json. It is possible that this page wasn't actually a prediction question pages ` )
}
}
2021-04-08 16:42:48 +00:00
// let string = JSON.stringify(results,null, 2)
2022-02-04 19:54:04 +00:00
// fs.writeFileSync('./data/infer-questions.json', string);
2021-04-11 18:08:59 +00:00
// console.log(results)
2021-10-15 09:40:05 +00:00
if ( results . length > 0 ) {
2022-02-04 19:54:04 +00:00
await upsert ( results , "infer-questions" )
2021-10-15 09:40:05 +00:00
} else {
2021-07-09 16:38:24 +00:00
console . log ( "Not updating results, as process was not signed in" )
}
2021-10-15 09:40:05 +00:00
2021-01-12 12:43:41 +00:00
let end = Date . now ( )
2021-10-15 09:40:05 +00:00
let difference = end - init
console . log ( ` Took ${ difference / 1000 } seconds, or ${ difference / ( 1000 * 60 ) } minutes. ` )
2021-01-12 12:43:41 +00:00
}
2021-04-10 18:18:22 +00:00
2022-02-04 18:32:53 +00:00
export async function infer ( ) {
2022-02-04 19:54:04 +00:00
let cookie = process . env . INFER _COOKIE || getCookie ( "infer" )
await applyIfCookieExists ( cookie , infer _inner )
2021-04-10 18:18:22 +00:00
}