From 407dfa800369ce32bced4a79408c4e83eadd1e3a Mon Sep 17 00:00:00 2001 From: RevGear <95308545+RevGear@users.noreply.github.com> Date: Wed, 11 Jan 2023 17:57:24 +0000 Subject: [PATCH] Add guide for unifi.com.my --- sites/unifi.com.my/unifi.com.my.channels.xml | 72 ++++++++++++++++++++ sites/unifi.com.my/unifi.com.my.config.js | 61 +++++++++++++++++ sites/unifi.com.my/unifi.com.my.test.js | 52 ++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 sites/unifi.com.my/unifi.com.my.channels.xml create mode 100644 sites/unifi.com.my/unifi.com.my.config.js create mode 100644 sites/unifi.com.my/unifi.com.my.test.js diff --git a/sites/unifi.com.my/unifi.com.my.channels.xml b/sites/unifi.com.my/unifi.com.my.channels.xml new file mode 100644 index 00000000..0fc81450 --- /dev/null +++ b/sites/unifi.com.my/unifi.com.my.channels.xml @@ -0,0 +1,72 @@ + + + + 8TV + Al Jazeera + Animax + arirang TV + AXN + BBC Earth HD + BBC Lifestyle HD + BBC World News HD + beIN SPORTS + beIN SPORTS 3 + beIN SPORTS 4 + Bernama TV + Boomerang HD + CBeebies HD + CCM + CCTV-4 HD + Celestial Movies + CGTN HD + CinemaWorld HD + CNA + Colors Cineplex + Colors Tamil HD + DidikTV KPM + Dreamworks HD + Dunia Sinema HD + DW + Euronews + France 24 + GEM + HITS + HITS Movies + Inspirasi + K-Plus + LAKU mall + Love Nature HD + Luxe.TV HD + MTV Live + NHK WORLD-JAPAN + Nick Jr. + Now Jelli HD + Paramount Network + Parlimen Malaysia + Pesona HD + ROCK Action + ROCK Entertainment + Salam HD + Sensasi + SETI + SIAR + Sony YAY! + SPOTV + SPOTV2 + TechStorm + Travel Channel HD + TV AlHijrah HD + TV1 + TV2 + TV3 + TV9 + tvN HD + tvN Movies HD + unifi Sports 1 + unifi Sports 2 + unifi Sports 3 + unifi Sports 4 + Warner TV HD + Wion + + \ No newline at end of file diff --git a/sites/unifi.com.my/unifi.com.my.config.js b/sites/unifi.com.my/unifi.com.my.config.js new file mode 100644 index 00000000..3c83a904 --- /dev/null +++ b/sites/unifi.com.my/unifi.com.my.config.js @@ -0,0 +1,61 @@ +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: 'unifi.com.my', + days: 2, + url: `https://unifi.com.my/tv/api/tv`, + request: { + cache: { + ttl: 60 * 60 * 1000 // 1 hour + }, + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' + }, + data({ date }) { + const params = new URLSearchParams() + params.append('date', date.format('YYYY-MM-DD')) + return params + } + }, + parser({ content, channel, date }) { + let programs = [] + const items = parseItems(content, channel) + items.forEach(item => { + const start = parseStart(item, date) + const stop = start.add(item.minute, 'minute') + programs.push({ + title: item.name, + start, + stop + }) + }) + return programs + } +} + +function parseItems(content, channel) { + try { + const data = JSON.parse(content) + if (!data) return [] + if (!Array.isArray(data)) return [] + + const channelData = data.find(i => i.id == channel.site_id) + return channelData.items && Array.isArray(channelData.items) ? channelData.items : [] + } catch (err) { + return [] + } +} + +function parseStart(item, date) { + const time = `${date.format('YYYY-MM-DD')} ${item.start_time}` + return dayjs.tz(time, 'YYYY-MM-DD H:mma', 'Asia/Kuala_Lumpur') +} + diff --git a/sites/unifi.com.my/unifi.com.my.test.js b/sites/unifi.com.my/unifi.com.my.test.js new file mode 100644 index 00000000..4fd333ae --- /dev/null +++ b/sites/unifi.com.my/unifi.com.my.test.js @@ -0,0 +1,52 @@ +// npx epg-grabber --config=sites/unifi.com.my/unifi.com.my.config.js --channels=sites/unifi.com.my/unifi.com.my.channels.xml --output=guide.xml --days=2 + +const { parser, url, request } = require('./unifi.com.my.config.js') +const dayjs = require('dayjs') +const utc = require('dayjs/plugin/utc') +const customParseFormat = require('dayjs/plugin/customParseFormat') +dayjs.extend(customParseFormat) +dayjs.extend(utc) + +const date = dayjs.utc('2023-01-09', 'YYYY-MM-DD').startOf('d') +const channel = { + site_id: '51882833', + xmltv_id: 'AXNMalaysia.my' +} + +it('can generate valid url', () => { + expect(url).toBe(`https://unifi.com.my/tv/api/tv`) +}) + +it('can generate valid request method', () => { + expect(request.method).toBe('POST') +}) + +it('can generate valid request headers', () => { + expect(request.headers).toMatchObject({ + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' + }) +}) + +it('can parse response', () => { + const content = `[{"id":"51882833","name":"AXN","logo":"https://playtv.unifi.com.my:7047/CPS/images/universal/film/logo/202109/20210927/2021092701574706798y.png","items":[{"name":"Blue Bloods (Season 11)","interval":"one","minute":55,"start_time":"4:20am","end_time":"5:15am"}]}]` + + const result = parser({ content, channel, date }).map(p => { + p.start = p.start.toJSON() + p.stop = p.stop.toJSON() + return p + }) + + expect(result).toMatchObject([ + { + title: 'Blue Bloods (Season 11)', + start: '2023-01-08T20:20:00.000Z', + stop: '2023-01-08T21:15:00.000Z' + } + ]) + }) + + it('can handle empty guide', () => { + const content = `[{"id":"51882833","name":"AXN","logo":"https://playtv.unifi.com.my:7047/CPS/images/universal/film/logo/202109/20210927/2021092701574706798y.png","items":[]}]` + const result = parser({ content, channel }) + expect(result).toMatchObject([]) + }) \ No newline at end of file