mirror of
https://github.com/iptv-org/epg
synced 2026-05-06 09:27:03 -04:00
Fixes linter errors
This commit is contained in:
@@ -1,114 +1,114 @@
|
||||
const axios = require('axios')
|
||||
const cheerio = require('cheerio')
|
||||
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: 'nos.pt',
|
||||
days: 2,
|
||||
url({ channel }) {
|
||||
return `https://www.nos.pt/particulares/televisao/guia-tv/Pages/channel.aspx?channel=${channel.site_id}`
|
||||
},
|
||||
async parser({ content, date }) {
|
||||
const programs = []
|
||||
const items = parseItems(content, date)
|
||||
date = date.subtract(1, 'd')
|
||||
for (let item of items) {
|
||||
const $item = cheerio.load(item)
|
||||
|
||||
const channelAcronym = parseChannelAcronym(content)
|
||||
const programId = parseProgramId($item)
|
||||
const details = await loadProgramDetails(channelAcronym, programId)
|
||||
|
||||
programs.push({
|
||||
title: details.title,
|
||||
description: details.description,
|
||||
icon: parseIcon(details),
|
||||
start: dayjs(details.start),
|
||||
stop: dayjs(details.stop)
|
||||
})
|
||||
}
|
||||
|
||||
return programs
|
||||
},
|
||||
async channels() {
|
||||
const html = await axios
|
||||
.get('https://www.nos.pt/particulares/televisao/guia-tv/Pages/default.aspx')
|
||||
.then(r => r.data)
|
||||
.catch(console.log)
|
||||
|
||||
const $ = cheerio.load(html)
|
||||
const items = $('#guide-filters > dl.dropdown-ord > dd > ul > li').toArray()
|
||||
|
||||
return items.map(item => {
|
||||
const $item = cheerio.load(item)
|
||||
|
||||
return {
|
||||
lang: 'pt',
|
||||
site_id: $item('.value').text().trim(),
|
||||
name: $item('a').clone().children().remove().end().text().trim()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function loadProgramDetails(channelAcronym, programId) {
|
||||
if (!channelAcronym || !programId) return {}
|
||||
const data = await axios
|
||||
.post(
|
||||
'https://www.nos.pt/_layouts/15/Armstrong/ApplicationPages/EPGGetProgramsAndDetails.aspx/GetProgramDetails',
|
||||
{
|
||||
programId,
|
||||
channelAcronym,
|
||||
hour: 'undefined',
|
||||
startHour: 'undefined',
|
||||
endHour: 'undefined'
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'content-type': 'application/json; charset=UTF-8'
|
||||
}
|
||||
}
|
||||
)
|
||||
.then(r => r.data)
|
||||
.catch(console.log)
|
||||
|
||||
if (!data) return {}
|
||||
|
||||
const [title, description, image, , , , start, stop] = data.d.split('_#|$_')
|
||||
|
||||
return {
|
||||
title,
|
||||
description,
|
||||
image,
|
||||
start,
|
||||
stop
|
||||
}
|
||||
}
|
||||
|
||||
function parseIcon(details) {
|
||||
return details.image ? `https://images.nos.pt/${details.image}` : null
|
||||
}
|
||||
|
||||
function parseProgramId($item) {
|
||||
return $item('a').attr('id')
|
||||
}
|
||||
|
||||
function parseChannelAcronym(content) {
|
||||
const $ = cheerio.load(content)
|
||||
|
||||
return $('#channel-logo > img').attr('alt')
|
||||
}
|
||||
|
||||
function parseItems(content, date) {
|
||||
const day = date.date()
|
||||
const $ = cheerio.load(content)
|
||||
|
||||
return $(`#day${day} > ul > li`).toArray()
|
||||
}
|
||||
const axios = require('axios')
|
||||
const cheerio = require('cheerio')
|
||||
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: 'nos.pt',
|
||||
days: 2,
|
||||
url({ channel }) {
|
||||
return `https://www.nos.pt/particulares/televisao/guia-tv/Pages/channel.aspx?channel=${channel.site_id}`
|
||||
},
|
||||
async parser({ content, date }) {
|
||||
const programs = []
|
||||
const items = parseItems(content, date)
|
||||
date = date.subtract(1, 'd')
|
||||
for (let item of items) {
|
||||
const $item = cheerio.load(item)
|
||||
|
||||
const channelAcronym = parseChannelAcronym(content)
|
||||
const programId = parseProgramId($item)
|
||||
const details = await loadProgramDetails(channelAcronym, programId)
|
||||
|
||||
programs.push({
|
||||
title: details.title,
|
||||
description: details.description,
|
||||
icon: parseIcon(details),
|
||||
start: dayjs(details.start),
|
||||
stop: dayjs(details.stop)
|
||||
})
|
||||
}
|
||||
|
||||
return programs
|
||||
},
|
||||
async channels() {
|
||||
const html = await axios
|
||||
.get('https://www.nos.pt/particulares/televisao/guia-tv/Pages/default.aspx')
|
||||
.then(r => r.data)
|
||||
.catch(console.log)
|
||||
|
||||
const $ = cheerio.load(html)
|
||||
const items = $('#guide-filters > dl.dropdown-ord > dd > ul > li').toArray()
|
||||
|
||||
return items.map(item => {
|
||||
const $item = cheerio.load(item)
|
||||
|
||||
return {
|
||||
lang: 'pt',
|
||||
site_id: $item('.value').text().trim(),
|
||||
name: $item('a').clone().children().remove().end().text().trim()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function loadProgramDetails(channelAcronym, programId) {
|
||||
if (!channelAcronym || !programId) return {}
|
||||
const data = await axios
|
||||
.post(
|
||||
'https://www.nos.pt/_layouts/15/Armstrong/ApplicationPages/EPGGetProgramsAndDetails.aspx/GetProgramDetails',
|
||||
{
|
||||
programId,
|
||||
channelAcronym,
|
||||
hour: 'undefined',
|
||||
startHour: 'undefined',
|
||||
endHour: 'undefined'
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'content-type': 'application/json; charset=UTF-8'
|
||||
}
|
||||
}
|
||||
)
|
||||
.then(r => r.data)
|
||||
.catch(console.log)
|
||||
|
||||
if (!data) return {}
|
||||
|
||||
const [title, description, image, , , , start, stop] = data.d.split('_#|$_')
|
||||
|
||||
return {
|
||||
title,
|
||||
description,
|
||||
image,
|
||||
start,
|
||||
stop
|
||||
}
|
||||
}
|
||||
|
||||
function parseIcon(details) {
|
||||
return details.image ? `https://images.nos.pt/${details.image}` : null
|
||||
}
|
||||
|
||||
function parseProgramId($item) {
|
||||
return $item('a').attr('id')
|
||||
}
|
||||
|
||||
function parseChannelAcronym(content) {
|
||||
const $ = cheerio.load(content)
|
||||
|
||||
return $('#channel-logo > img').attr('alt')
|
||||
}
|
||||
|
||||
function parseItems(content, date) {
|
||||
const day = date.date()
|
||||
const $ = cheerio.load(content)
|
||||
|
||||
return $(`#day${day} > ul > li`).toArray()
|
||||
}
|
||||
|
||||
@@ -1,100 +1,100 @@
|
||||
// npm run channels:parse -- --config=./sites/nos.pt/nos.pt.config.js --output=./sites/nos.pt/nos.pt.channels.xml
|
||||
// npm run grab -- --site=nos.pt
|
||||
|
||||
const { parser, url } = require('./nos.pt.config.js')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const axios = require('axios')
|
||||
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('2023-01-28', 'YYYY-MM-DD').startOf('d')
|
||||
const channel = {
|
||||
site_id: '5',
|
||||
xmltv_id: 'RTP1.pt'
|
||||
}
|
||||
|
||||
it('can generate valid url', () => {
|
||||
expect(url({ channel })).toBe(
|
||||
'https://www.nos.pt/particulares/televisao/guia-tv/Pages/channel.aspx?channel=5'
|
||||
)
|
||||
})
|
||||
|
||||
it('can parse response', async () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content.html'), 'utf8')
|
||||
|
||||
axios.post.mockImplementation((url, data) => {
|
||||
if (
|
||||
url ===
|
||||
'https://www.nos.pt/_layouts/15/Armstrong/ApplicationPages/EPGGetProgramsAndDetails.aspx/GetProgramDetails' &&
|
||||
JSON.stringify(data) ===
|
||||
JSON.stringify({
|
||||
programId: '81361',
|
||||
channelAcronym: 'RTP1',
|
||||
hour: 'undefined',
|
||||
startHour: 'undefined',
|
||||
endHour: 'undefined'
|
||||
})
|
||||
) {
|
||||
return Promise.resolve({
|
||||
data: JSON.parse(fs.readFileSync(path.resolve(__dirname, '__data__/program_0.json')))
|
||||
})
|
||||
} else if (
|
||||
url ===
|
||||
'https://www.nos.pt/_layouts/15/Armstrong/ApplicationPages/EPGGetProgramsAndDetails.aspx/GetProgramDetails' &&
|
||||
JSON.stringify(data) ===
|
||||
JSON.stringify({
|
||||
programId: '81382',
|
||||
channelAcronym: 'RTP1',
|
||||
hour: 'undefined',
|
||||
startHour: 'undefined',
|
||||
endHour: 'undefined'
|
||||
})
|
||||
) {
|
||||
return Promise.resolve({
|
||||
data: JSON.parse(fs.readFileSync(path.resolve(__dirname, '__data__/program_21.json')))
|
||||
})
|
||||
} else {
|
||||
return Promise.resolve({ data: '' })
|
||||
}
|
||||
})
|
||||
|
||||
let results = await parser({ content, date })
|
||||
results = results.map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(results[0]).toMatchObject({
|
||||
start: '2023-01-27T23:50:00.000Z',
|
||||
stop: '2023-01-28T00:36:00.000Z',
|
||||
title: 'Anatomia de Grey T.17 Ep.3',
|
||||
description:
|
||||
'Os médicos do Grey Sloan continuam a enfrentar a nova realidade do COVID-19 e lidam com um paciente conhecido e teimoso. Koracick fica encarregue dos internos e Link opera um terapeuta sexual.',
|
||||
icon: 'https://images.nos.pt/b6fd27f4bd0b404abd4c3fc4faa79024_resized_352x198.jpg'
|
||||
})
|
||||
|
||||
expect(results[21]).toMatchObject({
|
||||
start: '2023-01-28T21:38:00.000Z',
|
||||
stop: '2023-01-29T00:05:00.000Z',
|
||||
title: 'MasterChef Portugal T.1 Ep.10',
|
||||
description:
|
||||
'A maior competição de cozinha do mundo arranca ao comando de três dos mais conceituados chefs portugueses: Pedro Pena Bastos, Noélia Jerónimo e Ricardo Costa, que nos vão transmitir os seus conhecimentos e a sua paixão pela cozinha.',
|
||||
icon: 'https://images.nos.pt/8aa511d697f0401a88a0cb1ec2718cc3_resized_352x198.jpg'
|
||||
})
|
||||
})
|
||||
|
||||
it('can handle empty guide', async () => {
|
||||
const results = await parser({
|
||||
date,
|
||||
content: fs.readFileSync(path.resolve(__dirname, '__data__/no_content.html'), 'utf8')
|
||||
})
|
||||
|
||||
expect(results).toMatchObject([])
|
||||
})
|
||||
// npm run channels:parse -- --config=./sites/nos.pt/nos.pt.config.js --output=./sites/nos.pt/nos.pt.channels.xml
|
||||
// npm run grab -- --site=nos.pt
|
||||
|
||||
const { parser, url } = require('./nos.pt.config.js')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const axios = require('axios')
|
||||
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('2023-01-28', 'YYYY-MM-DD').startOf('d')
|
||||
const channel = {
|
||||
site_id: '5',
|
||||
xmltv_id: 'RTP1.pt'
|
||||
}
|
||||
|
||||
it('can generate valid url', () => {
|
||||
expect(url({ channel })).toBe(
|
||||
'https://www.nos.pt/particulares/televisao/guia-tv/Pages/channel.aspx?channel=5'
|
||||
)
|
||||
})
|
||||
|
||||
it('can parse response', async () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content.html'), 'utf8')
|
||||
|
||||
axios.post.mockImplementation((url, data) => {
|
||||
if (
|
||||
url ===
|
||||
'https://www.nos.pt/_layouts/15/Armstrong/ApplicationPages/EPGGetProgramsAndDetails.aspx/GetProgramDetails' &&
|
||||
JSON.stringify(data) ===
|
||||
JSON.stringify({
|
||||
programId: '81361',
|
||||
channelAcronym: 'RTP1',
|
||||
hour: 'undefined',
|
||||
startHour: 'undefined',
|
||||
endHour: 'undefined'
|
||||
})
|
||||
) {
|
||||
return Promise.resolve({
|
||||
data: JSON.parse(fs.readFileSync(path.resolve(__dirname, '__data__/program_0.json')))
|
||||
})
|
||||
} else if (
|
||||
url ===
|
||||
'https://www.nos.pt/_layouts/15/Armstrong/ApplicationPages/EPGGetProgramsAndDetails.aspx/GetProgramDetails' &&
|
||||
JSON.stringify(data) ===
|
||||
JSON.stringify({
|
||||
programId: '81382',
|
||||
channelAcronym: 'RTP1',
|
||||
hour: 'undefined',
|
||||
startHour: 'undefined',
|
||||
endHour: 'undefined'
|
||||
})
|
||||
) {
|
||||
return Promise.resolve({
|
||||
data: JSON.parse(fs.readFileSync(path.resolve(__dirname, '__data__/program_21.json')))
|
||||
})
|
||||
} else {
|
||||
return Promise.resolve({ data: '' })
|
||||
}
|
||||
})
|
||||
|
||||
let results = await parser({ content, date })
|
||||
results = results.map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(results[0]).toMatchObject({
|
||||
start: '2023-01-27T23:50:00.000Z',
|
||||
stop: '2023-01-28T00:36:00.000Z',
|
||||
title: 'Anatomia de Grey T.17 Ep.3',
|
||||
description:
|
||||
'Os médicos do Grey Sloan continuam a enfrentar a nova realidade do COVID-19 e lidam com um paciente conhecido e teimoso. Koracick fica encarregue dos internos e Link opera um terapeuta sexual.',
|
||||
icon: 'https://images.nos.pt/b6fd27f4bd0b404abd4c3fc4faa79024_resized_352x198.jpg'
|
||||
})
|
||||
|
||||
expect(results[21]).toMatchObject({
|
||||
start: '2023-01-28T21:38:00.000Z',
|
||||
stop: '2023-01-29T00:05:00.000Z',
|
||||
title: 'MasterChef Portugal T.1 Ep.10',
|
||||
description:
|
||||
'A maior competição de cozinha do mundo arranca ao comando de três dos mais conceituados chefs portugueses: Pedro Pena Bastos, Noélia Jerónimo e Ricardo Costa, que nos vão transmitir os seus conhecimentos e a sua paixão pela cozinha.',
|
||||
icon: 'https://images.nos.pt/8aa511d697f0401a88a0cb1ec2718cc3_resized_352x198.jpg'
|
||||
})
|
||||
})
|
||||
|
||||
it('can handle empty guide', async () => {
|
||||
const results = await parser({
|
||||
date,
|
||||
content: fs.readFileSync(path.resolve(__dirname, '__data__/no_content.html'), 'utf8')
|
||||
})
|
||||
|
||||
expect(results).toMatchObject([])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user