Created
January 21, 2020 18:22
-
-
Save AABoyles/7472f3b4dc0391c733d050e5baab3faa to your computer and use it in GitHub Desktop.
Scrape the Metaculus API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let script = document.createElement('script'); | |
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.1.0/papaparse.min.js'; | |
document.getElementsByTagName('body')[0].append(script); | |
function download(text, filename='data.txt', mime='text/plain'){ | |
let link = document.createElement('a'); | |
link.href = window.URL.createObjectURL(new Blob([text], {type: mime})); | |
link.setAttribute('download', filename); | |
link.click(); | |
} | |
function downloadJSON(json){ | |
download(JSON.stringify(json), 'data.json', 'application/json'); | |
} | |
function downloadCSV(json){ | |
download(Papa.unparse(json), 'data.csv', 'text/csv'); | |
} | |
function getMyPredictions(){ | |
return new Promise((res, rej) => { | |
fetch(`https://www.metaculus.com/api2/questions/?guessed_by=${metacData.user.id}&order_by=-activity&page=1`).then(data1 => data1.json().then(predictions => { | |
let n = Math.ceil(predictions.count/20); | |
let requests = []; | |
for(let i = 2; i <= n; i++){ | |
requests.push( | |
fetch(`https://www.metaculus.com/api2/questions/?guessed_by=${metacData.user.id}&order_by=-activity&page=${i}`).then(data2 => data2.json().then(page => { | |
predictions.results = predictions.results.concat(page.results); | |
})) | |
); | |
} | |
Promise.all(requests).then(() => res(predictions.results)); | |
})); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use by copying/pasting into Dev Console while logged into Metaculus. Then: