From 32fdf396dfba87d014c008e0f44628906be8f8d3 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Fri, 27 Aug 2021 23:27:14 +0300 Subject: [PATCH] Update m.tv.sms.cz.config.js --- sites/m.tv.sms.cz.config.js | 80 ++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/sites/m.tv.sms.cz.config.js b/sites/m.tv.sms.cz.config.js index cef55833..d34a0490 100644 --- a/sites/m.tv.sms.cz.config.js +++ b/sites/m.tv.sms.cz.config.js @@ -3,13 +3,14 @@ const iconv = require('iconv-lite') const { JSDOM } = jsdom const dayjs = require('dayjs') const utc = require('dayjs/plugin/utc') -var customParseFormat = require('dayjs/plugin/customParseFormat') -var timezone = require('dayjs/plugin/timezone') +const timezone = require('dayjs/plugin/timezone') +const customParseFormat = require('dayjs/plugin/customParseFormat') dayjs.extend(utc) -dayjs.extend(customParseFormat) dayjs.extend(timezone) +dayjs.extend(customParseFormat) +let PM = false module.exports = { lang: 'cs', site: 'm.tv.sms.cz', @@ -28,40 +29,53 @@ module.exports = { }, parser: function ({ buffer, date }) { const programs = [] - const string = iconv.decode(buffer, 'win1250') - const dom = new JSDOM(string) - const items = dom.window.document.querySelectorAll('#obsah > div > div.porady > div.porad') + const items = parseItems(buffer) items.forEach((item, i) => { - const time = (item.querySelector('div > span') || { textContent: '' }).textContent - .toString() - .trim() - const title = (item.querySelector('a > div') || { textContent: '' }).textContent - .toString() - .trim() - const description = (item.querySelector('a > div.detail') || { textContent: '' }).textContent - .toString() - .trim() - - if (time && title) { - let local = dayjs.utc(time, 'HH.mm').date(date.date()).month(date.month()).year(date.year()) - - if (local.hour() <= 6 && i > items.length / 2) { - local = local.date(local.date() + 1) - } - const start = dayjs.tz(local.toString(), 'Europe/Prague').toString() - - if (programs.length && !programs[programs.length - 1].stop) { - programs[programs.length - 1].stop = start - } - - programs.push({ - title, - description, - start - }) + const title = parseTitle(item) + const description = parseDescription(item) + const start = parseStart(item, date) + if (start.hour() > 11) PM = true + if (start.hour() < 12 && PM) start = start.add(1, 'd') + const stop = parseStop(item, date) + if (programs.length) { + programs[programs.length - 1].stop = start } + + programs.push({ + title, + description, + start, + stop + }) }) return programs } } + +function parseStop(item, date) { + return date.tz('Europe/Prague').endOf('d').add(6, 'h') +} + +function parseStart(item, date) { + let time = (item.querySelector('div > span') || { textContent: '' }).textContent.trim() + + time = `${date.format('MM/DD/YYYY')} ${time}` + + return dayjs.tz(time, 'MM/DD/YYYY HH.mm', 'Europe/Prague') +} + +function parseDescription(item) { + return (item.querySelector('a > div.detail') || { textContent: '' }).textContent.trim() +} + +function parseTitle(item) { + return (item.querySelector('a > div') || { textContent: '' }).textContent.trim() +} + +function parseItems(buffer) { + const string = iconv.decode(buffer, 'win1250') + const dom = new JSDOM(string) + + return dom.window.document.querySelectorAll('#obsah > div > div.porady > div.porad') +}