UNPKG

epl-fixtures

Version:

Fixtures for Premier League clubs

55 lines (48 loc) 1.62 kB
'use strict'; const cheerio = require('cheerio'); const trim = require('./trim'); const moment = require('moment'); module.exports = function (html, callback) { const matches = []; const $ = cheerio.load(html); $('table.contentTable').each(function (i, element) { /** * Match dates */ let date; let competition; $(this).find('tr:first-child').each(function (i, element) { const tableTitleAsArray = trim($(this).text()).split(' '); /** * EPL matches don't have EPL as clear text, * so we add this manually. * * All other matches have competition written in clear text, * we combine slices of tableTitle to get date and competition */ if (tableTitleAsArray.length === 4) { date = tableTitleAsArray.join(' '); competition = 'Barclays Premier League' } else { date = tableTitleAsArray.slice(0, 3).join(' '); competition = tableTitleAsArray.slice(4).join(' '); } }); /** * Games */ $(this).children().each(function (i, element) { /** * We need to ignore the table title */ if ($(this).find('td.clubs').length) { const time = trim($(this).find('td.time').text()); const clubs = trim($(this).find('td.clubs').text()).split(' v '); const location = trim($(this).find('td.location').text()); const userLocaleTime = moment(time + ' UTC +01:00', 'HH:mm ZZ').format('HH:mm'); matches.push({time, clubs, location, date, competition, userLocaleTime}); } }); }); callback(false, matches); }