Files
epg/sites/i.mjh.nz/i.mjh.nz.config.js

83 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-05-03 16:51:39 +03:00
const dayjs = require('dayjs')
const axios = require('axios')
const parser = require('epg-parser')
const isBetween = require('dayjs/plugin/isBetween')
const customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(isBetween)
dayjs.extend(customParseFormat)
2022-11-20 17:37:29 +03:00
const API_ENDPOINT = 'https://raw.githubusercontent.com/matthuisman/i.mjh.nz/master'
2022-05-03 16:51:39 +03:00
module.exports = {
site: 'i.mjh.nz',
2023-01-10 12:40:01 +03:00
days: 2,
2022-05-03 19:29:20 +03:00
request: {
cache: {
2022-11-20 17:37:29 +03:00
ttl: 3 * 60 * 60 * 1000 // 3h
2022-10-28 19:00:25 +03:00
},
2022-11-20 17:37:29 +03:00
maxContentLength: 30 * 1024 * 1024 // 30Mb
2022-05-03 19:29:20 +03:00
},
2022-05-03 16:51:39 +03:00
url: function ({ channel }) {
2022-11-20 17:37:29 +03:00
const [path] = channel.site_id.split('#')
2022-05-03 16:51:39 +03:00
2022-11-20 17:37:29 +03:00
return `${API_ENDPOINT}/${path}.xml`
2022-05-03 16:51:39 +03:00
},
2022-05-03 19:29:20 +03:00
parser: function ({ content, channel, date, cached }) {
2022-11-21 17:24:14 +03:00
const items = parseItems(content, channel, date)
return items.map(item => {
return {
...item,
title: getTitle(item),
description: getDescription(item),
categories: getCategories(item)
}
})
2022-05-03 16:51:39 +03:00
},
2022-11-07 21:45:07 +03:00
async channels({ path, lang = 'en' }) {
2022-11-20 17:37:29 +03:00
let xml = await axios
.get(`${API_ENDPOINT}/${path}.xml`)
2022-05-03 16:51:39 +03:00
.then(r => r.data)
.catch(console.log)
2022-11-20 17:37:29 +03:00
let data = parser.parse(xml)
2022-05-03 16:51:39 +03:00
2022-11-20 17:37:29 +03:00
return data.channels.map(channel => {
return {
2022-05-03 16:51:39 +03:00
lang,
2022-11-20 17:37:29 +03:00
site_id: `${path}#${channel.id}`,
name: channel.name[0].value
}
})
2022-05-03 16:51:39 +03:00
}
}
2022-11-21 17:24:14 +03:00
function getTitle(item) {
return item.title.length ? item.title[0].value : null
}
function getDescription(item) {
return item.desc.length ? item.desc[0].value : null
}
function getCategories(item) {
return item.category.map(c => c.value)
}
2022-05-03 16:51:39 +03:00
function parseItems(content, channel, date) {
try {
const curr_day = date
const next_day = date.add(1, 'd')
const [_, site_id] = channel.site_id.split('#')
const data = parser.parse(content)
if (!data || !Array.isArray(data.programs)) return []
return data.programs.filter(
p =>
p.channel === site_id && dayjs(p.start, 'YYYYMMDDHHmmss ZZ').isBetween(curr_day, next_day)
)
} catch (error) {
return []
}
}