diff --git a/.github/workflows/clickthecity.com.yml b/.github/workflows/clickthecity.com.yml new file mode 100644 index 00000000..31b3e2d8 --- /dev/null +++ b/.github/workflows/clickthecity.com.yml @@ -0,0 +1,17 @@ +name: clickthecity.com +on: + schedule: + - cron: '0 0 * * *' + 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 }} diff --git a/scripts/commands/channels/parse.js b/scripts/commands/channels/parse.js index 395d5fa6..0200de32 100644 --- a/scripts/commands/channels/parse.js +++ b/scripts/commands/channels/parse.js @@ -24,7 +24,12 @@ async function main() { if (isPromise(channels)) { channels = await channels } - channels = _.sortBy(channels, 'xmltv_id') + channels = channels.map(c => { + c.lang = c.lang || 'en' + + return c + }) + channels = _.sortBy(channels, ['lang', 'xmltv_id']) const dir = file.dirname(options.config) const outputFilepath = options.output || `${dir}/${config.site}.channels.xml` diff --git a/sites/clickthecity.com/clickthecity.com.config.js b/sites/clickthecity.com/clickthecity.com.config.js new file mode 100644 index 00000000..6f21e240 --- /dev/null +++ b/sites/clickthecity.com/clickthecity.com.config.js @@ -0,0 +1,104 @@ +const cheerio = require('cheerio') +const axios = require('axios') +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: 'clickthecity.com', + url({ channel }) { + return `https://www.clickthecity.com/tv/network/${channel.site_id}` + }, + request: { + method: 'POST', + headers: { + 'content-type': 'application/x-www-form-urlencoded' + }, + data({ date }) { + const params = new URLSearchParams() + params.append('optDate', date.format('YYYY-MM-DD')) + params.append('optTime', '00:00:00') + + return params + } + }, + parser({ content, date }) { + const programs = [] + const items = parseItems(content, date) + items.forEach(item => { + const prev = programs[programs.length - 1] + const $item = cheerio.load(item) + const start = parseStart($item, date) + const stop = parseStop($item, date) + if (stop && prev && stop.isBefore(prev.start)) return + programs.push({ + title: parseTitle($item), + start, + stop + }) + }) + + return programs + }, + async channels() { + const html = await axios + .get(`https://www.clickthecity.com/tv-networks/`) + .then(r => r.data) + .catch(console.log) + const $ = cheerio.load(html) + const items = $( + '#main > div > div > div > section.elementor-section.elementor-top-section.elementor-element.elementor-element-a3c51b3.elementor-section-boxed.elementor-section-height-default.elementor-section-height-default > div > div > div.elementor-column.elementor-col-50.elementor-top-column.elementor-element.elementor-element-b23e0a8 > div > div > div.elementor-element.elementor-element-b46952e.elementor-posts--align-center.elementor-grid-tablet-3.elementor-grid-mobile-3.elementor-grid-4.elementor-posts--thumbnail-top.elementor-widget.elementor-widget-posts > div > div > article' + ).toArray() + + return items.map(item => { + const name = $(item).find('div > h3').text().trim() + const url = $(item).find('a').attr('href') + const [_, site_id] = url.match(/network\/(.*)\//) || [null, null] + + return { + site_id, + name + } + }) + } +} + +function parseTitle($item) { + return $item('td > a').text().trim() +} + +function parseStart($item, date) { + const url = $item('td > a').attr('href') || '' + const [_, time] = url.match(/starttime=(\d{1,2}%3A\d{2}\+(AM|PM))/) || [null, null] + if (!time) return null + + return dayjs.tz( + `${date.format('YYYY-MM-DD')} ${time.replace('%3A', ':')}`, + 'YYYY-MM-DD h:mm A', + 'Asia/Manila' + ) +} + +function parseStop($item, date) { + const url = $item('td > a').attr('href') || '' + const [_, time] = url.match(/endtime=(\d{1,2}%3A\d{2}\+(AM|PM))/) || [null, null] + if (!time) return null + + return dayjs.tz( + `${date.format('YYYY-MM-DD')} ${time.replace('%3A', ':')}`, + 'YYYY-MM-DD h:mm A', + 'Asia/Manila' + ) +} + +function parseItems(content, date) { + const $ = cheerio.load(content) + const stringDate = date.format('MMMM DD') + + return $(`#tvlistings > tbody > tr:not(.bg-dark)`).toArray() +} diff --git a/sites/clickthecity.com/clickthecity.com.test.js b/sites/clickthecity.com/clickthecity.com.test.js new file mode 100644 index 00000000..f42f2179 --- /dev/null +++ b/sites/clickthecity.com/clickthecity.com.test.js @@ -0,0 +1,72 @@ +// npm run channels:parse -- --config=./sites/clickthecity.com/clickthecity.com.config.js --output=./sites/clickthecity.com/clickthecity.com_ph.channels.xml +// npx epg-grabber --config=sites/clickthecity.com/clickthecity.com.config.js --channels=sites/clickthecity.com/clickthecity.com_ph.channels.xml --output=guide.xml --days=2 + +const { parser, url, request } = require('./clickthecity.com.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('2022-03-05', 'YYYY-MM-DD').startOf('d') +const channel = { + site_id: 'tv5', + xmltv_id: 'TV5.ph' +} + +it('can generate valid url', () => { + expect(url({ channel })).toBe('https://www.clickthecity.com/tv/network/tv5') +}) + +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' + }) +}) + +it('can generate valid request data', () => { + const result = request.data({ date }) + expect(result.get('optDate')).toBe('2022-03-05') + expect(result.get('optTime')).toBe('00:00:00') +}) + +it('can parse response', () => { + const content = `
` + + const result = parser({ content, date }).map(p => { + p.start = p.start.toJSON() + p.stop = p.stop.toJSON() + return p + }) + + expect(result).toMatchObject([ + { + start: '2022-03-04T16:00:00.000Z', + stop: '2022-03-04T17:00:00.000Z', + title: `CCF Worship Service` + }, + { + start: '2022-03-04T22:30:00.000Z', + stop: '2022-03-04T23:30:00.000Z', + title: `Word Of God` + }, + { + start: '2022-03-05T12:00:00.000Z', + stop: '2022-03-05T13:00:00.000Z', + title: `Rated Korina S2` + } + ]) +}) + +it('can handle empty guide', () => { + const result = parser({ + date, + channel, + content: `` + }) + expect(result).toMatchObject([]) +}) diff --git a/sites/clickthecity.com/clickthecity.com_ph.channels.xml b/sites/clickthecity.com/clickthecity.com_ph.channels.xml new file mode 100644 index 00000000..d1b63bcb --- /dev/null +++ b/sites/clickthecity.com/clickthecity.com_ph.channels.xml @@ -0,0 +1,39 @@ + +