diff --git a/sites/beinsports.com/__data__/no-content.html b/sites/beinsports.com/__data__/no-content.html
new file mode 100644
index 00000000..309b2380
--- /dev/null
+++ b/sites/beinsports.com/__data__/no-content.html
@@ -0,0 +1,28 @@
+
+
+
diff --git a/sites/beinsports.com/beinsports.com.config.js b/sites/beinsports.com/beinsports.com.config.js
index ee7189e0..d7d269ce 100644
--- a/sites/beinsports.com/beinsports.com.config.js
+++ b/sites/beinsports.com/beinsports.com.config.js
@@ -1,9 +1,11 @@
const cheerio = require('cheerio')
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
+const timezone = require('dayjs/plugin/timezone')
const customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(utc)
+dayjs.extend(timezone)
dayjs.extend(customParseFormat)
module.exports = {
@@ -14,32 +16,32 @@ module.exports = {
)}`
},
parser: function ({ content, channel, date }) {
- let offset = -1
let programs = []
const items = parseItems(content, channel)
+ date = date.subtract(1, 'd')
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++
+ const $item = cheerio.load(item)
+ const title = parseTitle($item)
+ if (!title) return
+ const category = parseCategory($item)
+ const prev = programs[programs.length - 1]
+ let start = parseStart($item, date)
+ if (prev) {
+ if (start.isBefore(prev.start)) {
+ start = start.add(1, 'd')
+ date = date.add(1, 'd')
+ }
+ prev.stop = start
}
- 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++
+ let stop = parseStop($item, start)
+ if (stop.isBefore(start)) {
+ stop = stop.add(1, 'd')
}
-
programs.push({
title,
category,
- start: start.toString(),
- stop: stop.toString()
+ start,
+ stop
})
})
@@ -47,42 +49,37 @@ module.exports = {
}
}
-function parseTitle(item) {
- const $ = cheerio.load(item)
-
- return $('.title').text()
+function parseTitle($item) {
+ return $item('.title').text()
}
-function parseCategory(item) {
- const $ = cheerio.load(item)
-
- return $('.format').text()
+function parseCategory($item) {
+ return $item('.format').text()
}
-function parseStart(item, date) {
- const $ = cheerio.load(item)
- let [_, time] = $('.time')
+function parseStart($item, date) {
+ let [_, time] = $item('.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')
+ return dayjs.tz(time, 'YYYY-MM-DD HH:mm', 'Asia/Qatar')
}
-function parseStop(item, date) {
- const $ = cheerio.load(item)
- let [_, time] = $('.time')
+function parseStop($item, date) {
+ let [_, time] = $item('.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')
+ return dayjs.tz(time, 'YYYY-MM-DD HH:mm', 'Asia/Qatar')
}
function parseItems(content, channel) {
+ const [_, channelId] = channel.site_id.split('#')
const $ = cheerio.load(content)
- return $(`#channels_${channel.site_id} .slider > ul:first-child > li`).toArray()
+ return $(`#channels_${channelId} .slider > ul:first-child > li`).toArray()
}
diff --git a/sites/beinsports.com/beinsports.com.test.js b/sites/beinsports.com/beinsports.com.test.js
index 50daaa9c..5596e5bd 100644
--- a/sites/beinsports.com/beinsports.com.test.js
+++ b/sites/beinsports.com/beinsports.com.test.js
@@ -1,86 +1,46 @@
-// npx epg-grabber --config=sites/beinsports.com/beinsports.com.config.js --channels=sites/beinsports.com/beinsports.com_qa.channels.xml --output=guide.xml --timeout=30000 --days=2
+// npx epg-grabber --config=sites/beinsports.com/beinsports.com.config.js --channels=sites/beinsports.com/beinsports.com_qa-en.channels.xml --output=guide.xml --timeout=30000 --days=2
const { parser, url } = require('./beinsports.com.config.js')
+const fs = require('fs')
+const path = require('path')
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 = `
-
-
-
-
-
-

-
-
-
-
-
-
-
-
-
Red Bull Car Park Drift 2021
-
MotorSports
-
-
-
-
-
-
-
-

-
-
-
-
-
-
-`
+const date = dayjs.utc('2022-05-08', 'YYYY-MM-DD').startOf('d')
+const channel = { site_id: '#1', xmltv_id: 'BeINSports.qa' }
it('can generate valid url', () => {
const result = url({ date })
expect(result).toBe(
- 'https://epg.beinsports.com/utctime.php?mins=00&serviceidentity=beinsports.com&cdate=2021-10-24'
+ 'https://epg.beinsports.com/utctime.php?mins=00&serviceidentity=beinsports.com&cdate=2022-05-08'
)
})
it('can parse response', () => {
- const result = parser({ date, channel, content })
- expect(result).toMatchObject([
- {
- start: 'Sat, 23 Oct 2021 21:45:00 GMT',
- stop: 'Sun, 24 Oct 2021 00:40:00 GMT',
- title: 'Red Bull Car Park Drift 2021',
- category: 'MotorSports'
- }
- ])
+ const content = fs.readFileSync(path.resolve('sites/beinsports.com/__data__/content.html'))
+ const results = parser({ date, channel, content }).map(p => {
+ p.start = p.start.toJSON()
+ p.stop = p.stop.toJSON()
+ return p
+ })
+
+ expect(results[0]).toMatchObject({
+ start: '2022-05-07T19:45:00.000Z',
+ stop: '2022-05-07T21:30:00.000Z',
+ title: 'Al Arabi vs Al Khor - Qatar Stars League 2021/2022',
+ category: 'Qatar Stars League'
+ })
+})
+
+it('can handle empty guide', () => {
+ const noContent = fs.readFileSync(path.resolve('sites/beinsports.com/__data__/no-content.html'))
+ const result = parser({
+ date,
+ channel,
+ content: noContent
+ })
+ expect(result).toMatchObject([])
})
diff --git a/sites/beinsports.com/beinsports.com_qa-en.channels.xml b/sites/beinsports.com/beinsports.com_qa-en.channels.xml
new file mode 100644
index 00000000..b05a5ec6
--- /dev/null
+++ b/sites/beinsports.com/beinsports.com_qa-en.channels.xml
@@ -0,0 +1,24 @@
+
+
+
+ BeIn Sports
+ BeIn Sports News
+ BeIn Sports 2
+ BeIn Sports 3
+ BeIn Sports 4
+ BeIn Sports 5
+ BeIn Sports 6
+ BeIn Sports 7
+ BeIn Sports Premium 3
+ BeIn Sports Xtra 1
+ BeIn Sports Xtra 2
+ BeIn 4K
+ BeIn NBA
+ BeIn Sports English 1
+ BeIn Sports English 2
+ BeIn Sports English 3
+ BeIn Sports French 1
+ BeIn Sports French 2
+ BeIn Sports French 3
+
+
\ No newline at end of file
diff --git a/sites/beinsports.com/beinsports.com_qa.channels.xml b/sites/beinsports.com/beinsports.com_qa.channels.xml
deleted file mode 100644
index 783fb749..00000000
--- a/sites/beinsports.com/beinsports.com_qa.channels.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-
-
-
- BeIn Sports
- BeIn Sports News
- BeIn Sports 1
- BeIn Sports 2
- BeIn Sports 3
- BeIn Sports 4
- BeIn Sports 5
- BeIn Sports 6
- BeIn Sports 7
- BeIn Sports Premium 1
- BeIn Sports Premium 2
- BeIn Sports Premium 3
- BeIn Sports Xtra 1
- BeIn Sports Xtra 2
- BeIn 4K
- BeIn NBA
- BeIn Sports English 1
- BeIn Sports English 2
- BeIn Sports English 3
- BeIn Sports French 1
- BeIn Sports French 2
- BeIn Sports French 3
-
-
\ No newline at end of file