Last active
April 10, 2021 06:52
-
-
Save joshparkerj/f76ecc4fa685f6f1dac2ffffb8fac5f1 to your computer and use it in GitHub Desktop.
script to quickly get the number of page views for all of the wikipedia articles listed on a wikipedia category page.
This file contains 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
const parser = new DOMParser(); | |
Promise.all( | |
[...document.querySelectorAll('.mw-category-group a')] | |
.map(a => { | |
return fetch(`https://en.wikipedia.org/w/index.php?title=${a.href.split('wiki/')[1]}&action=info`) | |
.then(r => r.text()).then(bodyText => parser.parseFromString(bodyText, 'text/html')) | |
.then(doc => { | |
const title = doc.querySelector('#mw-pageinfo-display-title td ~ td').textContent; | |
const views = doc.querySelector('.mw-pvi-month').textContent; | |
return `Title: ${title} Views: ${views}`; | |
} | |
) | |
} | |
) | |
).then(results => { | |
const getViewCount = result => Number(result.match(/Views: ([\d,]+)/)[1].replace(',','')); | |
const sortedResults = results.sort((a,b) => getViewCount(b) - getViewCount(a)) | |
return sortedResults.join('\n'); | |
} | |
).then(output => console.log(output)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment