Update mi.tv.config.js

This commit is contained in:
Aleksandr Statciuk 2021-08-27 20:26:11 +03:00
parent 22b197d3f4
commit a6be3a5ad4

View file

@ -2,11 +2,14 @@ const jsdom = require('jsdom')
const { JSDOM } = jsdom
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
const customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(customParseFormat)
let PM = false
module.exports = {
lang: 'pt',
site: 'mi.tv',
@ -23,32 +26,48 @@ module.exports = {
},
parser({ content, date }) {
const programs = []
const dom = new JSDOM(content)
const items = dom.window.document.querySelectorAll('#listings > ul > li')
const items = parseItems(content)
items.forEach(item => {
const title = (item.querySelector('a > div.content > h2') || { textContent: '' }).textContent
const time = (item.querySelector('a > div.content > span.time') || { textContent: '' })
.textContent
if (title && time) {
const start = dayjs
.utc(time, 'HH:mm')
.set('D', date.get('D'))
.set('M', date.get('M'))
.set('y', date.get('y'))
if (programs.length && !programs[programs.length - 1].stop) {
programs[programs.length - 1].stop = start
}
programs.push({
title,
start
})
const title = parseTitle(item)
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 = parseStop(item, start)
if (programs.length) {
programs[programs.length - 1].stop = start
}
programs.push({
title,
start,
stop
})
})
return programs
}
}
function parseStop(item, date) {
return date.endOf('d').add(6, 'h')
}
function parseStart(item, date) {
let time = (item.querySelector('a > div.content > span.time') || { textContent: '' }).textContent
if (!time) return null
time = `${date.format('MM/DD/YYYY')} ${time}`
return dayjs.tz(time, 'MM/DD/YYYY HH:mm', 'America/Sao_Paulo')
}
function parseTitle(item) {
return (item.querySelector('a > div.content > h2') || { textContent: '' }).textContent
}
function parseItems(content) {
const dom = new JSDOM(content)
return dom.window.document.querySelectorAll('#listings > ul > li')
}