diff --git a/sites/taiwanplus.com/taiwanplus.com.channels.xml b/sites/taiwanplus.com/taiwanplus.com.channels.xml new file mode 100644 index 00000000..320a0ec0 --- /dev/null +++ b/sites/taiwanplus.com/taiwanplus.com.channels.xml @@ -0,0 +1,7 @@ + + + + Taiwan Plus TV + + \ No newline at end of file diff --git a/sites/taiwanplus.com/taiwanplus.com.config.js b/sites/taiwanplus.com/taiwanplus.com.config.js new file mode 100644 index 00000000..803f1844 --- /dev/null +++ b/sites/taiwanplus.com/taiwanplus.com.config.js @@ -0,0 +1,71 @@ +const dayjs = require('dayjs') + +var isSameOrAfter = require('dayjs/plugin/isSameOrAfter') +dayjs.extend(isSameOrAfter) + +var isSameOrBefore = require('dayjs/plugin/isSameOrBefore') +dayjs.extend(isSameOrBefore) + +module.exports = { + site: 'taiwanplus.com', + days: 7, + output: 'taiwanplus.com.guide.xml', + channels: 'taiwanplus.com.channels.xml', + lang: 'en', + delay: 5000, + + url: function () { + return 'https://www.taiwanplus.com/api/video/live/schedule/0' + }, + + request: { + method: 'GET', + timeout: 5000, + cache: { ttl: 60 * 60 * 1000 }, // 60 * 60 seconds = 1 hour + headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36' } + }, + + logo: function (context) { + return context.channel.logo + }, + + parser: function (context) { + let programs = [] + const scheduleDates = parseItems(context.content) + const today = dayjs.utc(context.date).startOf('day') + const lastDay = today.add(1, 'day') + + for(let scheduleDate of scheduleDates) { + const currentScheduleDate = new dayjs.utc(scheduleDate.date, 'YYYY/MM/DD') + + if (currentScheduleDate.isBefore(today)) { + continue + } else if (currentScheduleDate.isBefore(lastDay)) { + scheduleDate.schedule.forEach(function(program, i) { + programs.push({ + title: program.title, + start: dayjs.utc(program.dateTime, 'YYYY/MM/DD HH:mm'), + stop: (i != (scheduleDate.schedule.length - 1)) ? dayjs.utc(program.dateTime, 'YYYY/MM/DD HH:mm') : dayjs.utc(program.dateTime, 'YYYY/MM/DD HH:mm').add(1, 'day').startOf('day'), + description: program.description, + icon: program.image, + category: program.categoryName, + rating: program.ageRating + }) + }) + } else { + continue + } + } + + return programs + } +} + +function parseItems(content) { + if (content != '') { + const data = JSON.parse(content) + return (!data || !data.data || !Array.isArray(data.data)) ? [] : data.data + } else { + return [] + } +} \ No newline at end of file diff --git a/sites/taiwanplus.com/taiwanplus.com.test.js b/sites/taiwanplus.com/taiwanplus.com.test.js new file mode 100644 index 00000000..dace4883 --- /dev/null +++ b/sites/taiwanplus.com/taiwanplus.com.test.js @@ -0,0 +1,38 @@ +// npx epg-grabber --config=sites/taiwanplus.com/taiwanplus.com.config.js --channels=sites/taiwanplus.com/taiwanplus.com.channels.xml --output=guide.xml --days=3 +// npx jest taiwanplus.com.test.js + +const { url, parser } = require('./taiwanplus.com.config.js') +const dayjs = require('dayjs') +const utc = require('dayjs/plugin/utc') +dayjs.extend(utc) + +const date = dayjs.utc('2023-08-20', 'YYYY-MM-DD').startOf('d') +const channel = { site_id: '#', xmltv_id: 'TaiwanPlusTV.tw', lang: 'en', logo: 'https://i.imgur.com/SfcZyqm.png' } + +it('can generate valid url', () => { + expect(url({ channel, date })).toBe('https://www.taiwanplus.com/api/video/live/schedule/0') +}) + +it('can parse response', () => { + const content = `{"data":[{"date":"2023/08/20","weekday":"SUN","schedule":[{"programId":30668,"dateTime":"2023/08/20 00:00","time":"00:00","image":"https://prod-img.taiwanplus.com/live-schedule/Single/S30668_20230810104937.webp","title":"Master Class","shortDescription":"From blockchain to Buddha statues, Taiwan’s culture is a kaleidoscope of old and new just waiting to be discovered.","description":"From blockchain to Buddha statues, Taiwan’s culture is a kaleidoscope of old and new just waiting to be discovered.","ageRating":"0+","programWebSiteType":"4","url":"","vodId":null,"categoryId":90000474,"categoryType":2,"categoryName":"TaiwanPlus ✕ Discovery","categoryFullPath":"Originals/TaiwanPlus ✕ Discovery","encodedCategoryFullPath":"originals/taiwanplus-discovery"}]}],"success":true,"code":"0000","message":""}` + + const results = parser({ content, date }) + + expect(results).toMatchObject([ + { + title: 'Master Class', + start: dayjs.utc('2023/08/20 00:00', 'YYYY/MM/DD HH:mm'), + stop: dayjs.utc('2023/08/21 00:00', 'YYYY/MM/DD HH:mm'), + description: `From blockchain to Buddha statues, Taiwan’s culture is a kaleidoscope of old and new just waiting to be discovered.`, + icon: 'https://prod-img.taiwanplus.com/live-schedule/Single/S30668_20230810104937.webp', + category: 'TaiwanPlus ✕ Discovery', + rating: '0+' + } + ]) +}) + +it('can handle empty guide', () => { + const results = parser({ content: '' }) + + expect(results).toMatchObject([]) +})