-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
289 lines (268 loc) · 8.59 KB
/
index.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
var cheerio = require('cheerio');
var request = require('request');
var eachAsync = require('each-async');
var moment = require('moment');
module.exports = Gosu;
function Gosu(){}
/**
* Callback for fetching match URLs
*
* @callback fetchUrlsCallback
* @param {(null|string)} error - An error string
* @param {array} An array of strings containing URLs
*/
/*
* @param {string} [game] - Game type
* @param {integer} [page=1] - Page number
* @param {fetchUrlsCallback} callback
*/
Gosu.fetchMatchUrls = function (game, page, callback){
if (typeof game === 'function') { // Only required param
var callback = game;
var page = 1;
game = null;
} else if (typeof page === 'function'){ // Only 2 params
var callback = page;
if (typeof game === 'number' || !isNaN(parseInt(game, 10))) { // (page, callback)
page = game;
game = null;
} else { // (game, callback)
page = 1;
}
}
// Check type of 'page'
if (page === null) {
page = 1;
} else if (isNaN(page) || parseInt(Number(page)) != page || isNaN(parseInt(page, 10))) {
return callback('Invalid page number');
}
// Map URLs to game types
var gameSuffix = {
csgo: '/counterstrike',
dota2: '/dota2',
hearthstone: '/hearthstone',
hots: '/heroesofthestorm',
lol: '/lol'
}
var urls = []; // result
var requrl = 'http://www.gosugamers.net';
if (game) {
if (gameSuffix[game]) requrl = requrl + gameSuffix[game];
else return callback('Unknown game type: '+game);
}
requrl = requrl + '/gosubet?r-page='+page;
request(requrl, function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
$('table.simple.matches tbody').each(function (i, element) {
var table = cheerio.load(element);
table('tr td a.match').each(function (i, element) {
// Value bet enabled possibly?
urls.push('http://www.gosugamers.net'+element.attribs.href);
});
});
return callback(null, urls);
} else {
return callback(error);
}
});
}
/**
* Callback for fetching VOD URLs
*
* @callback fetchUrlsCallback
* @param {(null|string)} error - An error string
* @param {array} An array of strings containing URLs
*/
/*
* @param {string} [game] - Game type
* @param {integer} [page=1] - Page number
* @param {fetchUrlsCallback} callback
*/
Gosu.fetchVodUrls = function (game, page, callback){
if (typeof game === 'function') { // Only required param
var callback = game;
var page = 1;
game = null;
} else if (typeof page === 'function'){ // Only 2 params
var callback = page;
if (typeof game === 'number' || !isNaN(parseInt(game, 10))) { // (page, callback)
page = game;
game = null;
} else { // (game, callback)
page = 1;
}
}
// Check type of 'page'
if (page === null) {
page = 1;
} else if (isNaN(page) || parseInt(Number(page)) != page || isNaN(parseInt(page, 10))) {
return callback('Invalid page number');
}
// Map URLs to game types
var gameSuffix = {
csgo: '/counterstrike/vods',
dota2: '/dota2/vods',
hearthstone: '/hearthstone/vods',
hots: '/heroesofthestorm/vods',
lol: '/lol/vods'
}
var urls = []; // result
var requrl = 'http://www.gosugamers.net';
if (game) {
if (gameSuffix[game]) requrl = requrl + gameSuffix[game];
else return callback('Unknown game type: '+game);
}
request(requrl, function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
//Get to the table that has all the match information
$('.content .simple tbody').each(function (i, element) {
var table = cheerio.load(element);
//Get to the <a tag that has the URL to the vod of the match
table('tr td a.video').each(function (i, element) {
//split the link to remove the ?vodid off the end. usefull when returning the array because we can sort and uniq them
var temp = element.attribs.href.split(/[?]/);
urls.push('http://www.gosugamers.net'+temp[0]);
});
});
//Function to sort and return only unique entries
function sort_unique(arr) {
arr = arr.sort(function (a, b) { return a*1 - b*1; });
var ret = [arr[0]];
for (var i = 1; i < arr.length; i++) { // start loop at 1 as element 0 can never be a duplicate
if (arr[i-1] !== arr[i]) {
ret.push(arr[i]);
}
}
return ret;
}
//Sort the urls and only return uniq entries (ie no duplicates) as the parseMatch function will get all VODS on a page so
//dont need all the extra urls for each match (for multi round matches)
var sortedurls = sort_unique(urls);
return callback(null, sortedurls);
} else {
return callback(error);
}
});
}
/**
* Callback for parsing match URLs
*
* @callback parseMatchCallback
* @param {(null|string)} error - An error string
* @param {object} Match data
*/
/*
* @param {string} url - The full URL to a Gosu match
* @callback {parseMatchCallback} callback
*/
Gosu.parseMatch = function (url, callback){
var match = {
url: url,
home: {},
away: {},
status: 'Unknown'
};
var type = url.split('/');
match.type = type[3];
request(url, function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
match.home.name = $('.opponent1 h3 a').text();
match.home.url = $('.opponent1 h3 a').attr('href');
match.home.country = $('.opponent1 .flag').attr('title');
match.home.rank = parseInt($('.opponent1 .ranked').text().replace(/[^0-9\.]+/g, ''));
match.away.name = $('.opponent2 h3 a').text();
match.away.url = $('.opponent2 h3 a').attr('href');
match.away.country = $('.opponent2 .flag').attr('title');
match.away.rank = parseInt($('.opponent2 .ranked').text().replace(/[^0-9\.]+/g, ''));
match.rounds = $('.bestof').text();
match.tournament = $('.match-heading-overlay h1 a').text();
match.tournamenturl = $('.match-heading-overlay h1 a').attr('href');
//var str = $('.gg-row .col-12 .dark-buttons.matches-stream-options .matches-streams span textarea iframe').attr('src');
//match.vods.url = str.substring(str.lastIndexOf("/")+1,str.lastIndexOf("?"));
if ($('#valuebet').index()) {
match.valueBet = true;
} else {
match.valueBet = false;
}
// Matches that aren't live
if ($('.vs .datetime').text()){
var datetime = $('.vs .datetime').text().replace('/\n/g','').trim();
// Timzones as strings don't work, so we have to add offsets manually
if (datetime.indexOf('CEST') > -1) {
datetime = datetime + ' +0200';
} else if (datetime.indexOf('CET') > -1){
datetime = datetime + ' +0100';
}
// Save as unix timestamp
match.datetime = moment(datetime, 'MMMM DD, dddd, HH:mm Z').unix();
// If match complete, record results
if ($('.hidden.results').children().first().text()) {
match.status = 'Complete';
match.home.score = Number($('.hidden.results').children().first().text());
match.away.score = Number($('.hidden.results').children().last().text());
var urls = $('.matches-streams span textarea iframe');
//console.log(urls);
var index;
match.vods = [];
if (urls.length == 0){
match.vods.push("No VODs found for this match");
}else{
for (index = 0;index < urls.length; ++index){
//console.log(urls[index].attribs.src.split(/[/?]/)[4]);
match.vods.push(urls[index].attribs.src.split(/[/?]/)[4]);
}
}
} else {
match.status = 'Upcoming';
}
} else if ($('.vs .match-is-live').text()) { // Current Match
match.status = 'Live';
var twitch = $('.matches-streams .match-stream-tab object').attr('data').split("=")[1];
var twitchurl = "www.twitch.tv/"+twitch;
match.livestream = twitchurl;
//match.vods.push(twitchurl);
} else if ($('.upcomming')) { // Special case for upcoming matches with no scheduled time
match.status = 'Upcoming';
} else {
return callback('Unknown match status');
}
return callback(null, match);
} else {
return callback(error);
}
});
}
/**
* Callback for parsing match URLs
*
* @callback parseMatchesCallback
* @param {(null|string)} error - An error string
* @param {array} Array of objects containing match data
*/
/*
* @param {array} urls - An array containing full URLs to Gosu matches
* @callback {parseMatchesCallback} callback
*/
Gosu.parseMatches = function (urls, callback){
var self = this;
var matches = [];
eachAsync(urls, function (url, index, done) {
self.parseMatch(url, function (err, match) {
if (err) {
done(err);
} else {
matches.push(match);
done(null);
}
});
}, function (err){
if (err) {
return callback(err)
} else { // Success!
return callback(null, matches);
}
});
}