Merge pull request #291 from iptv-org/update-tvgid-ua

Update tvgid.ua
This commit is contained in:
Aleksandr Statciuk 2021-11-24 22:43:58 +03:00 committed by GitHub
commit 7c3c4df585
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 25 deletions

View file

@ -1,6 +1,5 @@
const jsdom = require('jsdom')
const iconv = require('iconv-lite') const iconv = require('iconv-lite')
const { JSDOM } = jsdom const cheerio = require('cheerio')
const dayjs = require('dayjs') const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc') const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone') const timezone = require('dayjs/plugin/timezone')
@ -16,48 +15,47 @@ module.exports = {
return `https://tvgid.ua/channels/${channel.site_id}/${date.format('DDMMYYYY')}/tmall/` return `https://tvgid.ua/channels/${channel.site_id}/${date.format('DDMMYYYY')}/tmall/`
}, },
parser: function ({ buffer, date }) { parser: function ({ buffer, date }) {
let PM = false
const programs = [] const programs = []
const items = parseItems(buffer) const items = parseItems(buffer)
items.forEach(item => { items.forEach(item => {
const title = parseTitle(item) const $item = cheerio.load(item)
let start = parseStart(item, date) const prev = programs[programs.length - 1]
let start = parseStart($item, date)
if (!start) return 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') const stop = start.add(1, 'h')
if (programs.length) { if (prev) {
programs[programs.length - 1].stop = start 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 return programs
} }
} }
function parseStart(item, date) { function parseStart($item, date) {
let time = (item.querySelector('td > table > tbody > tr > td.time') || { textContent: '' }) const timeString = $item('td > table > tbody > tr > td.time').text()
.textContent if (!timeString) return null
if (!time) return null const dateString = `${date.format('MM/DD/YYYY')} ${timeString}`
time = `${date.format('MM/DD/YYYY')} ${time}`
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) { function parseTitle($item) {
return ( return $item('td > table > tbody > tr > td.item').text().trim()
item.querySelector('td > table > tbody > tr > td.item > a') ||
item.querySelector('td > table > tbody > tr > td.item') || { textContent: '' }
).textContent
} }
function parseItems(buffer) { function parseItems(buffer) {
const string = iconv.decode(buffer, 'win1251') if (!buffer) return []
const dom = new JSDOM(string) 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)' '#container > tbody > tr:nth-child(2) > td > table > tbody > tr > td > table:nth-child(2) > tbody > tr:not(:first-child)'
) ).toArray()
} }

View 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>&nbsp;<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/" >&quot;Сніданок з 1+1&quot;</a >&nbsp;<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">Х/ф &quot;Біля моря&quot;</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/" >&quot;#Гуднайт_клаб&quot;</a >&nbsp;<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([])
})