UNPKG

22.9 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 that are not yet played
84async function upcomingMatches(teamID) {
85 var cheerio = require('cheerio'),
86 request = require('request');
87
88 //get all season pages
89 var pages = await csgo99damage.getAllSeasons(teamID);
90 var team = await csgo99damage.getShorthand(teamID);
91 pages = getSeasonpages(pages);
92 var page = pages[1];
93
94 return new Promise(function (resolve, reject) {
95 request({
96 url: page,
97 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
98 }, function (err, res, html) {
99 if (!err) {
100 var _ = require("underscore");
101
102 var $ = cheerio.load(html);
103
104 const result = $(".league_table_matches tr").map((i, element) => ({
105 Date: $(element).find('td:nth-of-type(1)').text().trim(),
106 Team1: $(element).find('td:nth-of-type(2)').text().trim(),
107 Team2: $(element).find('td:nth-of-type(3)').text().trim().replace('vs. ', ''),
108 score: $(element).find('td:nth-of-type(4)').text().trim(),
109 link: $(element).find('td:nth-of-type(2)').children().attr('href')
110 })).get()
111
112 var asT1 = _.where(result, { Team1: team });
113 var asT2 = _.where(result, { Team2: team });
114 var filtered = asT1.concat(asT2);
115
116 var filtered = asT1.concat(asT2);
117
118 //add all links from matches that have a score
119 var trimmed = [];
120 // match was finished
121 var regex = '(0|1|2):(0|1|2)'
122 for (var index in filtered) {
123 if (!filtered[index]['score'].match(regex)) {
124 var temp = filtered[index]['link'];
125 trimmed.push(temp.substr(temp.lastIndexOf('/') + 1));
126 }
127 }
128
129 resolve(trimmed);
130
131 } else {
132 reject(err);
133 }
134 });
135 })
136
137}
138
139// gets all matches that were initially scheduled
140async function allThisSeasonMatches(teamID) {
141 var cheerio = require('cheerio'),
142 request = require('request');
143
144 //get all season pages
145 var pages = await csgo99damage.getAllSeasons(teamID);
146 var team = await csgo99damage.getShorthand(teamID);
147 pages = getSeasonpages(pages);
148 var page = pages[1];
149
150 return new Promise(function (resolve, reject) {
151 request({
152 url: page,
153 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
154 }, function (err, res, html) {
155 if (!err) {
156 var _ = require("underscore");
157
158 var $ = cheerio.load(html);
159
160 const result = $(".league_table_matches tr").map((i, element) => ({
161 Date: $(element).find('td:nth-of-type(1)').text().trim(),
162 Team1: $(element).find('td:nth-of-type(2)').text().trim(),
163 Team2: $(element).find('td:nth-of-type(3)').text().trim().replace('vs. ', ''),
164 score: $(element).find('td:nth-of-type(4)').text().trim(),
165 link: $(element).find('td:nth-of-type(2)').children().attr('href')
166 })).get()
167
168 var asT1 = _.where(result, { Team1: team });
169 var asT2 = _.where(result, { Team2: team });
170 var filtered = asT1.concat(asT2);
171
172 var filtered = asT1.concat(asT2);
173
174 //add all links from matches that have a score
175 var trimmed = [];
176 for (var index in filtered) {
177 var temp = filtered[index]['link'];
178 trimmed.push(temp.substr(temp.lastIndexOf('/') + 1));
179 }
180
181 resolve(trimmed);
182
183 } else {
184 reject(err);
185 }
186 });
187 })
188
189}
190
191// gets a list of all matchIDs from the seasonpage
192function getMatchesThisSeason (page,team) {
193 var cheerio = require('cheerio'),
194 request = require('request');
195 return new Promise(function (resolve, reject) {
196 request({
197 url: page,
198 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
199 }, function (err, res, html) {
200 if (!err) {
201 var _ = require("underscore");
202
203 var $ = cheerio.load(html);
204
205 const result = $(".league_table_matches tr").map((i, element) => ({
206 Date: $(element).find('td:nth-of-type(1)').text().trim(),
207 Team1: $(element).find('td:nth-of-type(2)').text().trim(),
208 Team2: $(element).find('td:nth-of-type(3)').text().trim().replace('vs. ', ''),
209 score: $(element).find('td:nth-of-type(4)').text().trim(),
210 link: $(element).find('td:nth-of-type(2)').children().attr('href')
211 })).get()
212
213 var asT1 = _.where(result, { Team1: team });
214 var asT2 = _.where(result, { Team2: team });
215 var filtered = asT1.concat(asT2);
216
217 //add all links from matches that have a score
218 var trimmed = [];
219 // match was finished
220 var regex = '(0|1|2):(0|1|2)'
221 for (var index in filtered) {
222 if (filtered[index]['score'].match(regex)) {
223 var temp = filtered[index]['link'];
224 trimmed.push(temp.substr(temp.lastIndexOf('/') + 1));
225 }
226 }
227
228 resolve(trimmed);
229
230 } else {
231 reject(err);
232 }
233 });
234 })
235}
236
237// all pick bans for the last two seasons
238async function getPickBan (teamID, callback) {
239 var cheerio = require('cheerio'),
240 request = require('request');
241 var _ = require("underscore");
242 //get all season pages
243 var pages = await csgo99damage.getAllSeasons(teamID);
244 pages = getSeasonpages(pages);
245 console.log(pages);
246
247 var shortName = await csgo99damage.getShorthand(teamID);
248 var longName = await csgo99damage.getLongName(teamID);
249
250 //var tst = await csgo99damage.getOpponents(pages[pages.length - 1], longName);
251 //console.log(tst);
252
253 var matches = [];
254 for (var index in pages) {
255 var temp = await csgo99damage.getMatchesThisSeason(pages[index], shortName);
256 matches.push(temp);
257 }
258
259 matches = _.flatten(matches);
260 var matchdata = [];
261
262 //get all the matchdata
263 for (var i in matches) {
264 var data = await getMatch(matches[i])
265 matchdata.push(data);
266 }
267
268 var res = {};
269 var t1 = [];
270 var t2 = [];
271 // remove all invalid matches
272 for (var i in matchdata) {
273 var element = matchdata[i];
274 // check if valid
275 if (element && element.hasOwnProperty('pickban') && element.hasOwnProperty('team1') && element.hasOwnProperty('team2') && element.hasOwnProperty('status')) {
276 // check if match has correct status. This eliminates DefWins
277 // status 2 is played and done.
278 if (element['status'] == 2) {
279 if (element['team1'] == longName) {
280 t1.push(element['pickban']);
281 } else if (element['team2'] == longName) {
282 t2.push(element['pickban']);
283 }
284 }
285 }
286 }
287 // filter out empty arrays
288 t1 = t1.filter(e => e.length);
289 t2 = t2.filter(e => e.length);
290
291 res['T1'] = t1;
292 res['T2'] = t2;
293
294 console.log(res);
295
296 return res;
297}
298
299// prints the truth for testing
300function printtest () {
301 console.log("Node.js is evil!");
302}
303
304// get the whole team name of a league team give the correct team id
305async function getLongName (teamID) {
306 var cheerio = require('cheerio'),
307 request = require('request');
308 return new Promise(function (resolve, reject) {
309 request({
310 url: 'https://csgo.99damage.de/de/leagues/teams/' + teamID,
311 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
312 }, function (err, res, html) {
313 if (!err) {
314 var $ = cheerio.load(html);
315 const result = $("#content div h2").text()
316 var out = result.substr(0, result.indexOf('(')).trim();
317 console.log(out);
318 resolve(out);
319 } else {
320 reject(err);
321 }
322 });
323 });
324}
325
326// get the shorthand of a league team give the correct team id
327async function getShorthand (teamID) {
328 var cheerio = require('cheerio'),
329 request = require('request');
330 return new Promise(function (resolve, reject) {
331 request({
332 url: 'https://csgo.99damage.de/de/leagues/teams/' + teamID,
333 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
334 }, function (err, res, html) {
335 if (!err) {
336 var $ = cheerio.load(html);
337 const result = $("#content div h2").text()
338
339 var regExp = /\(([^)]+)\)/;
340 var matches = regExp.exec(result);
341
342 console.log(matches[1]);
343
344 resolve(matches[1]);
345 } else {
346 reject(err);
347 }
348 });
349 });
350}
351
352// gets all available data for a give match id
353function getMatch (matchID) {
354 var cheerio = require('cheerio'),
355 request = require('request');
356
357 return new Promise(function (resolve, reject) {
358 request({
359 url: 'http://csgo.99damage.de/de/leagues/matches/' + matchID,
360 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
361 }, function (err,res, html) {
362 if (!err) {
363 var $ = cheerio.load(html);
364 $('#content').each(function () {
365 var winner = 0, status = 0,
366 score = $(this).find('.score').text().trim(),
367 date = $(this).find('.right').text().replace(' CEST', '').split(', '),
368 team1logo = $($(this).find('.team_logo img')[0]).attr('src'),
369 team2logo = $($(this).find('.team_logo img')[1]).attr('src')
370 streams = [],
371 pickban = getVote(html);
372 $('#content div').each(function (k) {
373 if (k == 42) {
374 var as = $(this).find('a');
375 for (var i = 0; i < as.length; i++) {
376 streams.push([$(as[i]).text(), $(as[i]).attr('href')]);
377 }
378 }
379 });
380 if (score.indexOf('verschoben') >= 0) {
381 winner = 3;
382 status = 4;
383 } else if (score.indexOf('LIVE!') >= 0) {
384 status = 1;
385 } else {
386 if (score.indexOf('defwin') >= 0)
387 status = 3
388 else
389 status = 2;
390 var split = score.substr(0, 5).split(':');
391 split[0] = parseInt(split[0]);
392 split[1] = parseInt(split[1]);
393 if (split[0] < split[1])
394 winner = 2;
395 else if (split[0] == split[1])
396 winner = 3;
397 else
398 winner = 1;
399 }
400 csgo99damage.temp.matches[matchID] = {
401 team1: $($(this).find('.team')[0]).text().trim(),
402 team2: $($(this).find('.team')[1]).text().trim(),
403 team1logo: team1logo,
404 team2logo: team2logo,
405 matchID: matchID,
406 winner: winner,
407 status: status,
408 streams: streams,
409 pickban: pickban,
410 start: timeByDate(date[0], date[1])
411 }
412 resolve({
413 team1: $($(this).find('.team')[0]).text().trim(),
414 team2: $($(this).find('.team')[1]).text().trim(),
415 team1logo: team1logo,
416 team2logo: team2logo,
417 matchID: matchID,
418 winner: winner,
419 status: status,
420 streams: streams,
421 pickban: pickban,
422 start: timeByDate(date[0], date[1])
423 });
424 });
425 } else {
426 reject(err);
427 }
428 });
429 });
430};
431
432// gets all matches currently playing
433function getMatches (callback) {
434 var cheerio = require('cheerio'),
435 request = require('request');
436 if (this.temp.lastUpdate >= 1 && time() - this.temp.lastUpdate < 300) {
437 callback(null, this.temp.matches)
438 } else {
439 request({
440 url: 'http://csgo.99damage.de/de/leagues/matches/',
441 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
442 }, function (err, res, html) {
443 if (!err) {
444 csgo99damage.temp.matches = {};
445 csgo99damage.temp.lastUpdate = time();
446 var $ = cheerio.load(html);
447 $('#content > .item').each(function () {
448
449 var matchID = $($(this).find('a')[1]).attr('href').substr(35),
450 span = $(this).find('span'),
451 status = $(span[3]).text().trim().toLowerCase(),
452 winner = 0,
453 team1 = $(span[4]).text().trim(),
454 team2 = $(span[2]).text().trim(),
455 state = status,
456 hour = '00:00';
457
458 matchID = matchID.substr(0, matchID.indexOf('-'));
459
460 if (status == 'defwin') status = 3;
461 else if (status == 'live') status = 1;
462 else if (status == 'versch.') status = 4;
463 else if (status.indexOf('h') < 0) status = 2;
464 else status = 0;
465
466 if (status == 0) {
467 hour = state.substr(0, 5);
468 }
469 if (status == 2 && state.indexOf(':') >= 0) {
470 var win = state.split(':');
471 win[0] = parseInt(win[0]);
472 win[1] = parseInt(win[1]);
473 if (win[0] == win[1]) {
474 winner = 3;
475 } else if (win[0] < win[1]) {
476 winner = 1;
477 } else {
478 winner = 2;
479 }
480 }
481
482 if (team1 != team2) csgo99damage.temp.matches[matchID] = {
483 status: status,
484 winner: winner,
485 start: timeByDate($(span[1]).text().trim(), hour),
486 team1: team1,
487 team2: team2
488 }
489 });
490 callback(null, csgo99damage.temp.matches);
491 } else {
492 callback(err);
493 }
494 });
495 }
496};
497
498// gets a list of all oppenents from the league page
499// excludes the give team
500async function getOpponents(page, team) {
501 var cheerio = require('cheerio'),
502 request = require('request');
503 return new Promise(function (resolve, reject) {
504 request({
505 url: page,
506 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
507 }, function (err, res, html) {
508 if (!err) {
509 var $ = cheerio.load(html);
510
511 const result = $(".league_table tr").map((i, element) => ({
512 Name: $(element).find('td:nth-of-type(2)').text().trim(),
513 link: $(element).find('td:nth-of-type(2) a').attr('href')
514 })).get()
515
516 result.shift();
517 result.shift();
518 console.log(result);
519
520 //add all links from matches that have a score
521 var trimmed = [];
522 // match was finished
523 for (var index in result) {
524 if (result && result[index].hasOwnProperty('link') && result[index].hasOwnProperty('Name')) {
525 if (result[index]['Name'] != team) {
526
527 var temp = result[index]['link'];
528 console.log(temp);
529 temp = temp.substr(temp.lastIndexOf('/') + 1);
530 temp = temp.substr(0, temp.indexOf('-'));
531 console.log(temp);
532 trimmed.push(temp);
533 }
534
535 }
536 }
537
538 console.log(trimmed);
539
540 resolve(trimmed);
541
542 } else {
543 reject(err);
544 }
545 });
546 })
547}
548
549// gets a list of all oppenents from the league page
550// excludes the give team
551async function getOpponentsCurrentSeason(team) {
552 var cheerio = require('cheerio'),
553 request = require('request');
554
555 //get all season pages
556 var pages = await csgo99damage.getAllSeasons(teamID);
557 var team = await csgo99damage.getShorthand(teamID);
558 pages = getSeasonpages(pages);
559 var page = pages[1];
560
561 return new Promise(function (resolve, reject) {
562 request({
563 url: page,
564 headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0' }
565 }, function (err, res, html) {
566 if (!err) {
567 var $ = cheerio.load(html);
568
569 const result = $(".league_table tr").map((i, element) => ({
570 Name: $(element).find('td:nth-of-type(2)').text().trim(),
571 link: $(element).find('td:nth-of-type(2) a').attr('href')
572 })).get()
573
574 result.shift();
575 result.shift();
576 console.log(result);
577
578 //add all links from matches that have a score
579 var trimmed = [];
580 // match was finished
581 for (var index in result) {
582 if (result && result[index].hasOwnProperty('link') && result[index].hasOwnProperty('Name')) {
583 if (result[index]['Name'] != team) {
584
585 var temp = result[index]['link'];
586 console.log(temp);
587 temp = temp.substr(temp.lastIndexOf('/') + 1);
588 temp = temp.substr(0, temp.indexOf('-'));
589 console.log(temp);
590 trimmed.push(temp);
591 }
592
593 }
594 }
595
596 console.log(trimmed);
597
598 resolve(trimmed);
599
600 } else {
601 reject(err);
602 }
603 });
604 })
605}
606
607var csgo99damage = {
608 temp: {
609 lastUpdate: 0,
610 matches: {}
611 },
612 getAllSeasons:getAllSeasons,
613 getShorthand:getShorthand,
614 getMatchesThisSeason:getMatchesThisSeason,
615 getLongName:getLongName,
616 getOpponents:getOpponents,
617 printtest:printtest,
618 getPickBan:getPickBan,
619 getMatches:getMatches,
620 getMatch:getMatch,
621 upcomingMatches:upcomingMatches,
622 allThisSeasonMatches:allThisSeasonMatches,
623 getOpponentsCurrentSeason:getOpponentsCurrentSeason
624}
625
626module.exports = csgo99damage;