-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapeMatchData.js
More file actions
139 lines (121 loc) · 3.99 KB
/
scrapeMatchData.js
File metadata and controls
139 lines (121 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* scrapeMatchData.js
* Daniel Posse
*
* Reads in array of url strings created by scrapeTournamentData.js
* scrapes match data from smash.gg brackets
*/
// import modules
const axios = require("axios");
const fs = require("fs");
const csv = require("fast-csv");
const cheerio = require("cheerio");
csv
.fromPath("tournamentData.csv", { headers: true })
.on("data", async data => {
/**
* Data object looks like
* {
* name: tournament name
* date: tournament date
* entrants: number of players
* winner: tournament winner
* url: ssbwiki page for tournament
* }
*
*/
//console.log(data);
/**
* TODO
*
* [X] - go to data.url
* [X] - find bracket link
* [ ] - go to bracket url
* [ ] - bracket matches into database
*/
let bracketUrl = await findBracketUrl(data.url);
//get out of current iteration if we got undefined for bracketUrl
if (bracketUrl === undefined) return;
console.log(bracketUrl);
// find out if link is smash.gg or challonge.com
let smashRegExp = /smash.gg/;
let challongeRegExp = /challonge.com/;
//checking for smash.gg first since most tournaments listed there
if (bracketUrl.match(smashRegExp) !== null) {
//console.log('this tournament is on smash.gg\n');
scrapeSmashgg(bracketUrl);
} else if (bracketUrl.match(challongeRegExp) !== null) {
//console.log('this tournament is on challonge.com\n');
scrapeChallonge(bracketUrl);
} else {
//console.log('Tournament not listed on smash.gg or challonge.com');
}
})
.on("end", () => {
console.log("hey im done");
});
async function findBracketUrl(tournamentUrl) {
let url;
const response = await axios.get(tournamentUrl);
/*console.log(response.data); works, throws out way too much html */
/* axios reponse object: {
data: {},
status: 200,
statusText: 'OK',
headers: {},
config: {},
request: {}
}
*/
/*
* Current html layout:
* Under the "Results" section in any tournament page there is
* <span class="mw-headline" id="Super_Smash_Bros._Ultimate_singles">
* <i>
* <a href="/Super_Smash_Bros._Ultimate" title="Super Smash Bros. Ultimate">Super Smash Bros. Ultimate</a>
* </i> singles
* </span>
*
* currently get all spans with class mw-headline with cheerio, then iterate through and find the one with correct id
* possible improvement - find a way to get straight to id. $('#Super_Smash_Bros._Ultimate_singles') doesn't work?
*/
const $ = cheerio.load(response.data);
const headlines = $("span.mw-headline");
headlines.each((index, headline) => {
if ($(headline).attr("id") === "Super_Smash_Bros._Ultimate_singles") {
/*
* $(headline).html() returns
* "<i><a href="[link]" title="...">Super...</a></i> singles"
* this is the html inside the span
*/
/*
HTML layout of ssbwiki tournament pages:
<h3>
<!-- HERE IS THE SPAN WE FIND FROM HEADLINES -->
<span class="mw-headline" id="Super_Smash_Bros._Ultimate_singles">
<i>
<a href="/Super_Smash_Bros._Ultimate" title="...">[...]</a>
</i>
" singles"
</span>
</h3>
<p>
"([num entrants] entrants)"
<br>
<!-- HERE IS THE a ELEMENT WITH THE URL WE WANT -->
<a href="[link to smash.gg or challonge.com page]">Top [#] bracket</a>
<!-- more bracket links below if applicable -->
</p>
*/
url = $(headline)
.parent()
.next()
.children()
.next()
.first()
.attr("href");
//console.log(url);
} //end if
}); //end each
return url;
} //end findBracketUrl