Update tv.yandex.ru.config.js

This commit is contained in:
Aleksandr Statciuk 2021-11-25 04:53:59 +03:00
parent 2325ac7a2d
commit e0cf049438

View file

@ -1,5 +1,4 @@
const jsdom = require('jsdom') const dayjs = require('dayjs')
const { JSDOM } = jsdom
module.exports = { module.exports = {
site: 'tv.yandex.ru', site: 'tv.yandex.ru',
@ -11,33 +10,43 @@ module.exports = {
}, },
url: function ({ date, channel }) { url: function ({ date, channel }) {
const [region, id] = channel.site_id.split('#') const [region, id] = channel.site_id.split('#')
return `https://tv.yandex.ru/${region}/channel/${id}?date=${date.format('YYYY-MM-DD')}` return `https://tv.yandex.ru/${region}/channel/${id}?date=${date.format('YYYY-MM-DD')}`
}, },
logo: function ({ content }) { logo: function ({ content }) {
const dom = new JSDOM(content) const data = parseContent(content)
const img = dom.window.document.querySelector(
'#mount > div > main > div > div > div.content__header > div > div.channel-header__title > figure > img'
)
return img ? 'https:' + img.src : null return data ? `https:${data.channel.logo.maxSize.src}` : null
}, },
parser: function ({ content }) { parser: function ({ content }) {
const initialState = content.match(/window.__INITIAL_STATE__ = (.*);/i) const programs = []
let programs = [] const items = parseItems(content)
if (!initialState && !initialState[1]) return programs items.forEach(item => {
programs.push({
const data = JSON.parse(initialState[1], null, 2) title: item.title,
if (data.channel) { description: item.program.description,
programs = data.channel.schedule.events.map(i => { category: item.program.type.name,
return { start: dayjs(item.start),
title: i.title, stop: dayjs(item.finish)
description: i.program.description, })
start: i.start,
stop: i.finish
}
}) })
}
return programs return programs
} }
} }
function parseContent(content) {
const [_, initialState] = content.match(/window.__INITIAL_STATE__ = (.*);/i) || [null, null]
if (!initialState) return null
const data = JSON.parse(initialState)
if (!data) return null
return data.channel
}
function parseItems(content) {
const data = parseContent(content)
if (!data || !data.schedule || !Array.isArray(data.schedule.events)) return []
return data.schedule.events
}