diff --git a/sites/tv.yandex.ru/tv.yandex.ru.config.js b/sites/tv.yandex.ru/tv.yandex.ru.config.js index 77b4543e..29476a85 100644 --- a/sites/tv.yandex.ru/tv.yandex.ru.config.js +++ b/sites/tv.yandex.ru/tv.yandex.ru.config.js @@ -1,5 +1,4 @@ -const jsdom = require('jsdom') -const { JSDOM } = jsdom +const dayjs = require('dayjs') module.exports = { site: 'tv.yandex.ru', @@ -11,33 +10,43 @@ module.exports = { }, url: function ({ date, channel }) { const [region, id] = channel.site_id.split('#') + return `https://tv.yandex.ru/${region}/channel/${id}?date=${date.format('YYYY-MM-DD')}` }, logo: function ({ content }) { - const dom = new JSDOM(content) - const img = dom.window.document.querySelector( - '#mount > div > main > div > div > div.content__header > div > div.channel-header__title > figure > img' - ) + const data = parseContent(content) - return img ? 'https:' + img.src : null + return data ? `https:${data.channel.logo.maxSize.src}` : null }, parser: function ({ content }) { - const initialState = content.match(/window.__INITIAL_STATE__ = (.*);/i) - let programs = [] - if (!initialState && !initialState[1]) return programs - - const data = JSON.parse(initialState[1], null, 2) - if (data.channel) { - programs = data.channel.schedule.events.map(i => { - return { - title: i.title, - description: i.program.description, - start: i.start, - stop: i.finish - } + const programs = [] + const items = parseItems(content) + items.forEach(item => { + programs.push({ + title: item.title, + description: item.program.description, + category: item.program.type.name, + start: dayjs(item.start), + stop: dayjs(item.finish) }) - } + }) 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 +} diff --git a/sites/tv.yandex.ru/tv.yandex.ru.test.js b/sites/tv.yandex.ru/tv.yandex.ru.test.js new file mode 100644 index 00000000..37aceb40 --- /dev/null +++ b/sites/tv.yandex.ru/tv.yandex.ru.test.js @@ -0,0 +1,61 @@ +// npx epg-grabber --config=sites/tv.yandex.ru/tv.yandex.ru.config.js --channels=sites/tv.yandex.ru/tv.yandex.ru_kz.channels.xml --output=.gh-pages/guides/kz/tv.yandex.ru.epg.xml --days=2 + +const { parser, url, request, logo } = require('./tv.yandex.ru.config.js') +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-25', 'YYYY-MM-DD').startOf('d') +const channel = { + site_id: '162#31-kanal-429', + xmltv_id: '31Kanal.kz' +} +const content = ` ` + +it('can generate valid url', () => { + expect(url({ channel, date })).toBe( + 'https://tv.yandex.ru/162/channel/31-kanal-429?date=2021-11-25' + ) +}) + +it('can generate valid request headers', () => { + expect(request.headers).toMatchObject({ + Cookie: + 'yandexuid=8747786251615498142; Expires=Tue, 11 Mar 2031 21:29:02 GMT; Domain=yandex.ru; Path=/' + }) +}) + +it('can generate valid logo url', () => { + expect(logo({ content })).toBe( + 'https://avatars.mds.yandex.net/get-tv-channel-logos/30303/2a00000170e2a3ef22bf7cfa4ff11e8405de/170x100' + ) +}) + +it('can parse response', () => { + const result = parser({ content }).map(p => { + p.start = p.start.toJSON() + p.stop = p.stop.toJSON() + return p + }) + + expect(result).toMatchObject([ + { + start: '2021-11-24T23:00:00.000Z', + stop: '2021-11-24T23:58:00.000Z', + title: `Ризамын (каз.).`, + category: 'досуг', + description: 'kLX6FVKAIiDCGBFE' + } + ]) +}) + +it('can handle empty guide', () => { + const result = parser({ + date, + channel, + content: `` + }) + expect(result).toMatchObject([]) +})