Skip to content

Commit

Permalink
Make it possible to filter out teams that has no Liquipedia profile. …
Browse files Browse the repository at this point in the history
…Adding gitignore. Handle request errors that fail to initialize ssl
  • Loading branch information
buxxi committed Dec 30, 2019
1 parent 61fd3da commit 32dc9eb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
10 changes: 9 additions & 1 deletion MMM-Liquipedia-Dota2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Module.register("MMM-Liquipedia-Dota2",{
defaults: {
matchUpdateInterval : 60*60*1000, //Once every hour should be a good enough default
displayCount : 5,
requiredProfiles: 0,
sourceUrl : "https://liquipedia.net/dota2/api.php?action=parse&format=json&page=Liquipedia:Upcoming_and_ongoing_matches"
},

Expand Down Expand Up @@ -37,7 +38,7 @@ Module.register("MMM-Liquipedia-Dota2",{
return { matches : [] };
}
return {
matches : self.matches.matches.slice(0, self.config.displayCount).map(function(match) {
matches : self.matches.matches.filter(self.profileFilter(self.config.requiredProfiles)).slice(0, self.config.displayCount).map(function(match) {
return {
team1 : match.team1,
team2 : match.team2,
Expand Down Expand Up @@ -73,4 +74,11 @@ Module.register("MMM-Liquipedia-Dota2",{
return Math.floor(diff / 60) + " h";
}
},

profileFilter : function(requiredProfiles) {
return function(match) {
var profiles = (match.team1.hasProfile ? 1 : 0) + (match.team2.hasProfile ? 1 : 0);
return profiles >= requiredProfiles && match.team1.name && match.team2.name;
};
}
});
6 changes: 3 additions & 3 deletions MMM-Liquipedia-Dota2.njk
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{% for match in matches %}
<tr class="small">
<td>
<span class="bright">{{ match.team1 }}</span>
<span> vs </span>
<span class="bright">{{ match.team2 }}</span>
<span class="bright">{{ match.team1.name }}</span>
<span> vs </span>
<span class="bright">{{ match.team2.name }}</span>
</td>
<td class="align-right {{" bright" if match.starts == "live" else ""}}">{{ match.starts }}</td>
</tr>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
- ``matchUpdateInterval`` : How often it should fetch new matches in seconds, anything lower than 30 minutes is throttled since it could lead to an ip-ban, default is 60 minutes.
- ``displayCount`` : the amount of matches to display, default is 5
- ``sourceUrl``: The API-url to use, could possibly be changed to other games on Liquipedia too?
- ``requiredProfiles``: the amount of teams in the match that needs to have a profile page on Liquipedia, 0-2, default is 0
26 changes: 22 additions & 4 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const NodeHelper = require("node_helper");
const request = require("request");
const jsdom = require("jsdom").JSDOM;
const moment = require("moment");
const tls = require('tls');

const RATE_LIMIT_MILLISECONDS = 30000;

Expand Down Expand Up @@ -38,15 +39,19 @@ module.exports = NodeHelper.create({
gzip : true,
headers : {
"User-Agent" : "MagicMirror/MMM-Liquipedia-Dota2/1.0; (https://github.com/buxxi/MMM-Liquipedia-Dota2)"
}
},
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
self.sendSocketNotification("DOTA2_MATCHES", {
url: url,
data: self.parseMatches(JSON.parse(response.body).parse.text['*'])
});
} else {
self.sendSocketNotification("DOTA2_MATCHES_ERROR", { statusCode : response.statusCode, url : url });
if (response) {
self.sendSocketNotification("DOTA2_MATCHES_ERROR", { statusCode : response.statusCode, url : url });
} else {
self.sendSocketNotification("DOTA2_MATCHES_ERROR", { statusCode : error.toString(), url : url });
}
}
});
},
Expand All @@ -56,6 +61,9 @@ module.exports = NodeHelper.create({
var tables = dom.window.document.querySelectorAll("table");

function teamName(div) {
if (!div) {
return undefined;
}
var teamName = div.innerText;
var a = div.querySelector("a");
if (!a) {
Expand All @@ -66,6 +74,10 @@ module.exports = NodeHelper.create({

return teamName;
}

function hasProfile(div) {
return !div.querySelector("a.new");
}

var result = [];

Expand All @@ -75,8 +87,14 @@ module.exports = NodeHelper.create({
var tournament = table.querySelector(".match-countdown~div a").title;

result.push({
team1 : teamName(teams[0]),
team2 : teamName(teams[1]),
team1 : {
name: teamName(teams[0]),
hasProfile: hasProfile(teams[0])
},
team2 : {
name: teamName(teams[1]),
hasProfile: hasProfile(teams[1])
},
date : date.toISOString(),
tournament : tournament
});
Expand Down

0 comments on commit 32dc9eb

Please sign in to comment.