diff --git a/sites/beinsports.com/beinsports.com.config.js b/sites/beinsports.com/beinsports.com.config.js new file mode 100644 index 00000000..ebbdd827 --- /dev/null +++ b/sites/beinsports.com/beinsports.com.config.js @@ -0,0 +1,96 @@ +const cheerio = require('cheerio') +const dayjs = require('dayjs') +const utc = require('dayjs/plugin/utc') +const customParseFormat = require('dayjs/plugin/customParseFormat') + +dayjs.extend(utc) +dayjs.extend(customParseFormat) + +module.exports = { + request: { + timeout: 30000 + }, + site: 'beinsports.com', + url: function ({ date }) { + return `https://epg.beinsports.com/utctime.php?mins=00&serviceidentity=beinsports.com&cdate=${date.format( + 'YYYY-MM-DD' + )}` + }, + logo({ content, channel }) { + const $ = cheerio.load(content) + + return $(`#channels_${channel.site_id} .channel img`).attr('src') + }, + parser: function ({ content, channel, date }) { + let offset = -1 + let programs = [] + const items = parseItems(content, channel) + items.forEach(item => { + const title = parseTitle(item) + const category = parseCategory(item) + let start = parseStart(item, date) + if (!start) return + if (start.hour() > 18 && offset === -1) { + start = start.subtract(1, 'd') + } else if (start.hour() < 12 && offset === -1) { + offset++ + } + let stop = parseStop(item, date) + if (!stop) return + if (stop.hour() > 18 && offset === -1) { + stop = stop.subtract(1, 'd') + } else if (stop.hour() < 12 && offset === -1) { + offset++ + } + + programs.push({ + title, + category, + start: start.toString(), + stop: stop.toString() + }) + }) + + return programs + } +} + +function parseTitle(item) { + const $ = cheerio.load(item) + + return $('.title').text() +} + +function parseCategory(item) { + const $ = cheerio.load(item) + + return $('.format').text() +} + +function parseStart(item, date) { + const $ = cheerio.load(item) + let [_, time] = $('.time') + .text() + .match(/^(\d{2}:\d{2})/) || [null, null] + if (!time) return null + time = `${date.format('YYYY-MM-DD')} ${time}` + + return dayjs.utc(time, 'YYYY-MM-DD HH:mm') +} + +function parseStop(item, date) { + const $ = cheerio.load(item) + let [_, time] = $('.time') + .text() + .match(/(\d{2}:\d{2})$/) || [null, null] + if (!time) return null + time = `${date.format('YYYY-MM-DD')} ${time}` + + return dayjs.utc(time, 'YYYY-MM-DD HH:mm') +} + +function parseItems(content, channel) { + const $ = cheerio.load(content) + + return $(`#channels_${channel.site_id} .slider > ul:first-child > li`).toArray() +} diff --git a/sites/beinsports.com/beinsports.com.test.js b/sites/beinsports.com/beinsports.com.test.js new file mode 100644 index 00000000..97be8e6d --- /dev/null +++ b/sites/beinsports.com/beinsports.com.test.js @@ -0,0 +1,93 @@ +// npx epg-grabber --config=sites/beinsports.com/beinsports.com.config.js --channels=sites/beinsports.com/beinsports.com_qa.channels.xml --days=2 --output=.gh-pages/guides/qa/beinsports.com.epg.xml + +const { parser, url, logo } = require('./beinsports.com.config.js') +const dayjs = require('dayjs') +const utc = require('dayjs/plugin/utc') +const customParseFormat = require('dayjs/plugin/customParseFormat') +dayjs.extend(customParseFormat) +dayjs.extend(utc) + +const date = dayjs.utc('2021-10-24', 'YYYY-MM-DD').startOf('d') +const channel = { site_id: '1', xmltv_id: 'BeInSports.qa' } +const content = ` +