From 4a58f2b91f38033e6440e846da3c1078989dc9c4 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sun, 13 Nov 2022 10:52:34 +0300 Subject: [PATCH 1/5] Create __data__/ --- sites/programme.tvb.com/__data__/content.html | 3 +++ sites/programme.tvb.com/__data__/no-content.html | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 sites/programme.tvb.com/__data__/content.html create mode 100644 sites/programme.tvb.com/__data__/no-content.html diff --git a/sites/programme.tvb.com/__data__/content.html b/sites/programme.tvb.com/__data__/content.html new file mode 100644 index 00000000..14d317fb --- /dev/null +++ b/sites/programme.tvb.com/__data__/content.html @@ -0,0 +1,3 @@ + + diff --git a/sites/programme.tvb.com/__data__/no-content.html b/sites/programme.tvb.com/__data__/no-content.html new file mode 100644 index 00000000..6c648b03 --- /dev/null +++ b/sites/programme.tvb.com/__data__/no-content.html @@ -0,0 +1,3 @@ + + From d1882d123811631a3f7084134ad656a91034b5f2 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sun, 13 Nov 2022 10:52:40 +0300 Subject: [PATCH 2/5] Create programme.tvb.com.test.js --- .../programme.tvb.com.test.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 sites/programme.tvb.com/programme.tvb.com.test.js diff --git a/sites/programme.tvb.com/programme.tvb.com.test.js b/sites/programme.tvb.com/programme.tvb.com.test.js new file mode 100644 index 00000000..d2523470 --- /dev/null +++ b/sites/programme.tvb.com/programme.tvb.com.test.js @@ -0,0 +1,47 @@ +// npx epg-grabber --config=sites/programme.tvb.com/programme.tvb.com.config.js --channels=sites/programme.tvb.com/programme.tvb.com_hk.channels.xml --output=guide.xml --days=2 + +const { parser, url } = require('./programme.tvb.com.config.js') +const fs = require('fs') +const path = require('path') +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('2022-11-15', 'YYYY-MM-DD').startOf('d') +const channel = { + site_id: 'B', + xmltv_id: 'J2.hk' +} + +it('can generate valid url', () => { + expect(url({ channel, date })).toBe( + 'https://programme.tvb.com/ajax.php?action=channellist&code=B&date=2022-11-15' + ) +}) + +it('can parse response', () => { + const content = fs.readFileSync(path.resolve(__dirname, '__data__/content.html')) + const results = parser({ content, date }).map(p => { + p.start = p.start.toJSON() + p.stop = p.stop.toJSON() + return p + }) + + expect(results[0]).toMatchObject({ + start: '2022-11-14T22:00:00.000Z', + stop: '2022-11-14T23:00:00.000Z', + title: '想見你#3[粵/普][PG]', + description: + '韻如因父母離婚都不要自己而跑出家門,遇到子維,兩人互吐心事。雨萱順著照片上的唱片行線索,找到一家同名咖啡店,從文磊處得知照片中人是已經過世的韻如,從而推測那個男生也不是詮勝,但她內心反而更加痛苦。' + }) +}) + +it('can handle empty guide', () => { + const result = parser({ + content: fs.readFileSync(path.resolve(__dirname, '__data__/no-content.html')), + date + }) + expect(result).toMatchObject([]) +}) From cdac8f93e1550c90923c97bcb7d6755d34ead669 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sun, 13 Nov 2022 10:52:45 +0300 Subject: [PATCH 3/5] Create programme.tvb.com.config.js --- .../programme.tvb.com.config.js | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 sites/programme.tvb.com/programme.tvb.com.config.js diff --git a/sites/programme.tvb.com/programme.tvb.com.config.js b/sites/programme.tvb.com/programme.tvb.com.config.js new file mode 100644 index 00000000..a5b4d240 --- /dev/null +++ b/sites/programme.tvb.com/programme.tvb.com.config.js @@ -0,0 +1,63 @@ +const dayjs = require('dayjs') +const cheerio = require('cheerio') +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: 'programme.tvb.com', + url: function ({ channel, date }) { + return `https://programme.tvb.com/ajax.php?action=channellist&code=${ + channel.site_id + }&date=${date.format('YYYY-MM-DD')}` + }, + parser: function ({ content, date }) { + let programs = [] + const items = parseItems(content) + items.forEach(item => { + const prev = programs[programs.length - 1] + const $item = cheerio.load(item) + let start = parseStart($item, date) + if (prev) { + if (start.isBefore(prev.start)) { + start = start.add(1, 'd') + date = date.add(1, 'd') + } + prev.stop = start + } + const stop = start.add(30, 'm') + programs.push({ + title: parseTitle($item), + description: parseDescription($item), + start, + stop + }) + }) + + return programs + } +} + +function parseTitle($item) { + return $item('.ftit').text().trim() +} + +function parseDescription($item) { + return $item('.full').text().trim() +} + +function parseStart($item, date) { + const time = $item('.time').text() + + return dayjs.tz(`${date.format('YYYY-MM-DD')} ${time}`, 'YYYY-MM-DD hh:mmA', 'Asia/Hong_Kong') +} + +function parseItems(content) { + const $ = cheerio.load(content) + + return $('ul > li.item').toArray() +} From 0f3686de7b134ac369e6be35aa4da7e1bb5f60e9 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sun, 13 Nov 2022 10:53:07 +0300 Subject: [PATCH 4/5] Create programme.tvb.com_hk.channels.xml --- .../programme.tvb.com_hk.channels.xml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 sites/programme.tvb.com/programme.tvb.com_hk.channels.xml diff --git a/sites/programme.tvb.com/programme.tvb.com_hk.channels.xml b/sites/programme.tvb.com/programme.tvb.com_hk.channels.xml new file mode 100644 index 00000000..a8210200 --- /dev/null +++ b/sites/programme.tvb.com/programme.tvb.com_hk.channels.xml @@ -0,0 +1,14 @@ + + + + J2 + 華語劇台 + TVB經典台 + 粵語片台 + 無綫財經體育資訊台 + 翡翠台 + 無綫新聞台 + Pearl + TVB星河頻道 + + From b246c1f3b8d84ccd442619b87624adabf3b3fab3 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sun, 13 Nov 2022 10:53:13 +0300 Subject: [PATCH 5/5] Create programme.tvb.com.yml --- .github/workflows/programme.tvb.com.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/workflows/programme.tvb.com.yml diff --git a/.github/workflows/programme.tvb.com.yml b/.github/workflows/programme.tvb.com.yml new file mode 100644 index 00000000..11f614a4 --- /dev/null +++ b/.github/workflows/programme.tvb.com.yml @@ -0,0 +1,17 @@ +name: programme.tvb.com +on: + schedule: + - cron: '0 3 * * *' + workflow_dispatch: + workflow_run: + workflows: [_trigger] + types: + - completed +jobs: + load: + uses: ./.github/workflows/_load.yml + with: + site: ${{github.workflow}} + secrets: + APP_ID: ${{ secrets.APP_ID }} + APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}