Skip to content

Commit 230d576

Browse files
committed
Initializing
0 parents  commit 230d576

File tree

5 files changed

+473
-0
lines changed

5 files changed

+473
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
input.xml
3+
output.csv

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### PWP checker
2+
3+
Crawls the DCI page for planeswalker points for the last season as Wizards has refused to provide a decent API for this.
4+
5+
6+
#Installation:
7+
8+
- Make sure you have [NodeJS 10+](https://nodejs.org/en/) and [Git](https://git-scm.com/) installed
9+
- Check out this repository: `git clone https://github.com/vlasn/pwp-check.git`
10+
- Install dependencies (puppeteer and xml2js) via `npm install`
11+
- Run the script `node .`
12+
- In case you're too lazy to rename your input file, you can specify its name as the first argument: `node . iamlazy.xml`. Defaults to `input.xml`
13+
- Should you want to go faster, you can also define the amount of time the script should wait (ms) after opening the points' modal as the second argument: `node . input.xml 500`. This defaults to 1000ms or 1 second.
14+
15+
The script expects your input file format to be XML, as per the following model:
16+
```xml
17+
<LocalPlayers>
18+
<Player FirstName="Veljo" LastName="Lasn" MiddleInitial="" DciNumber="9115259053" CountryCode="EE" IsJudge="False" />
19+
</LocalPlayers>
20+
```

index.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const puppeteer = require('puppeteer')
2+
const fs = require('fs')
3+
const path = require('path')
4+
const xml2js = require('xml2js')
5+
const [,,inputFilename = 'input.xml', timeout = 1000] = process.argv
6+
7+
const t = ms => (new Promise(r => setTimeout(r, ms)))
8+
9+
async function asyncForEach(array, callback) {
10+
for (let index = 0; index < array.length; index++) {
11+
await callback(array[index], index, array)
12+
}
13+
}
14+
15+
const parseInput = filename => new Promise((resolve, reject) => {
16+
const fileToString = fs.readFileSync(path.resolve(__dirname, filename), {encoding: "latin1"})
17+
xml2js.parseString(fileToString, (err, result) =>{
18+
if (err) {
19+
console.log(err)
20+
reject(err)
21+
return
22+
}
23+
const players = result.LocalPlayers.Player.map(({$}) => $)
24+
return resolve(players)
25+
})
26+
})
27+
28+
const navigateAndGetValue = async (page, dciNumber) => {
29+
console.log(`Fetching points for DCI#${dciNumber}`)
30+
await page.goto(`http://www.wizards.com/Magic/PlaneswalkerPoints/${dciNumber}`, { waitUntil: 'networkidle0' })
31+
await page.evaluate(() => ShowPointHistoryModal('Yearly'))
32+
await t(timeout)
33+
const points = await page.evaluate(() => document.querySelector('#YearlyValue > div:nth-child(5) > div.PointsValue').innerText)
34+
return points
35+
}
36+
37+
const iterate = async (page, player, stream) => {
38+
const dur = Date.now()
39+
const points = await navigateAndGetValue(page, player.DciNumber)
40+
if (parseInt(points) < 100) {
41+
console.log(`${player.FirstName} ${player.LastName} scored under 100 points, not logging. \n`)
42+
return
43+
}
44+
const line = `${player.FirstName}, ${player.LastName}, ${player.DciNumber}, ${points} \n`
45+
stream.write(line)
46+
console.log(`Fetched ${points} points for ${player.FirstName} ${player.LastName} in ${Date.now() - dur}ms \n`)
47+
return
48+
}
49+
50+
const output = fs.createWriteStream('output.csv', {flags: 'a'});
51+
52+
(async () => {
53+
const players = await parseInput(inputFilename)
54+
console.log(`Checking ${players.length} players' points..`)
55+
const now = new Date()
56+
const browser = await puppeteer.launch()
57+
const page = await browser.newPage()
58+
59+
await page.setRequestInterception(true);
60+
page.on('request', (request) => {
61+
if (['image', 'font', 'stylesheet'].includes(request.resourceType())){
62+
request.abort();
63+
} else {
64+
request.continue();
65+
}
66+
});
67+
await asyncForEach(players, n => iterate(page, n, output))
68+
await browser.close()
69+
await output.close()
70+
console.log(`Done in ${Date.now() - now}ms`)
71+
return
72+
})()

0 commit comments

Comments
 (0)