Files
epg/sites/rev.bs/rev.bs.config.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-11-21 22:20:18 +03:00
const _ = require('lodash')
const axios = require('axios')
2021-10-01 21:37:16 +03:00
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
dayjs.extend(utc)
dayjs.extend(timezone)
module.exports = {
site: 'rev.bs',
2023-01-10 12:40:01 +03:00
days: 2,
2021-10-01 21:37:16 +03:00
url: function ({ date }) {
2021-11-21 22:20:18 +03:00
return `https://www.rev.bs/wp-content/uploads/tv-guide/${date.format('YYYY-MM-DD')}_0.json`
2021-10-01 21:37:16 +03:00
},
parser: async function ({ content, channel, date }) {
const programs = []
const items0 = parseItems(content, channel)
2021-11-21 22:20:18 +03:00
if (!items0.length) return programs
2021-10-01 21:37:16 +03:00
const items1 = parseItems(await loadNextItems(date, 1), channel)
const items2 = parseItems(await loadNextItems(date, 2), channel)
const items3 = parseItems(await loadNextItems(date, 3), channel)
const items = _.unionBy(items0, items1, items2, items3, 'sid')
items.forEach(item => {
const start = parseStart(item, date)
const stop = start.add(item.duration, 'm')
programs.push({
title: item.title,
start,
stop
})
})
return programs
}
}
async function loadNextItems(date, index) {
2021-11-21 22:20:18 +03:00
const url = `https://www.rev.bs/wp-content/uploads/tv-guide/${date.format(
'YYYY-MM-DD'
)}_${index}.json`
2021-10-01 21:37:16 +03:00
return axios
.get(url, {
responseType: 'arraybuffer'
})
.then(res => res.data.toString())
2021-11-21 22:20:18 +03:00
.catch(console.log)
2021-10-01 21:37:16 +03:00
}
2021-11-21 22:20:18 +03:00
function parseStart(item, date) {
2021-10-01 21:37:16 +03:00
const shift = parseInt(item.s)
2021-11-21 22:20:18 +03:00
return dayjs.tz(date.add(shift, 'm').toString(), 'America/New_York')
2021-10-01 21:37:16 +03:00
}
function parseItems(content, channel) {
2021-11-21 22:20:18 +03:00
let data
try {
data = JSON.parse(content)
2023-10-02 06:35:33 +03:00
} catch (error) {
return []
}
2021-11-21 22:20:18 +03:00
if (!data || data.status !== 'OK') return []
2021-10-01 21:37:16 +03:00
2021-11-21 22:20:18 +03:00
return data.data.schedule[channel.site_id] || []
2021-10-01 21:37:16 +03:00
}