Last active
May 14, 2020 15:48
-
-
Save el-gringo/eebc19a46ca1e59dcea883e434bd99e4 to your computer and use it in GitHub Desktop.
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
let page = 0; | |
const rxp = /bought (\d+|a) coffee/g; | |
const url = new URL("https://www.buymeacoffee.com/tGJba8O"); | |
url.searchParams.set('notification', '1'); | |
const gifts = []; | |
function addResult(text) { | |
gifts.push( ...[ ...text.matchAll(rxp) ].map(m => Number(m[1]) || 1) ); | |
const nbCoffees = gifts.reduce((p, n) => p + n, 0); | |
console.log('gifts', ({ | |
nbGifts: gifts.length, | |
nbCoffees, | |
total: nbCoffees * 5 | |
})); | |
++page; | |
go(); | |
} | |
function showTotal() { | |
const values = [ ...new Set(gifts) ].sort(); | |
const results = values.reduce( | |
(p, c) => ({ | |
...p, | |
[c]: gifts.filter(g => g === c).length | |
}), {} | |
); | |
console.log('distribution', results); | |
} | |
function go() { | |
url.searchParams.set('page', page); | |
fetch(url.href) | |
.then(r => r.text()) | |
.then(t => t ? addResult(t) : showTotal()) | |
} | |
go(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment