From 4ae0c38dafcc97107ff3cce1011b3caad732211c Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sat, 5 Mar 2022 23:55:00 +0300 Subject: [PATCH] Create i24news.tv.config.js --- sites/i24news.tv/i24news.tv.config.js | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 sites/i24news.tv/i24news.tv.config.js diff --git a/sites/i24news.tv/i24news.tv.config.js b/sites/i24news.tv/i24news.tv.config.js new file mode 100644 index 00000000..7141e34d --- /dev/null +++ b/sites/i24news.tv/i24news.tv.config.js @@ -0,0 +1,66 @@ +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 = { + site: 'i24news.tv', + url: function ({ channel }) { + const [lang] = channel.site_id.split('#') + + return `https://api.i24news.tv/v2/${lang}/schedules/world` + }, + parser: function ({ content, date }) { + let programs = [] + const items = parseItems(content, date) + items.forEach(item => { + if (!item.show) return + programs.push({ + title: item.show.title, + description: item.show.body, + icon: parseIcon(item), + start: parseStart(item, date), + stop: parseStop(item, date) + }) + }) + + return programs + } +} + +function parseIcon(item) { + return item.show.image ? item.show.image.href : null +} + +function parseStart(item, date) { + if (!item.startHour) return null + + return dayjs.tz( + `${date.format('YYYY-MM-DD')} ${item.startHour}`, + 'YYYY-MM-DD HH:mm', + 'Asia/Jerusalem' + ) +} + +function parseStop(item, date) { + if (!item.endHour) return null + + return dayjs.tz( + `${date.format('YYYY-MM-DD')} ${item.endHour}`, + 'YYYY-MM-DD HH:mm', + 'Asia/Jerusalem' + ) +} + +function parseItems(content, date) { + const data = JSON.parse(content) + if (!Array.isArray(data)) return [] + let day = date.day() - 1 + day = day < 0 ? 6 : day + + return data.filter(item => item.day === day) +}