This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
top_players.js
118 lines (101 loc) · 2.98 KB
/
top_players.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
const cheerio = require('cheerio');
const http = require('https');
const testing = false;
const SOCCER_ETHIOPIA_TOP_PLAYERS_URL = "https://www.soccerethiopia.net/ethpl-2012";
/**
* A function to process the data from the website. The returned array of object will be similar to
* [{
* "player" : "ሙጂብ ቃሲም",
* "team" : "ፋሲል ከነማ",
* "goals" : "9",
* "place" : "1"
* }]
* @param response from the website
* @return {Array} - of objects containing detail about the top players
*/
function getTopPlayersData(response) {
let returnable = []
let $ = cheerio.load(response);
let tables = $("table")
tables.each((i, e) => {
let strongTags = $('strong' , e)
let thisIsTheOne = false;
strongTags.each((sI , sE) => {
if($(sE).text() === 'ጎል አስቆጣሪዎች') {
thisIsTheOne = true;
}
})
// now get the contents and work with them
if (thisIsTheOne) {
let trs = $('tr' , e)
let place = 1;
trs.each((tI , tE) => {
let tds = $('td' , tE)
if (tds.length == 3) {
// 1st is the name of the player
// 2nd is the club
// 3rd is the goals
if ($(tds.get(0)).text() == 'ተጫዋች') return;
let playerItem = {
player: $(tds.get(0)).text(),
team: $(tds.get(1)).text(),
goals: $(tds.get(2)).text(),
place: place++
}
returnable.push(playerItem)
}
})
}
})
return returnable
}
/**
* if (testing) -> read from offline file
* @return {string}
* @api Public
*/
function readTopPlayersFromFile () {
const fx = require('fs');
try {
return fx.readFileSync('./ethpl_2012.html', 'utf-8');
} catch (e) {
console.error("file_" , e);
return ""
}
}
/**
* if (!testing) -> read data from website
* @return {Promise<String>}
*/
async function readTopPlayersFromWeb () {
return new Promise(resolve => http.get(SOCCER_ETHIOPIA_TOP_PLAYERS_URL, res => {
let data = "";
res.on("error", err => {
resolve(err.reason);
});
res.on('data', d => {
data += d;
});
res.on("end" , () => {
resolve(data);
})
})).catch(err => console.log(err));
}
/**
* Entry point to the api
* @return {Array}
*/
exports.getTopPlayersList = async function() {
if (testing) {
return new Promise(resolve => resolve(getTopPlayersData(readTopPlayersFromFile())));
} else {
return readTopPlayersFromWeb().then(data => getTopPlayersData(data));
}
}
/**
* Entry point to the api
* @return {String} json
*/
exports.getTopPlayersListJSON = async function() {
return JSON.stringify(await this.getTopPlayersList())
}