Skip to content

Instantly share code, notes, and snippets.

@joshparkerj
Last active April 10, 2021 06:52
Show Gist options
  • Save joshparkerj/f76ecc4fa685f6f1dac2ffffb8fac5f1 to your computer and use it in GitHub Desktop.
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.
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