Skip to content

Instantly share code, notes, and snippets.

@AABoyles
Created January 21, 2020 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AABoyles/7472f3b4dc0391c733d050e5baab3faa to your computer and use it in GitHub Desktop.
Save AABoyles/7472f3b4dc0391c733d050e5baab3faa to your computer and use it in GitHub Desktop.
Scrape the Metaculus API
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));
}));
});
}
@AABoyles
Copy link
Author

Use by copying/pasting into Dev Console while logged into Metaculus. Then:

getMyPredictions().then(downloadCSV);
# OR
getMyPredictions().then(downloadJSON);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment