-
Notifications
You must be signed in to change notification settings - Fork 0
/
espn.js
51 lines (45 loc) · 1.48 KB
/
espn.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
const cheerio = require('cheerio');
const LOG = require('logger');
function getURL(type, year) {
return `https://www.espn.com/olympics/${type}/${year}/medals`;
}
async function getMedalData(type, year, limit) {
var url = getURL(type, year);
var response = await fetch(url);
if (!response.ok) {
LOG.log("Error fetching data from ESPN");
}
var body = await response.text();
const $ = cheerio.load(body);
var medalContainer = $('.medal-container');
var medalTable = medalContainer.next();
if (medalTable[0].name !== 'table') {
return {
header: 'No Medals Data',
rows: 'No Medals data available yet'
}
}
var header = medalTable.find('caption').first().text();
var rows = medalTable.find('tbody');
let countries = [];
rows.find('tr').each((index, row) => {
if(index >= limit){
return false;
}
let data = {
abbreviation: $(row).find('.country_name--abbrev').first().text(),
country: $(row).find('.country_name--long').first().text(),
img: $(row).find('.team-logo').attr('src'),
gold: $(row).find('td').eq(1).text(),
silver: $(row).find('td').eq(2).text(),
bronze: $(row).find('td').eq(3).text(),
total: $(row).find('td').eq(4).text(),
}
countries.push(data);
});
return {
header: header,
rows: countries
}
}
module.exports = { getMedalData };