mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-10 00:50:09 -04:00
commit
7c3c4df585
2 changed files with 79 additions and 25 deletions
|
@ -1,6 +1,5 @@
|
|||
const jsdom = require('jsdom')
|
||||
const iconv = require('iconv-lite')
|
||||
const { JSDOM } = jsdom
|
||||
const cheerio = require('cheerio')
|
||||
const dayjs = require('dayjs')
|
||||
const utc = require('dayjs/plugin/utc')
|
||||
const timezone = require('dayjs/plugin/timezone')
|
||||
|
@ -16,48 +15,47 @@ module.exports = {
|
|||
return `https://tvgid.ua/channels/${channel.site_id}/${date.format('DDMMYYYY')}/tmall/`
|
||||
},
|
||||
parser: function ({ buffer, date }) {
|
||||
let PM = false
|
||||
const programs = []
|
||||
const items = parseItems(buffer)
|
||||
items.forEach(item => {
|
||||
const title = parseTitle(item)
|
||||
let start = parseStart(item, date)
|
||||
const $item = cheerio.load(item)
|
||||
const prev = programs[programs.length - 1]
|
||||
let start = parseStart($item, date)
|
||||
if (!start) return
|
||||
if (start.hour() > 11) PM = true
|
||||
if (start.hour() < 12 && PM) start = start.add(1, 'd')
|
||||
const stop = start.add(1, 'h')
|
||||
if (programs.length) {
|
||||
programs[programs.length - 1].stop = start
|
||||
if (prev) {
|
||||
if (start.isBefore(prev.start)) {
|
||||
start = start.add(1, 'd')
|
||||
date = date.add(1, 'd')
|
||||
}
|
||||
prev.stop = start
|
||||
}
|
||||
|
||||
programs.push({ title, start, stop })
|
||||
programs.push({ title: parseTitle($item), start, stop })
|
||||
})
|
||||
|
||||
return programs
|
||||
}
|
||||
}
|
||||
|
||||
function parseStart(item, date) {
|
||||
let time = (item.querySelector('td > table > tbody > tr > td.time') || { textContent: '' })
|
||||
.textContent
|
||||
if (!time) return null
|
||||
time = `${date.format('MM/DD/YYYY')} ${time}`
|
||||
function parseStart($item, date) {
|
||||
const timeString = $item('td > table > tbody > tr > td.time').text()
|
||||
if (!timeString) return null
|
||||
const dateString = `${date.format('MM/DD/YYYY')} ${timeString}`
|
||||
|
||||
return dayjs.tz(time, 'MM/DD/YYYY HH:mm', 'Europe/Kiev')
|
||||
return dayjs.tz(dateString, 'MM/DD/YYYY HH:mm', 'Europe/Kiev')
|
||||
}
|
||||
|
||||
function parseTitle(item) {
|
||||
return (
|
||||
item.querySelector('td > table > tbody > tr > td.item > a') ||
|
||||
item.querySelector('td > table > tbody > tr > td.item') || { textContent: '' }
|
||||
).textContent
|
||||
function parseTitle($item) {
|
||||
return $item('td > table > tbody > tr > td.item').text().trim()
|
||||
}
|
||||
|
||||
function parseItems(buffer) {
|
||||
const string = iconv.decode(buffer, 'win1251')
|
||||
const dom = new JSDOM(string)
|
||||
if (!buffer) return []
|
||||
const html = iconv.decode(buffer, 'win1251')
|
||||
const $ = cheerio.load(html)
|
||||
|
||||
return dom.window.document.querySelectorAll(
|
||||
return $(
|
||||
'#container > tbody > tr:nth-child(2) > td > table > tbody > tr > td > table:nth-child(2) > tbody > tr:not(:first-child)'
|
||||
)
|
||||
).toArray()
|
||||
}
|
||||
|
|
56
sites/tvgid.ua/tvgid.ua.test.js
Normal file
56
sites/tvgid.ua/tvgid.ua.test.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
// npx epg-grabber --config=sites/tvgid.ua/tvgid.ua.config.js --channels=sites/tvgid.ua/tvgid.ua_ua.channels.xml --output=.gh-pages/guides/ua/tvgid.ua.epg.xml --days=2
|
||||
|
||||
const { parser, url } = require('./tvgid.ua.config.js')
|
||||
const iconv = require('iconv-lite')
|
||||
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('2021-11-24', 'YYYY-MM-DD').startOf('d')
|
||||
const channel = {
|
||||
site_id: '1plus1',
|
||||
xmltv_id: '1Plus1.ua'
|
||||
}
|
||||
|
||||
it('can generate valid url', () => {
|
||||
expect(url({ channel, date })).toBe('https://tvgid.ua/channels/1plus1/24112021/tmall/')
|
||||
})
|
||||
|
||||
it('can parse response', () => {
|
||||
const content = `<!DOCTYPE html><html> <head><meta http-equiv="Content-Type" content="text/html; charset=windows-1251"></head> <body> <table> <tr> <td style="padding: 0 10px"> <table cellspacing="0" cellpadding="0" width="100%"> <tr> <td> <table width="100%" cellspacing="0" cellpadding="0"> <tr> <td width="100%" style="border-right: 1px solid #ccc" valign="top"> <table cellspacing="0" cellpadding="0" border="0" width="99%" id="container"> <tr> <td> <h1 class="h1small">Телепрограма 1+1 на 24.11.2021</h1> </td></tr><tr> <td> <table cellspacing="0" cellpadding="0" width="100%"> <tr> <td valign="top" style="padding-right: 5px"> <table cellspacing="0" cellpadding="0" border="0" width="100%" bgcolor="#F0F7FD" > <tr> <td><img src="/i/lt-crn.jpg" width="10" height="10"/></td><td width="100%"> <img src="/i/tp.gif" width="100%" height="1"/> </td><td><img src="/i/rt-crn.jpg" width="10" height="10"/></td></tr></table> <table cellspacing="0" cellpadding="2" border="0" width="100%" bgcolor="#F0F7FD" > <tr> <td colspan="2" style="padding-left: 10px"> <table cellspacing="0" cellpadding="0"> <tr> <td class="title-channel" align="left"> <a href="/channels/1plus1/24112021">1+1</a> </td><td class="poll-in-forum"> <a href="/channels/1plus1/gb/">Обговорити</a> <a >(917)</a > </td></tr></table> </td></tr><tr> <td colspan="2" style="padding-left: 10px"> <table cellspacing="0" cellpadding="0"> <tr> <td class="time">06:30</td><td class="item"> <a href="/entertainment/10170/snidanok-z-1-1/" >"Сніданок з 1+1"</a > <img src="/i/stars/3o5.gif" width="56" height="10" valign="absmiddle"/> </td></tr></table> </td></tr><tr> <td colspan="2" style="padding-left: 10px"> <table cellspacing="0" cellpadding="0"> <tr> <td class="time">00:35</td><td class="item">Х/ф "Біля моря"</td></tr></table> </td></tr><tr> <td colspan="2" style="padding-left: 10px"> <table cellspacing="0" cellpadding="0"> <tr> <td class="time">03:00</td><td class="item"> <a href="/entertainment/303513/gudnajtklab/" >"#Гуднайт_клаб"</a > <img src="/i/stars/45o5.gif" width="56" height="10" valign="absmiddle"/> </td></tr></table> </td></tr></table> </td></tr></table> </td></tr></table> </td></tr></table> </td></tr></table> </td></tr></table> </body></html>`
|
||||
const buffer = iconv.encode(Buffer.from(content), 'win1251')
|
||||
const result = parser({ buffer, date }).map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(result).toMatchObject([
|
||||
{
|
||||
start: '2021-11-24T04:30:00.000Z',
|
||||
stop: '2021-11-24T22:35:00.000Z',
|
||||
title: `"Сніданок з 1+1"`
|
||||
},
|
||||
{
|
||||
start: '2021-11-24T22:35:00.000Z',
|
||||
stop: '2021-11-25T01:00:00.000Z',
|
||||
title: `Х/ф "Біля моря"`
|
||||
},
|
||||
{
|
||||
start: '2021-11-25T01:00:00.000Z',
|
||||
stop: '2021-11-25T02:00:00.000Z',
|
||||
title: `"#Гуднайт_клаб"`
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
it('can handle empty guide', () => {
|
||||
const result = parser({
|
||||
date,
|
||||
channel,
|
||||
content: `<!DOCTYPE html><html><head></head><body><textarea data-jtrt-table-id="508" id="jtrt_table_settings_508" cols="30" rows="10"></textarea></body></html>`
|
||||
})
|
||||
expect(result).toMatchObject([])
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue