Fix firstmedia.com schedule mislead.

The retrieved schedules from firstmedia.com api indeed confusing. Taken for example
the following snippet:

```json
{
  channelNo: '245',
  title: 'News Bulletin',
  date: '2023-11-07 17:00:00',
  startTime: '2023-11-07 17:00:00',
  endTime: '2023-11-07 17:30:00',
  description: 'News Bulletin',
  long_description: 'Hourly update of international news with an emphasis on the Arab world.'
}
```

Neither `startTime` nor `endTime` is an actual time but an offset from `date`.
If its an actual time then it would overlap with each others.

The workaround is to calculate the start and stop time offset, sort the schedules
based on those offset, and last skip overlapped schedules.

Signed-off-by: Toha <tohenk@yahoo.com>
This commit is contained in:
Toha 2023-11-07 14:40:01 +07:00
parent 960b057611
commit 6096bee441
No known key found for this signature in database
GPG key ID: 2D7AA6389D44DCAB

View file

@ -7,24 +7,36 @@ dayjs.extend(utc)
module.exports = {
site: 'firstmedia.com',
days: 1,
url: function ({ channel, date }) {
days: 2,
url({ channel, date }) {
return `https://api.firstmedia.com/api/content/tv-guide/list?date=${date.format('DD/MM/YYYY')}&channel=${
channel.site_id
}&startTime=0&endTime=24`
},
parser: function ({ content, channel }) {
parser({ content, channel, date }) {
if (!content || !channel) return []
let programs = []
const programs = []
const items = parseItems(content, channel.site_id)
items.forEach(item => {
programs.push({
title: parseTitle(item),
description: parseDescription(item),
start: parseStart(item).toISOString(),
stop: parseStop(item).toISOString()
.map(item => {
item.start = toDelta(item.date, item.startTime)
item.stop = toDelta(item.date, item.endTime)
return item
})
.sort((a, b) => a.start - b.start)
const dt = date.tz('Asia/Jakarta').startOf('d')
let lastStop
items.forEach(item => {
if (lastStop === undefined || item.start >= lastStop) {
lastStop = item.stop
programs.push({
title: parseTitle(item),
description: parseDescription(item),
start: asDate(parseStart({ item, date: dt })),
stop: asDate(parseStop({ item, date: dt }))
})
}
})
return programs
@ -66,10 +78,22 @@ function parseDescription(item) {
return item.long_description
}
function parseStart(item) {
return dayjs.tz(item.startTime, 'YYYY-MM-DD HH:mm:ss', 'Asia/Jakarta')
function parseStart({ item, date }) {
return date.add(item.start, 'ms')
}
function parseStop(item) {
return dayjs.tz(item.endTime, 'YYYY-MM-DD HH:mm:ss', 'Asia/Jakarta')
function parseStop({ item, date }) {
return date.add(item.stop, 'ms')
}
function toDelta(from, to) {
return toDate(to).diff(toDate(from), 'milliseconds')
}
function toDate(date) {
return dayjs(date, 'YYYY-MM-DD HH:mm:ss')
}
function asDate(date) {
return date.toISOString()
}