Created
November 20, 2018 15:03
-
-
Save leinonen/278f0508c87dec3e7fcfe1ec91484025 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const R = require('ramda') | |
const request = require('request') | |
const cheerio = require('cheerio') | |
const host = '' | |
const refreshInterval = 5000 | |
const sortByScore = R.sortWith([R.descend(R.prop('score'))]) | |
function getScores(data) { | |
const $ = cheerio.load(data) | |
let rawScores = $('#results').text().trim().split(' | ') | |
let scores = rawScores.map(s => { | |
const [name, score] = s.split(' - ') | |
return { name, score: parseInt(score, 10) } | |
}) | |
return sortByScore(scores) | |
} | |
function handleResult(err, res, body) { | |
if (!err && res.statusCode === 200) { | |
const scores = getScores(body) | |
const { name, score } = scores[0] | |
const { name: loserName, score: loserScore } = scores[1] | |
const diff = score - loserScore | |
console.log(`${name} is the leader with ${score} points, a whopping ${diff} more points than ${loserName} who only has ${loserScore} points`) | |
} | |
} | |
function vote() { | |
request.get(host, handleResult) | |
} | |
setInterval(vote, refreshInterval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment