diff --git a/sites/tv.dir.bg/tv.dir.bg.config.js b/sites/tv.dir.bg/tv.dir.bg.config.js new file mode 100644 index 00000000..39f8cd96 --- /dev/null +++ b/sites/tv.dir.bg/tv.dir.bg.config.js @@ -0,0 +1,95 @@ +const axios = require('axios') +const cheerio = require('cheerio') +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) + +module.exports = { + site: 'tv.dir.bg', + url({ channel, date }) { + return `https://tv.dir.bg/tv_channel.php?id=${channel.site_id}&dd=${date.format('DD.MM')}` + }, + parser({ content, date }) { + const programs = [] + const items = parseItems(content) + items.forEach(item => { + const $item = cheerio.load(item) + const prev = programs[programs.length - 1] + let start = parseStart($item, date) + if (!start) return + if (prev) { + if (start.isBefore(prev.start)) { + start = start.add(1, 'd') + date = date.add(1, 'd') + } + prev.stop = start + } + const stop = start.add(30, 'm') + programs.push({ + title: parseTitle($item), + start, + stop + }) + }) + + return programs + }, + async channels() { + const requests = [ + axios.get(`https://tv.dir.bg/programata.php?t=0`), + axios.get(`https://tv.dir.bg/programata.php?t=1`) + ] + + const items = await Promise.all(requests) + .then(r => { + return r + .map(i => { + const html = i.data + const $ = cheerio.load(html) + return $('#programa-left > div > div > div > a').toArray() + }) + .reduce((acc, curr) => { + acc = acc.concat(curr) + return acc + }, []) + }) + .catch(console.log) + + const $ = cheerio.load('') + return items.map(item => { + const $item = $(item) + return { + lang: 'bg', + site_id: $item.attr('href').replace('tv_channel.php?id=', ''), + name: $item.find('div.thumbnail > img').attr('alt'), + logo: $item.find('div.thumbnail > img').data('src') + } + }) + } +} + +function parseStart($item, date) { + const time = $item('i').text() + if (!time) return null + const dateString = `${date.format('MM/DD/YYYY')} ${time}` + + return dayjs.tz(dateString, 'MM/DD/YYYY HH.mm', 'Europe/Sofia') +} + +function parseTitle($item) { + return $item + .text() + .replace(/^\d{2}.\d{2}/, '') + .trim() +} + +function parseItems(content) { + const $ = cheerio.load(content) + + return $('#events > li').toArray() +} diff --git a/sites/tv.dir.bg/tv.dir.bg.test.js b/sites/tv.dir.bg/tv.dir.bg.test.js new file mode 100644 index 00000000..bef02cba --- /dev/null +++ b/sites/tv.dir.bg/tv.dir.bg.test.js @@ -0,0 +1,55 @@ +// node ./scripts/commands/parse-channels.js --config=./sites/tv.dir.bg/tv.dir.bg.config.js --output=./sites/tv.dir.bg/tv.dir.bg_bg.channels.xml +// npx epg-grabber --config=sites/tv.dir.bg/tv.dir.bg.config.js --channels=sites/tv.dir.bg/tv.dir.bg_bg.channels.xml --output=guide.xml --days=2 + +const { parser, url } = require('./tv.dir.bg.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('2022-01-20', 'YYYY-MM-DD').startOf('d') +const channel = { + site_id: '12', + xmltv_id: 'BTV.bg' +} + +it('can generate valid url', () => { + expect(url({ channel, date })).toBe('https://tv.dir.bg/tv_channel.php?id=12&dd=20.01') +}) + +it('can parse response', () => { + const content = `
` + const result = parser({ content, date }).map(p => { + p.start = p.start.toJSON() + p.stop = p.stop.toJSON() + return p + }) + + expect(result).toMatchObject([ + { + start: '2022-01-20T04:00:00.000Z', + stop: '2022-01-20T13:00:00.000Z', + title: `„Тази сутрин” - информационно предаване с водещи Златимир Йочеви Биляна Гавазова` + }, + { + start: '2022-01-20T13:00:00.000Z', + stop: '2022-01-21T03:30:00.000Z', + title: `„Доктор Чудо” - сериал, еп.71` + }, + { + start: '2022-01-21T03:30:00.000Z', + stop: '2022-01-21T04:00:00.000Z', + title: `„Лице в лице” /п./` + } + ]) +}) + +it('can handle empty guide', () => { + const result = parser({ + date, + channel, + content: `
    ` + }) + expect(result).toMatchObject([]) +}) diff --git a/sites/tv.dir.bg/tv.dir.bg_bg.channels.xml b/sites/tv.dir.bg/tv.dir.bg_bg.channels.xml new file mode 100644 index 00000000..4d9770cb --- /dev/null +++ b/sites/tv.dir.bg/tv.dir.bg_bg.channels.xml @@ -0,0 +1,110 @@ + + + + 24 Kitchen Bulgaria + 7/8 TV + Alfa TV + AMC Balkan + Animal Planet Europe + AXN Black + AXN Bulgaria + AXN White + Balkanika Music TV + Bloomberg TV Bulgaria + BNT 1 + BNT 2 + BNT 3 + BNT 4 + Boomerang Central & Eastern Europe + BTV + BTV Action + BTV Cinema + BTV Comedy + BTV Lady + Bulgaria On Air + Cartoon Network Bulgaria + CBS Reality Europe + Cinemax 2 Central Europe + Cinemax Central Europe + CNN International Europe + Crime + Investigation UK + Da Vinci + Diema + Diema Family + Diema Sport + Diema Sport 2 + Diema Sport 3 + Discovery Channel Bulgaria + Discovery Science + Disney Channel Bulgaria + Disney Junior Romania & Bulgaria + DocuBox HD + EKids + Epic Drama + Eurosport 1 + Eurosport 2 North-East + Evrokom + Extreme Sports Channel + FashionTV Europe + Fen Folk + Fen TV + FightBox HD + FilmBox Central Europe + FilmBox Extra HD Adria + FilmBox Plus Adria + Food Network EMEA + Fox Bulgaria + Fox Crime Bulgaria + Fox Life Bulgaria + Fuel TV + HBO 2 Central Europe + HBO 3 Central Europe + HBO Adria + HGTV Pan Regional + History Europe + History Europe + Hobby TV + Investigation Discovery Europe + JimJam Europe + Kino Nova + Love Nature + Max Sport 1 + Max Sport 2 + Max Sport 3 + Max Sport 4 + MCM Top + Mezzo Live HD + MTV Hits UK + National Geographic Bulgaria + National Geographic Wild Bulgaria + Nickelodeon Europe + Nick Jr Central & Eastern Europe + Nova News + Nova Sport + Nova TV + NTV Mir + Okhota i Rybalka + Perviy kanal + Planeta Folk + Planeta HD + POTV + Ring + RM TV + SKAT + Telemedia + TLC Balkan + Trace Sport Stars + Travel Channel Europe + Travel TV + TV 1 + TV 1000 East + TV Evropa + TVT + TV Vraca + UA TV + Viasat Explore East + Viasat History HD + Viasat History HD + Viasat Nature East + +