This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
forked from andrewda/hltv-live-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLive.js
132 lines (113 loc) · 5.5 KB
/
Live.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
var request = require("request");
var parseString = require("xml2js").parseString;
var unixTime = require("unix-time");
var cheerio = require("cheerio");
var EE = require("events").EventEmitter;
var inherits = require("util").inherits;
var liveMatchid = [];
var first = true;
var _this;
function Live(options) {
_this = this;
if (options.polling) {
this.pollTime = options.polling;
} else {
this.pollTime = 30000;
}
this.pollGames();
setInterval(this.pollGames, this.pollTime);
}
inherits(Live, EE);
Live.prototype.pollGames = function() {
request("http://www.hltv.org/hltv.rss.php?pri=15", function(error, response, body) {
if (!error && response.statusCode === 200) {
parseString(body, function(err, result) {
if (err) {
throw err;
} else {
if (!first) {
result.rss.channel[0].item.forEach(function(game) {
if (unixTime(game.pubDate[0]) - 180 <= unixTime(new Date())) {
request(game.link[0], function(error, response, body) {
if (!error && response.statusCode === 200) {
$ = cheerio.load(body);
var html = $(".hotmatchbox").text();
var html2 = $(".text-center", ".hotmatchbox").text().trim().split(" ").clean("");
var midpatt = new RegExp("matchid = ([0-9]*)");
var lidpatt = new RegExp("listid = ([0-9]*)");
var boxpatt = new RegExp("Best of ([0-9]*)");
if (/matchid = [0-9]*/.test(html) && html2.length >= 10) {
var matchid = midpatt.exec(html)[1];
var listid = lidpatt.exec(html)[1];
var bestof = boxpatt.exec(html)[1];
if (liveMatchid.indexOf(matchid) === -1) {
liveMatchid.push(matchid);
emit("newGame", {
teams: [game.title[0].split(" vs ")[0], game.title[0].split(" vs ")[1]],
matchid: matchid,
listid: listid,
bestof: bestof,
time: getTime(unixTime(game.pubDate[0])),
players: [
[html2[0], html2[1], html2[2], html2[3], html2[4]],
[html2[5], html2[6], html2[7], html2[8], html2[9]]
],
start_time: unixTime(game.pubDate[0]),
time_since_start: unixTime(new Date()) - unixTime(game.pubDate[0])
});
}
}
}
});
}
});
} else {
result.rss.channel[0].item.forEach(function(game) {
if (unixTime(game.pubDate[0]) <= unixTime(new Date())) {
request(game.link[0], function(error, response, body) {
if (!error && response.statusCode === 200) {
$ = cheerio.load(body);
var html = $(".hotmatchbox").text();
var html2 = $(".text-center", ".hotmatchbox").text().trim().split(" ").clean("");
var midpatt = new RegExp("matchid = ([0-9]*)");
if (/matchid = [0-9]*/.test(html) && html2.length >= 10) {
var matchid = midpatt.exec(html)[1];
if (liveMatchid.indexOf(matchid) === -1) {
liveMatchid.push(matchid);
}
}
}
});
}
});
first = false;
}
}
});
}
});
};
function emit(event, message) {
_this.emit(event, message);
}
function getTime(UNIX_timestamp) {
var a = new Date(UNIX_timestamp * 1000);
return {
year: a.getFullYear(),
month: a.getMonth() + 1,
date: a.getDate(),
hour: a.getHours(),
min: a.getMinutes(),
sec: a.getSeconds()
};
}
Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) {
if (this[i] === deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};
module.exports = Live;