Initial Commit
This commit is contained in:
1
sites/rtp.pt/__data__/content.json
Normal file
1
sites/rtp.pt/__data__/content.json
Normal file
File diff suppressed because one or more lines are too long
15
sites/rtp.pt/rtp.pt.channels.xml
Normal file
15
sites/rtp.pt/rtp.pt.channels.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<site site="rtp.pt">
|
||||
<channels>
|
||||
<channel lang="pt" xmltv_id="RTP1.pt" site_id="lis#1">RTP1</channel>
|
||||
<channel lang="pt" xmltv_id="RTP2.pt" site_id="lis#8">RTP2</channel>
|
||||
<channel lang="pt" xmltv_id="RTP3.pt" site_id="lis#7">RTP3</channel>
|
||||
<channel lang="pt" xmltv_id="RTPMemoria.pt" site_id="lis#9">RTP Memória</channel>
|
||||
<channel lang="pt" xmltv_id="RTPAfrica.pt" site_id="lis#6">RTP África</channel>
|
||||
<channel lang="pt" xmltv_id="RTPInternacional.pt" site_id="lis#5">RTP Internacional</channel>
|
||||
<channel lang="pt" xmltv_id="RTPiAmerica.pt" site_id="rja#5">RTP Internacional América</channel>
|
||||
<channel lang="pt" xmltv_id="RTPiAsia.pt" site_id="per#5">RTP Internacional Ásia</channel>
|
||||
<channel lang="pt" xmltv_id="RTPAcores.pt" site_id="lis#3">RTP Açores</channel>
|
||||
<channel lang="pt" xmltv_id="RTPMadeira.pt" site_id="lis#4">RTP Madeira</channel>
|
||||
</channels>
|
||||
</site>
|
||||
81
sites/rtp.pt/rtp.pt.config.js
Normal file
81
sites/rtp.pt/rtp.pt.config.js
Normal file
@@ -0,0 +1,81 @@
|
||||
const _ = require('lodash')
|
||||
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)
|
||||
|
||||
const tz = {
|
||||
lis: 'Europe/Lisbon',
|
||||
per: 'Asia/Macau',
|
||||
rja: 'America/Sao_Paulo'
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
site: 'rtp.pt',
|
||||
days: 2,
|
||||
url({ channel, date }) {
|
||||
let [region, channelCode] = channel.site_id.split('#')
|
||||
return `https://www.rtp.pt/EPG/json/rtp-channels-page/list-grid/tv/${channelCode}/${date.format(
|
||||
'D-M-YYYY'
|
||||
)}/${region}`
|
||||
},
|
||||
parser({ content, channel }) {
|
||||
let programs = []
|
||||
const items = parseItems(content)
|
||||
items.forEach(item => {
|
||||
const prev = programs[programs.length - 1]
|
||||
let start = parseStart(item, channel)
|
||||
if (!start) return
|
||||
if (prev) {
|
||||
prev.stop = start
|
||||
}
|
||||
const stop = start.add(30, 'm')
|
||||
programs.push({
|
||||
title: item.name,
|
||||
description: item.description,
|
||||
icon: parseIcon(item),
|
||||
start,
|
||||
stop
|
||||
})
|
||||
})
|
||||
|
||||
return programs
|
||||
},
|
||||
async channels() {
|
||||
const items = await axios
|
||||
.get('https://www.rtp.pt/EPG/json/rtp-home-page/list-channels/tv')
|
||||
.then(r => r.data.result)
|
||||
.catch(console.error)
|
||||
|
||||
return items.map(i => {
|
||||
return {
|
||||
lang: 'pt',
|
||||
site_id: i.channel_code,
|
||||
name: i.channel_name
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function parseIcon(item) {
|
||||
const last = item.image.pop()
|
||||
if (!last) return null
|
||||
return last.src
|
||||
}
|
||||
|
||||
function parseStart(item, channel) {
|
||||
let [region] = channel.site_id.split('#')
|
||||
return dayjs.tz(item.date, 'YYYY-MM-DD HH:mm:ss', tz[region])
|
||||
}
|
||||
|
||||
function parseItems(content) {
|
||||
if (!content) return []
|
||||
const data = JSON.parse(content)
|
||||
|
||||
return _.flatten(Object.values(data.result))
|
||||
}
|
||||
45
sites/rtp.pt/rtp.pt.test.js
Normal file
45
sites/rtp.pt/rtp.pt.test.js
Normal file
@@ -0,0 +1,45 @@
|
||||
// npm run channels:parse -- --config=./sites/rtp.pt/rtp.pt.config.js --output=./sites/rtp.pt/rtp.pt.channels.xml
|
||||
// npx epg-grabber --config=sites/rtp.pt/rtp.pt.config.js --channels=sites/rtp.pt/rtp.pt.channels.xml --output=guide.xml --days=2
|
||||
|
||||
const { parser, url } = require('./rtp.pt.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-12-02', 'YYYY-MM-DD').startOf('d')
|
||||
const channel = {
|
||||
site_id: 'lis#4',
|
||||
xmltv_id: 'RTPMadeira.pt'
|
||||
}
|
||||
|
||||
it('can generate valid url', () => {
|
||||
expect(url({ channel, date })).toBe(
|
||||
'https://www.rtp.pt/EPG/json/rtp-channels-page/list-grid/tv/4/2-12-2022/lis'
|
||||
)
|
||||
})
|
||||
|
||||
it('can parse response', () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content.json'))
|
||||
let results = parser({ content, channel }).map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(results[9]).toMatchObject({
|
||||
start: '2022-12-02T23:30:00.000Z',
|
||||
stop: '2022-12-03T00:00:00.000Z',
|
||||
title: 'Telejornal Madeira',
|
||||
description: 'Informação de proximidade. De confiança!',
|
||||
icon: 'https://cdn-images.rtp.pt/EPG/imagens/15790_43438_8820.png?w=384&h=216'
|
||||
})
|
||||
})
|
||||
|
||||
it('can handle empty guide', () => {
|
||||
const result = parser({ content: '', channel, date })
|
||||
expect(result).toMatchObject([])
|
||||
})
|
||||
Reference in New Issue
Block a user