diff --git a/sites/tvgid.ua/tvgid.ua.config.js b/sites/tvgid.ua/tvgid.ua.config.js index 80f4ed4b..2e5f1ee9 100644 --- a/sites/tvgid.ua/tvgid.ua.config.js +++ b/sites/tvgid.ua/tvgid.ua.config.js @@ -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() } diff --git a/sites/tvgid.ua/tvgid.ua.test.js b/sites/tvgid.ua/tvgid.ua.test.js new file mode 100644 index 00000000..8a68ab72 --- /dev/null +++ b/sites/tvgid.ua/tvgid.ua.test.js @@ -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 = `

Телепрограма 1+1 на 24.11.2021

1+1 Обговорити (917)
06:30 "Сніданок з 1+1" 
00:35Х/ф "Біля моря"
03:00 "#Гуднайт_клаб" 
` + 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: `` + }) + expect(result).toMatchObject([]) +})