mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-09 16:40:07 -04:00
Fixes linter errors
This commit is contained in:
parent
57e508fc3b
commit
63c86a2b30
393 changed files with 28447 additions and 28443 deletions
|
@ -1,89 +1,89 @@
|
|||
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: 'rthk.hk',
|
||||
days: 2,
|
||||
request: {
|
||||
headers({ channel }) {
|
||||
return {
|
||||
Cookie: `lang=${channel.lang}`
|
||||
}
|
||||
},
|
||||
cache: {
|
||||
ttl: 60 * 60 * 1000 // 1h
|
||||
}
|
||||
},
|
||||
url: function ({ date }) {
|
||||
return `https://www.rthk.hk/timetable/main_timetable/${date.format('YYYYMMDD')}`
|
||||
},
|
||||
parser({ content, channel, date }) {
|
||||
const programs = []
|
||||
const items = parseItems(content, channel)
|
||||
for (let item of items) {
|
||||
const $item = cheerio.load(item)
|
||||
programs.push({
|
||||
title: parseTitle($item),
|
||||
sub_title: parseSubTitle($item),
|
||||
categories: parseCategories($item),
|
||||
icon: parseIcon($item),
|
||||
start: parseStart($item, date),
|
||||
stop: parseStop($item, date)
|
||||
})
|
||||
}
|
||||
|
||||
return programs
|
||||
}
|
||||
}
|
||||
|
||||
function parseIcon($item) {
|
||||
return $item('.single-wrap').data('p')
|
||||
}
|
||||
|
||||
function parseCategories($item) {
|
||||
let cate = $item('.single-wrap').data('cate') || ''
|
||||
let [, categories] = cate.match(/^\|(.*)\|$/) || [null, '']
|
||||
|
||||
return categories.split('||').filter(Boolean)
|
||||
}
|
||||
|
||||
function parseTitle($item) {
|
||||
return $item('.showTit').attr('title')
|
||||
}
|
||||
|
||||
function parseSubTitle($item) {
|
||||
return $item('.showEpi').attr('title')
|
||||
}
|
||||
|
||||
function parseStart($item, date) {
|
||||
const timeRow = $item('.timeRow').text().trim()
|
||||
const [, HH, mm] = timeRow.match(/^(\d+):(\d+)-/) || [null, null, null]
|
||||
if (!HH || !mm) return null
|
||||
|
||||
return dayjs.tz(`${date.format('YYYY-MM-DD')} ${HH}:${mm}`, 'YYYY-MM-DD HH:mm', 'Asia/Hong_Kong')
|
||||
}
|
||||
|
||||
function parseStop($item, date) {
|
||||
const timeRow = $item('.timeRow').text().trim()
|
||||
const [, HH, mm] = timeRow.match(/-(\d+):(\d+)$/) || [null, null, null]
|
||||
if (!HH || !mm) return null
|
||||
|
||||
return dayjs.tz(`${date.format('YYYY-MM-DD')} ${HH}:${mm}`, 'YYYY-MM-DD HH:mm', 'Asia/Hong_Kong')
|
||||
}
|
||||
|
||||
function parseItems(content, channel) {
|
||||
const data = JSON.parse(content)
|
||||
if (!data || !Array.isArray(data.result)) return []
|
||||
const channelData = data.result.find(i => i.key == channel.site_id)
|
||||
if (!channelData || !channelData.data) return []
|
||||
const $ = cheerio.load(channelData.data)
|
||||
|
||||
return $('.showWrap').toArray()
|
||||
}
|
||||
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: 'rthk.hk',
|
||||
days: 2,
|
||||
request: {
|
||||
headers({ channel }) {
|
||||
return {
|
||||
Cookie: `lang=${channel.lang}`
|
||||
}
|
||||
},
|
||||
cache: {
|
||||
ttl: 60 * 60 * 1000 // 1h
|
||||
}
|
||||
},
|
||||
url: function ({ date }) {
|
||||
return `https://www.rthk.hk/timetable/main_timetable/${date.format('YYYYMMDD')}`
|
||||
},
|
||||
parser({ content, channel, date }) {
|
||||
const programs = []
|
||||
const items = parseItems(content, channel)
|
||||
for (let item of items) {
|
||||
const $item = cheerio.load(item)
|
||||
programs.push({
|
||||
title: parseTitle($item),
|
||||
sub_title: parseSubTitle($item),
|
||||
categories: parseCategories($item),
|
||||
icon: parseIcon($item),
|
||||
start: parseStart($item, date),
|
||||
stop: parseStop($item, date)
|
||||
})
|
||||
}
|
||||
|
||||
return programs
|
||||
}
|
||||
}
|
||||
|
||||
function parseIcon($item) {
|
||||
return $item('.single-wrap').data('p')
|
||||
}
|
||||
|
||||
function parseCategories($item) {
|
||||
let cate = $item('.single-wrap').data('cate') || ''
|
||||
let [, categories] = cate.match(/^\|(.*)\|$/) || [null, '']
|
||||
|
||||
return categories.split('||').filter(Boolean)
|
||||
}
|
||||
|
||||
function parseTitle($item) {
|
||||
return $item('.showTit').attr('title')
|
||||
}
|
||||
|
||||
function parseSubTitle($item) {
|
||||
return $item('.showEpi').attr('title')
|
||||
}
|
||||
|
||||
function parseStart($item, date) {
|
||||
const timeRow = $item('.timeRow').text().trim()
|
||||
const [, HH, mm] = timeRow.match(/^(\d+):(\d+)-/) || [null, null, null]
|
||||
if (!HH || !mm) return null
|
||||
|
||||
return dayjs.tz(`${date.format('YYYY-MM-DD')} ${HH}:${mm}`, 'YYYY-MM-DD HH:mm', 'Asia/Hong_Kong')
|
||||
}
|
||||
|
||||
function parseStop($item, date) {
|
||||
const timeRow = $item('.timeRow').text().trim()
|
||||
const [, HH, mm] = timeRow.match(/-(\d+):(\d+)$/) || [null, null, null]
|
||||
if (!HH || !mm) return null
|
||||
|
||||
return dayjs.tz(`${date.format('YYYY-MM-DD')} ${HH}:${mm}`, 'YYYY-MM-DD HH:mm', 'Asia/Hong_Kong')
|
||||
}
|
||||
|
||||
function parseItems(content, channel) {
|
||||
const data = JSON.parse(content)
|
||||
if (!data || !Array.isArray(data.result)) return []
|
||||
const channelData = data.result.find(i => i.key == channel.site_id)
|
||||
if (!channelData || !channelData.data) return []
|
||||
const $ = cheerio.load(channelData.data)
|
||||
|
||||
return $('.showWrap').toArray()
|
||||
}
|
||||
|
|
|
@ -1,82 +1,82 @@
|
|||
// npm run grab -- --site=rthk.hk
|
||||
|
||||
const { parser, url, request } = require('./rthk.hk.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)
|
||||
|
||||
jest.mock('axios')
|
||||
|
||||
const date = dayjs.utc('2022-12-02', 'YYYY-MM-DD').startOf('d')
|
||||
const channel = {
|
||||
site_id: '31',
|
||||
xmltv_id: 'RTHKTV31.hk',
|
||||
lang: 'zh'
|
||||
}
|
||||
|
||||
it('can generate valid url', () => {
|
||||
expect(url({ date })).toBe('https://www.rthk.hk/timetable/main_timetable/20221202')
|
||||
})
|
||||
|
||||
it('can generate valid request headers', () => {
|
||||
expect(request.headers({ channel })).toMatchObject({
|
||||
Cookie: 'lang=zh'
|
||||
})
|
||||
})
|
||||
|
||||
it('can generate valid request headers for English version', () => {
|
||||
const channelEN = { ...channel, lang: 'en' }
|
||||
|
||||
expect(request.headers({ channel: channelEN })).toMatchObject({
|
||||
Cookie: 'lang=en'
|
||||
})
|
||||
})
|
||||
|
||||
it('can parse response', () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content_zh.json'))
|
||||
let results = parser({ content, channel, date })
|
||||
results = results.map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(results[0]).toMatchObject({
|
||||
start: '2022-12-01T16:00:00.000Z',
|
||||
stop: '2022-12-01T17:00:00.000Z',
|
||||
title: '問天',
|
||||
sub_title: '第十四集',
|
||||
categories: ['戲劇'],
|
||||
icon: 'https://www.rthk.hk/assets/images/rthk/dtt31/thegreataerospace/10239_1920_s.jpg'
|
||||
})
|
||||
})
|
||||
|
||||
it('can parse response in English', () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content_en.json'))
|
||||
let results = parser({ content, channel, date })
|
||||
results = results.map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(results[0]).toMatchObject({
|
||||
start: '2022-12-01T16:00:00.000Z',
|
||||
stop: '2022-12-01T17:00:00.000Z',
|
||||
title: 'The Great Aerospace',
|
||||
sub_title: 'Episode 14',
|
||||
categories: ['戲劇'],
|
||||
icon: 'https://www.rthk.hk/assets/images/rthk/dtt31/thegreataerospace/10239_1920_s.jpg'
|
||||
})
|
||||
})
|
||||
|
||||
it('can handle empty guide', () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/no_content.json'))
|
||||
const results = parser({ date, channel, content })
|
||||
|
||||
expect(results).toMatchObject([])
|
||||
})
|
||||
// npm run grab -- --site=rthk.hk
|
||||
|
||||
const { parser, url, request } = require('./rthk.hk.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)
|
||||
|
||||
jest.mock('axios')
|
||||
|
||||
const date = dayjs.utc('2022-12-02', 'YYYY-MM-DD').startOf('d')
|
||||
const channel = {
|
||||
site_id: '31',
|
||||
xmltv_id: 'RTHKTV31.hk',
|
||||
lang: 'zh'
|
||||
}
|
||||
|
||||
it('can generate valid url', () => {
|
||||
expect(url({ date })).toBe('https://www.rthk.hk/timetable/main_timetable/20221202')
|
||||
})
|
||||
|
||||
it('can generate valid request headers', () => {
|
||||
expect(request.headers({ channel })).toMatchObject({
|
||||
Cookie: 'lang=zh'
|
||||
})
|
||||
})
|
||||
|
||||
it('can generate valid request headers for English version', () => {
|
||||
const channelEN = { ...channel, lang: 'en' }
|
||||
|
||||
expect(request.headers({ channel: channelEN })).toMatchObject({
|
||||
Cookie: 'lang=en'
|
||||
})
|
||||
})
|
||||
|
||||
it('can parse response', () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content_zh.json'))
|
||||
let results = parser({ content, channel, date })
|
||||
results = results.map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(results[0]).toMatchObject({
|
||||
start: '2022-12-01T16:00:00.000Z',
|
||||
stop: '2022-12-01T17:00:00.000Z',
|
||||
title: '問天',
|
||||
sub_title: '第十四集',
|
||||
categories: ['戲劇'],
|
||||
icon: 'https://www.rthk.hk/assets/images/rthk/dtt31/thegreataerospace/10239_1920_s.jpg'
|
||||
})
|
||||
})
|
||||
|
||||
it('can parse response in English', () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content_en.json'))
|
||||
let results = parser({ content, channel, date })
|
||||
results = results.map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(results[0]).toMatchObject({
|
||||
start: '2022-12-01T16:00:00.000Z',
|
||||
stop: '2022-12-01T17:00:00.000Z',
|
||||
title: 'The Great Aerospace',
|
||||
sub_title: 'Episode 14',
|
||||
categories: ['戲劇'],
|
||||
icon: 'https://www.rthk.hk/assets/images/rthk/dtt31/thegreataerospace/10239_1920_s.jpg'
|
||||
})
|
||||
})
|
||||
|
||||
it('can handle empty guide', () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/no_content.json'))
|
||||
const results = parser({ date, channel, content })
|
||||
|
||||
expect(results).toMatchObject([])
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue