From 1c9ed540438ce7afaa16b0d075c78ea8fe51e22c Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Wed, 24 Nov 2021 20:50:04 +0300 Subject: [PATCH 1/2] Create m.tv.sms.cz.test.js --- sites/m.tv.sms.cz/m.tv.sms.cz.test.js | 80 +++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 sites/m.tv.sms.cz/m.tv.sms.cz.test.js diff --git a/sites/m.tv.sms.cz/m.tv.sms.cz.test.js b/sites/m.tv.sms.cz/m.tv.sms.cz.test.js new file mode 100644 index 00000000..ea421258 --- /dev/null +++ b/sites/m.tv.sms.cz/m.tv.sms.cz.test.js @@ -0,0 +1,80 @@ +// npx epg-grabber --config=sites/m.tv.sms.cz/m.tv.sms.cz.config.js --channels=sites/m.tv.sms.cz/m.tv.sms.cz_cz.channels.xml --output=.gh-pages/guides/cz/m.tv.sms.cz.epg.xml --days=2 + +const { parser, url, logo } = require('./m.tv.sms.cz.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: 'Cero', + xmltv_id: '0.es' +} +const content = `
` +const buffer = iconv.encode(Buffer.from(content), 'win1250') + +it('can generate valid url', () => { + expect(url({ channel, date })).toBe( + 'https://m.tv.sms.cz/index.php?stanice=Cero&cas=0&den=2021-11-24' + ) +}) + +it('can generate valid logo url', () => { + expect(logo({ content })).toBe('https://www.sms.cz/kategorie/televize/bmp/loga/velka/cero.png') +}) + +it('can parse response', () => { + 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:02:00.000Z', + stop: '2021-11-24T04:56:00.000Z', + title: `La magia de la Luna: El octavo continente`, + description: 'Documentales' + }, + { + start: '2021-11-24T04:56:00.000Z', + stop: '2021-11-24T22:00:00.000Z', + title: `Explorando Europa: El nacimiento de un continente`, + description: 'Documentales' + }, + { + start: '2021-11-24T22:00:00.000Z', + stop: '2021-11-24T23:05:00.000Z', + title: `Late Motiv (41)`, + description: 'Entretenimiento' + }, + { + start: '2021-11-24T23:05:00.000Z', + stop: '2021-11-25T00:20:00.000Z', + title: `La Resistencia (41)`, + description: 'Entretenimiento' + }, + { + start: '2021-11-25T00:20:00.000Z', + stop: '2021-11-25T01:20:00.000Z', + title: `Ilustres Ignorantes: Cantantes`, + description: 'Entretenimiento' + } + ]) +}) + +it('can handle empty guide', () => { + const result = parser({ + buffer: iconv.encode( + Buffer.from( + `` + ), + 'win1250' + ) + }) + expect(result).toMatchObject([]) +}) From a29ebc926148b23328c6592a9fc2adaafe4632ea Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Wed, 24 Nov 2021 20:50:11 +0300 Subject: [PATCH 2/2] Update m.tv.sms.cz.config.js --- sites/m.tv.sms.cz/m.tv.sms.cz.config.js | 54 ++++++++++++------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/sites/m.tv.sms.cz/m.tv.sms.cz.config.js b/sites/m.tv.sms.cz/m.tv.sms.cz.config.js index b8f77276..203f0dea 100644 --- a/sites/m.tv.sms.cz/m.tv.sms.cz.config.js +++ b/sites/m.tv.sms.cz/m.tv.sms.cz.config.js @@ -1,6 +1,5 @@ -const jsdom = require('jsdom') +const cheerio = require('cheerio') const iconv = require('iconv-lite') -const { JSDOM } = jsdom const dayjs = require('dayjs') const utc = require('dayjs/plugin/utc') const timezone = require('dayjs/plugin/timezone') @@ -18,29 +17,29 @@ module.exports = { )}` }, logo: function ({ content }) { - const dom = new JSDOM(content) - const img = dom.window.document.querySelector('.logo_out > img') + const $ = cheerio.load(content) + const imgSrc = $('.logo_out > img').attr('src') - return img ? img.src : null + return imgSrc ? `https:${imgSrc}` : null }, parser: function ({ buffer, date }) { - let PM = false const programs = [] const items = parseItems(buffer) items.forEach((item, i) => { - const title = parseTitle(item) - const description = parseDescription(item) - let start = parseStart(item, date) - 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 + const prev = programs[programs.length - 1] + const $item = cheerio.load(item) + let start = parseStart($item, date) + if (prev) { + if (start.isBefore(prev.start)) { + start = start.add(1, 'd') + date = date.add(1, 'd') + } + prev.stop = start } - + const stop = start.add(1, 'h') programs.push({ - title, - description, + title: parseTitle($item), + description: parseDescription($item), start, stop }) @@ -50,25 +49,24 @@ module.exports = { } } -function parseStart(item, date) { - let time = (item.querySelector('div > span') || { textContent: '' }).textContent.trim() +function parseStart($item, date) { + const timeString = $item('div > span').text().trim() + 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/Prague') + return dayjs.tz(dateString, 'MM/DD/YYYY HH.mm', 'Europe/Prague') } -function parseDescription(item) { - return (item.querySelector('a > div.detail') || { textContent: '' }).textContent.trim() +function parseDescription($item) { + return $item('a.nazev > div.detail').text().trim() } -function parseTitle(item) { - return (item.querySelector('a > div') || { textContent: '' }).textContent.trim() +function parseTitle($item) { + return $item('a.nazev > div:nth-child(1)').text().trim() } function parseItems(buffer) { const string = iconv.decode(buffer, 'win1250') - const dom = new JSDOM(string) + const $ = cheerio.load(string) - return dom.window.document.querySelectorAll('#obsah > div > div.porady > div.porad') + return $('#obsah > div > div.porady > div.porad').toArray() }