UNPKG

18.6 kBJavaScriptView Raw
1
2function time() {
3 return Math.round((new Date()).getTime() / 1000);
4}
5
6function timeByDate(date, hours) {
7 var date = date.split(".");
8 return new Date(date[1] + "/" + date[0] + "/" + date[2] + ' ' + hours + ':00').getTime() / 1000;
9}
10
11function getSeasonpages(allseasons) {
12 var seasonpages = [];
13 for (var index in allseasons) {
14 if (allseasons.length - 2 <= index) {
15 seasonpages.push(allseasons[index]['link']);
16 }
17 }
18 return seasonpages;
19}
20
21function getVote(html) {
22 var _ = require("underscore");
23 var cheerio = require('cheerio');
24 var $ = cheerio.load(html);
25
26 const result = $("#match_log tr").map((i, element) => ({
27 Zeit: $(element).find('td:nth-of-type(1)').text().trim(),
28 Benutzer: $(element).find('td:nth-of-type(2)').text().trim(),
29 Aktion: $(element).find('td:nth-of-type(3)').text().trim(),
30 Details: $(element).find('td:nth-of-type(4)').text().trim()
31 })).get()
32
33
34 var filtered = _.where(result, { Aktion: "mapvote_ended" });
35 //console.log("============================");
36 //console.log(filtered);
37 //console.log("----------------------------");
38 var first = filtered[0];
39 //console.log(first);
40 //console.log(first.hasOwnProperty('Details'));
41 var output = [];
42
43 if (first && first.hasOwnProperty('Details') && first.hasOwnProperty('Aktion')) {
44 if (first['Details'] != 'timeouted') {
45 output.push(first['Details']);
46 } else {
47 console.log('timeouted.')
48 }
49 }
50
51 return output;
52}
53
54///
55/// exported functions
56///
57
58// gets all seasonpages a team played ever give a team id
59function getAllSeasons (teamID) {
60 var cheerio = require('cheerio'),
61 request = require('request');
62 return new Promise(function (resolve, reject) {
63 request({
64 url: 'https://csgo.99damage.de/de/leagues/teams/' + teamID,
65 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
66 }, function (err, res, html) {
67 if (!err) {
68 var $ = cheerio.load(html);
69 const result = $("#content h2:contains(Werdegang)+table tr").map((i, element) => ({
70 Season: $(element).find('td:nth-of-type(1)').text().trim(),
71 link: $(element).find('td:nth-of-type(2)').children().attr('href'),
72 Score: $(element).find('td:nth-of-type(3)').text().trim()
73 })).get()
74
75 resolve(result);
76 } else {
77 reject(err);
78 }
79 });
80 })
81 }
82
83// gets all matches that are scheduled this season, includes defwins at el
84async function upcomingMatches(teamID) {
85 var cheerio = require('cheerio'),
86 request = require('request');
87 var pages = await getAllSeasons(teamID);
88 var page = pages[pages.lenght - 1];
89 return new Promise(function (resolve, reject) {
90 request({
91 url: page,
92 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
93 }, function (err, res, html) {
94 if (!err) {
95 var _ = require("underscore");
96
97 var $ = cheerio.load(html);
98
99 const result = $(".league_table_matches tr").map((i, element) => ({
100 Date: $(element).find('td:nth-of-type(1)').text().trim(),
101 Team1: $(element).find('td:nth-of-type(2)').text().trim(),
102 Team2: $(element).find('td:nth-of-type(3)').text().trim().replace('vs. ', ''),
103 score: $(element).find('td:nth-of-type(4)').text().trim(),
104 link: $(element).find('td:nth-of-type(2)').children().attr('href')
105 })).get()
106
107 var asT1 = _.where(result, { Team1: team });
108 var asT2 = _.where(result, { Team2: team });
109 var filtered = asT1.concat(asT2);
110
111 resolve(filtered);
112
113 } else {
114 reject(err);
115 }
116 });
117 })
118}
119
120// gets a list of all matchIDs from the seasonpage
121function getMatchesThisSeason (page,team) {
122 var cheerio = require('cheerio'),
123 request = require('request');
124 return new Promise(function (resolve, reject) {
125 request({
126 url: page,
127 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
128 }, function (err, res, html) {
129 if (!err) {
130 var _ = require("underscore");
131
132 var $ = cheerio.load(html);
133
134 const result = $(".league_table_matches tr").map((i, element) => ({
135 Date: $(element).find('td:nth-of-type(1)').text().trim(),
136 Team1: $(element).find('td:nth-of-type(2)').text().trim(),
137 Team2: $(element).find('td:nth-of-type(3)').text().trim().replace('vs. ', ''),
138 score: $(element).find('td:nth-of-type(4)').text().trim(),
139 link: $(element).find('td:nth-of-type(2)').children().attr('href')
140 })).get()
141
142 var asT1 = _.where(result, { Team1: team });
143 var asT2 = _.where(result, { Team2: team });
144 var filtered = asT1.concat(asT2);
145
146 //add all links from matches that have a score
147 var trimmed = [];
148 // match was finished
149 var regex = '(0|1|2):(0|1|2)'
150 for (var index in filtered) {
151 if (filtered[index]['score'].match(regex)) {
152 var temp = filtered[index]['link'];
153 trimmed.push(temp.substr(temp.lastIndexOf('/') + 1));
154 }
155 }
156
157 resolve(trimmed);
158
159 } else {
160 reject(err);
161 }
162 });
163 })
164}
165
166// all pick bans for the last two seasons
167async function getPickBan (teamID, callback) {
168 var cheerio = require('cheerio'),
169 request = require('request');
170 var _ = require("underscore");
171 //get all season pages
172 var pages = await csgo99damage.getAllSeasons(teamID);
173 pages = getSeasonpages(pages);
174 console.log(pages);
175
176 var shortName = await csgo99damage.getShorthand(teamID);
177 var longName = await csgo99damage.getLongName(teamID);
178
179 //var tst = await csgo99damage.getOpponents(pages[pages.length - 1], longName);
180 //console.log(tst);
181
182 var matches = [];
183 for (var index in pages) {
184 var temp = await csgo99damage.getMatchesThisSeason(pages[index], shortName);
185 matches.push(temp);
186 }
187
188 matches = _.flatten(matches);
189 var matchdata = [];
190
191 //get all the matchdata
192 for (var i in matches) {
193 var data = await getMatch(matches[i])
194 matchdata.push(data);
195 }
196
197 var res = {};
198 var t1 = [];
199 var t2 = [];
200 // remove all invalid matches
201 for (var i in matchdata) {
202 var element = matchdata[i];
203 // check if valid
204 if (element && element.hasOwnProperty('pickban') && element.hasOwnProperty('team1') && element.hasOwnProperty('team2') && element.hasOwnProperty('status')) {
205 // check if match has correct status. This eliminates DefWins
206 // status 2 is played and done.
207 if (element['status'] == 2) {
208 if (element['team1'] == longName) {
209 t1.push(element['pickban']);
210 } else if (element['team2'] == longName) {
211 t2.push(element['pickban']);
212 }
213 }
214 }
215 }
216 // filter out empty arrays
217 t1 = t1.filter(e => e.length);
218 t2 = t2.filter(e => e.length);
219
220 res['T1'] = t1;
221 res['T2'] = t2;
222
223 console.log(res);
224
225 return res;
226}
227
228// prints the truth for testing
229function printtest () {
230 console.log("Node.js is evil!");
231}
232
233// get the whole team name of a league team give the correct team id
234async function getLongName (teamID) {
235 var cheerio = require('cheerio'),
236 request = require('request');
237 return new Promise(function (resolve, reject) {
238 request({
239 url: 'https://csgo.99damage.de/de/leagues/teams/' + teamID,
240 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
241 }, function (err, res, html) {
242 if (!err) {
243 var $ = cheerio.load(html);
244 const result = $("#content div h2").text()
245 var out = result.substr(0, result.indexOf('(')).trim();
246 console.log(out);
247 resolve(out);
248 } else {
249 reject(err);
250 }
251 });
252 });
253}
254
255// get the shorthand of a league team give the correct team id
256async function getShorthand (teamID) {
257 var cheerio = require('cheerio'),
258 request = require('request');
259 return new Promise(function (resolve, reject) {
260 request({
261 url: 'https://csgo.99damage.de/de/leagues/teams/' + teamID,
262 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
263 }, function (err, res, html) {
264 if (!err) {
265 var $ = cheerio.load(html);
266 const result = $("#content div h2").text()
267
268 var regExp = /\(([^)]+)\)/;
269 var matches = regExp.exec(result);
270
271 console.log(matches[1]);
272
273 resolve(matches[1]);
274 } else {
275 reject(err);
276 }
277 });
278 });
279}
280
281// gets all available data for a give match id
282function getMatch (matchID) {
283 var cheerio = require('cheerio'),
284 request = require('request');
285
286 return new Promise(function (resolve, reject) {
287 request({
288 url: 'http://csgo.99damage.de/de/leagues/matches/' + matchID,
289 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
290 }, function (err,res, html) {
291 if (!err) {
292 var $ = cheerio.load(html);
293 $('#content').each(function () {
294 var winner = 0, status = 0,
295 score = $(this).find('.score').text().trim(),
296 date = $(this).find('.right').text().replace(' CEST', '').split(', '),
297 team1logo = $($(this).find('.team_logo img')[0]).attr('src'),
298 team2logo = $($(this).find('.team_logo img')[1]).attr('src')
299 streams = [],
300 pickban = getVote(html);
301 $('#content div').each(function (k) {
302 if (k == 42) {
303 var as = $(this).find('a');
304 for (var i = 0; i < as.length; i++) {
305 streams.push([$(as[i]).text(), $(as[i]).attr('href')]);
306 }
307 }
308 });
309 if (score.indexOf('verschoben') >= 0) {
310 winner = 3;
311 status = 4;
312 } else if (score.indexOf('LIVE!') >= 0) {
313 status = 1;
314 } else {
315 if (score.indexOf('defwin') >= 0)
316 status = 3
317 else
318 status = 2;
319 var split = score.substr(0, 5).split(':');
320 split[0] = parseInt(split[0]);
321 split[1] = parseInt(split[1]);
322 if (split[0] < split[1])
323 winner = 2;
324 else if (split[0] == split[1])
325 winner = 3;
326 else
327 winner = 1;
328 }
329 csgo99damage.temp.matches[matchID] = {
330 team1: $($(this).find('.team')[0]).text().trim(),
331 team2: $($(this).find('.team')[1]).text().trim(),
332 team1logo: team1logo,
333 team2logo: team2logo,
334 matchID: matchID,
335 winner: winner,
336 status: status,
337 streams: streams,
338 pickban: pickban,
339 start: timeByDate(date[0], date[1])
340 }
341 resolve({
342 team1: $($(this).find('.team')[0]).text().trim(),
343 team2: $($(this).find('.team')[1]).text().trim(),
344 team1logo: team1logo,
345 team2logo: team2logo,
346 matchID: matchID,
347 winner: winner,
348 status: status,
349 streams: streams,
350 pickban: pickban,
351 start: timeByDate(date[0], date[1])
352 });
353 });
354 } else {
355 reject(err);
356 }
357 });
358 });
359};
360
361// gets all matches currently playing
362function getMatches (callback) {
363 var cheerio = require('cheerio'),
364 request = require('request');
365 if (this.temp.lastUpdate >= 1 && time() - this.temp.lastUpdate < 300) {
366 callback(null, this.temp.matches)
367 } else {
368 request({
369 url: 'http://csgo.99damage.de/de/leagues/matches/',
370 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
371 }, function (err, res, html) {
372 if (!err) {
373 csgo99damage.temp.matches = {};
374 csgo99damage.temp.lastUpdate = time();
375 var $ = cheerio.load(html);
376 $('#content > .item').each(function () {
377
378 var matchID = $($(this).find('a')[1]).attr('href').substr(35),
379 span = $(this).find('span'),
380 status = $(span[3]).text().trim().toLowerCase(),
381 winner = 0,
382 team1 = $(span[4]).text().trim(),
383 team2 = $(span[2]).text().trim(),
384 state = status,
385 hour = '00:00';
386
387 matchID = matchID.substr(0, matchID.indexOf('-'));
388
389 if (status == 'defwin') status = 3;
390 else if (status == 'live') status = 1;
391 else if (status == 'versch.') status = 4;
392 else if (status.indexOf('h') < 0) status = 2;
393 else status = 0;
394
395 if (status == 0) {
396 hour = state.substr(0, 5);
397 }
398 if (status == 2 && state.indexOf(':') >= 0) {
399 var win = state.split(':');
400 win[0] = parseInt(win[0]);
401 win[1] = parseInt(win[1]);
402 if (win[0] == win[1]) {
403 winner = 3;
404 } else if (win[0] < win[1]) {
405 winner = 1;
406 } else {
407 winner = 2;
408 }
409 }
410
411 if (team1 != team2) csgo99damage.temp.matches[matchID] = {
412 status: status,
413 winner: winner,
414 start: timeByDate($(span[1]).text().trim(), hour),
415 team1: team1,
416 team2: team2
417 }
418 });
419 callback(null, csgo99damage.temp.matches);
420 } else {
421 callback(err);
422 }
423 });
424 }
425};
426
427// gets a list of all oppenents from the league page
428// excludes the give team
429async function getOpponents(page, team) {
430 var cheerio = require('cheerio'),
431 request = require('request');
432 return new Promise(function (resolve, reject) {
433 request({
434 url: page,
435 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
436 }, function (err, res, html) {
437 if (!err) {
438 var $ = cheerio.load(html);
439
440 const result = $(".league_table tr").map((i, element) => ({
441 Name: $(element).find('td:nth-of-type(2)').text().trim(),
442 link: $(element).find('td:nth-of-type(2) a').attr('href')
443 })).get()
444
445 result.shift();
446 result.shift();
447 console.log(result);
448
449 //add all links from matches that have a score
450 var trimmed = [];
451 // match was finished
452 for (var index in result) {
453 if (result && result[index].hasOwnProperty('link') && result[index].hasOwnProperty('Name')) {
454 if (result[index]['Name'] != team) {
455
456 var temp = result[index]['link'];
457 console.log(temp);
458 temp = temp.substr(temp.lastIndexOf('/') + 1);
459 temp = temp.substr(0, temp.indexOf('-'));
460 console.log(temp);
461 trimmed.push(temp);
462 }
463
464 }
465 }
466
467 console.log(trimmed);
468
469 resolve(trimmed);
470
471 } else {
472 reject(err);
473 }
474 });
475 })
476}
477
478
479var csgo99damage = {
480 temp: {
481 lastUpdate: 0,
482 matches: {}
483 },
484 getAllSeasons:getAllSeasons,
485 getShorthand:getShorthand,
486 getMatchesThisSeason:getMatchesThisSeason,
487 getLongName:getLongName,
488 getOpponents:getOpponents,
489 printtest:printtest,
490 getPickBan:getPickBan,
491 getMatches:getMatches,
492 getMatch:getMatch,
493 upcomingMatches:upcomingMatches
494}
495
496module.exports = csgo99damage;