From fd4f044aa6e49fb68043379a4057dfbae60df229 Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Sun, 9 Jul 2023 19:37:31 +0300 Subject: [PATCH] Create nhkworldpremium.com.config.js --- .../nhkworldpremium.com.config.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 sites/nhkworldpremium.com/nhkworldpremium.com.config.js diff --git a/sites/nhkworldpremium.com/nhkworldpremium.com.config.js b/sites/nhkworldpremium.com/nhkworldpremium.com.config.js new file mode 100644 index 00000000..7d64a51d --- /dev/null +++ b/sites/nhkworldpremium.com/nhkworldpremium.com.config.js @@ -0,0 +1,56 @@ +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: 'nhkworldpremium.com', + days: 7, + request: { + cache: { + ttl: 60 * 60 * 1000 // 1 hour + } + }, + url({ channel }) { + return `https://nhkworldpremium.com/backend/api/v1/front/episodes?lang=${channel.lang}` + }, + parser({ content, date }) { + let programs = [] + const items = parseItems(content, date) + items.forEach(item => { + const start = dayjs.tz(item.schedule, 'Asia/Seoul') + const duration = parseDuration(item) + const stop = start.add(duration, 's') + programs.push({ + title: item.programTitle, + sub_title: item.episodeTitle, + start, + stop + }) + }) + + return programs + } +} + +function parseDuration(item) { + const [h, m, s] = item.period.split(':') + + if (!h || !m || !s) return 0 + + return parseInt(h) * 3600 + parseInt(m) * 60 + parseInt(s) +} + +function parseItems(content, date) { + try { + const data = JSON.parse(content) + + if (!data || !data.item || !Array.isArray(data.item.episodes)) return [] + + return data.item.episodes.filter(ep => ep.schedule.startsWith(date.format('YYYY-MM-DD'))) + } catch (err) { + return [] + } +}