mirror of
https://github.com/iptv-org/epg
synced 2025-12-16 10:26:41 -05:00
move html content to specific folder, remove yarn.lock
This commit is contained in:
@@ -1,28 +1,80 @@
|
|||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const cheerio = require('cheerio')
|
const cheerio = require('cheerio')
|
||||||
|
const url = require('url')
|
||||||
const { DateTime } = require('luxon')
|
const { DateTime } = require('luxon')
|
||||||
|
|
||||||
|
let cachedToken = null
|
||||||
|
let tokenExpiry = null
|
||||||
|
|
||||||
|
|
||||||
|
async function getToken() {
|
||||||
|
if (cachedToken && tokenExpiry && DateTime.now() < tokenExpiry) {
|
||||||
|
return cachedToken
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axios.get('https://tv.dir.bg/init')
|
||||||
|
|
||||||
|
// Check different possible locations for the token
|
||||||
|
let token = null
|
||||||
|
if (response.data && response.data.csrfToken) {
|
||||||
|
token = response.data.csrfToken
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token) {
|
||||||
|
cachedToken = token
|
||||||
|
tokenExpiry = DateTime.now().plus({ hours: 1 })
|
||||||
|
return token
|
||||||
|
} else {
|
||||||
|
console.error('CSRF token not found in response structure:', Object.keys(response.data || {}))
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching token:', error.message)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
site: 'tv.dir.bg',
|
site: 'tv.dir.bg',
|
||||||
days: 2,
|
days: 2,
|
||||||
url({ channel, date }) {
|
async url({ channel, date }) {
|
||||||
return `https://tv.dir.bg/tv_channel.php?id=${channel.site_id}&dd=${date.format('DD.MM')}`
|
const token = await getToken()
|
||||||
|
if (!token) {
|
||||||
|
throw new Error('Unable to retrieve CSRF token')
|
||||||
|
}
|
||||||
|
|
||||||
|
const form = new url.URLSearchParams({
|
||||||
|
_token: token,
|
||||||
|
channel: channel.site_id,
|
||||||
|
day: date.format('YYYY-MM-DD')
|
||||||
|
})
|
||||||
|
|
||||||
|
return axios.post('https://tv.dir.bg/load/programs', form.toString(), {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
||||||
|
'X-Requested-With': 'XMLHttpRequest'
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
parser({ content, date }) {
|
parser({ content, date }) {
|
||||||
const programs = []
|
const programs = []
|
||||||
const items = parseItems(content)
|
const items = parseItems(content)
|
||||||
|
|
||||||
items.forEach(item => {
|
items.forEach(item => {
|
||||||
const $item = cheerio.load(item)
|
const $item = cheerio.load(item)
|
||||||
const prev = programs[programs.length - 1]
|
const prev = programs[programs.length - 1]
|
||||||
let start = parseStart($item, date)
|
let start = parseStart($item, date)
|
||||||
if (!start) return
|
if (!start) return
|
||||||
|
|
||||||
if (prev) {
|
if (prev) {
|
||||||
if (start < prev.start) {
|
if (start < prev.start) {
|
||||||
start = start.plus({ days: 1 })
|
start = start.plus({ days: 1 })
|
||||||
date = date.add(1, 'd')
|
date = date.plus({ days: 1 })
|
||||||
}
|
}
|
||||||
prev.stop = start
|
prev.stop = start
|
||||||
}
|
}
|
||||||
|
|
||||||
const stop = start.plus({ minutes: 30 })
|
const stop = start.plus({ minutes: 30 })
|
||||||
programs.push({
|
programs.push({
|
||||||
title: parseTitle($item),
|
title: parseTitle($item),
|
||||||
@@ -34,55 +86,57 @@ module.exports = {
|
|||||||
return programs
|
return programs
|
||||||
},
|
},
|
||||||
async channels() {
|
async channels() {
|
||||||
const requests = [
|
try {
|
||||||
axios.get('https://tv.dir.bg/programata.php?t=0'),
|
const response = await axios.get('https://tv.dir.bg/channels')
|
||||||
axios.get('https://tv.dir.bg/programata.php?t=1')
|
const $ = cheerio.load(response.data)
|
||||||
]
|
|
||||||
|
const channels = []
|
||||||
const items = await Promise.all(requests)
|
|
||||||
.then(r => {
|
$('.channel_cont').each((index, element) => {
|
||||||
return r
|
const $element = $(element)
|
||||||
.map(i => {
|
|
||||||
const html = i.data
|
const $link = $element.find('a.channel_link')
|
||||||
const $ = cheerio.load(html)
|
const href = $link.attr('href')
|
||||||
return $('#programa-left > div > div > div > a').toArray()
|
|
||||||
|
const $img = $element.find('img')
|
||||||
|
const name = $img.attr('alt')
|
||||||
|
const logo = $img.attr('src')
|
||||||
|
|
||||||
|
const site_id = href ? href.match(/\/programa\/(\d+)/)?.[1] : ''
|
||||||
|
|
||||||
|
if (site_id && name) {
|
||||||
|
channels.push({
|
||||||
|
lang: 'bg',
|
||||||
|
site_id: site_id,
|
||||||
|
name: name,
|
||||||
|
logo: logo
|
||||||
})
|
})
|
||||||
.reduce((acc, curr) => {
|
}
|
||||||
acc = acc.concat(curr)
|
|
||||||
return acc
|
|
||||||
}, [])
|
|
||||||
})
|
})
|
||||||
.catch(console.log)
|
|
||||||
|
return channels
|
||||||
const $ = cheerio.load('')
|
|
||||||
return items.map(item => {
|
} catch (error) {
|
||||||
const $item = $(item)
|
console.error('Error fetching channels:', error)
|
||||||
return {
|
return []
|
||||||
lang: 'bg',
|
|
||||||
site_id: $item.attr('href').replace('tv_channel.php?id=', ''),
|
|
||||||
name: $item.find('div.thumbnail > img').attr('alt')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function parseStart($item, date) {
|
function parseStart($item, date) {
|
||||||
const time = $item('i').text()
|
const timeText = $item('.broadcast-time').text().trim()
|
||||||
if (!time) return null
|
if (!timeText) return null
|
||||||
const dateString = `${date.format('MM/DD/YYYY')} ${time}`
|
|
||||||
|
const [hours, minutes] = timeText.split(':').map(Number)
|
||||||
return DateTime.fromFormat(dateString, 'MM/dd/yyyy HH.mm', { zone: 'Europe/Sofia' }).toUTC()
|
const dateTime = date.isValid ? date : DateTime.fromISO(date)
|
||||||
|
return dateTime.set({ hour: hours, minute: minutes, second: 0, millisecond: 0 })
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseTitle($item) {
|
function parseTitle($item) {
|
||||||
return $item
|
return $item('.broadcast-title').text().trim()
|
||||||
.text()
|
|
||||||
.replace(/^\d{2}.\d{2}/, '')
|
|
||||||
.trim()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseItems(content) {
|
function parseItems(content) {
|
||||||
const $ = cheerio.load(content)
|
const $ = cheerio.load(content)
|
||||||
|
return $('.broadcast-item').toArray()
|
||||||
return $('#events > li').toArray()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
const { parser, url } = require('./tv.dir.bg.config.js')
|
const { parser, url } = require('./tv.dir.bg.config.js')
|
||||||
|
const fs = require('fs')
|
||||||
|
const path = require('path')
|
||||||
const dayjs = require('dayjs')
|
const dayjs = require('dayjs')
|
||||||
const utc = require('dayjs/plugin/utc')
|
const utc = require('dayjs/plugin/utc')
|
||||||
const customParseFormat = require('dayjs/plugin/customParseFormat')
|
const customParseFormat = require('dayjs/plugin/customParseFormat')
|
||||||
dayjs.extend(customParseFormat)
|
dayjs.extend(customParseFormat)
|
||||||
dayjs.extend(utc)
|
dayjs.extend(utc)
|
||||||
|
|
||||||
const date = dayjs.utc('2022-01-20', 'YYYY-MM-DD').startOf('d')
|
const date = dayjs.utc('2025-06-30', 'YYYY-MM-DD').startOf('d')
|
||||||
const channel = {
|
const channel = {
|
||||||
site_id: '12',
|
site_id: '12',
|
||||||
xmltv_id: 'BTV.bg'
|
xmltv_id: 'BTV.bg'
|
||||||
}
|
}
|
||||||
|
|
||||||
it('can generate valid url', () => {
|
it('can generate valid url', () => {
|
||||||
expect(url({ channel, date })).toBe('https://tv.dir.bg/tv_channel.php?id=12&dd=20.01')
|
expect(url({ channel, date })).toBe('https://tv.dir.bg/programa/12')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can parse response', () => {
|
it('can parse response', () => {
|
||||||
const content =
|
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content.html'))
|
||||||
'<!DOCTYPE html><html><head></head><body><div class="container" id="news"><div class="row"><div class="col-sm-12 col-md-5"><ul id="events"><li><i></i> <div class="progress"> <div class="progress-bar progress-bar-striped active" role="progressbar" style="width:99%"> </div></div></li><li><a href="tv_show_info.php?id"="10"><i>06.00</i>„<b>Тази сутрин</b>” - информационно предаване с водещи Златимир Йочеви Биляна Гавазова</a></li><li><i>15.00</i>„Доктор Чудо” - сериал, еп.71</li><li><a href="tv_show_info.php?id"="1601"><i>05.30</i>„<b>Лице в лице</b>” /п./ </a></li></ul></div></div></div></body></html>'
|
|
||||||
const result = parser({ content, date }).map(p => {
|
const result = parser({ content, date }).map(p => {
|
||||||
p.start = p.start.toJSON()
|
p.start = p.start.toJSON()
|
||||||
p.stop = p.stop.toJSON()
|
p.stop = p.stop.toJSON()
|
||||||
@@ -26,19 +27,19 @@ it('can parse response', () => {
|
|||||||
|
|
||||||
expect(result).toMatchObject([
|
expect(result).toMatchObject([
|
||||||
{
|
{
|
||||||
start: '2022-01-20T04:00:00.000Z',
|
start: '2025-06-30T08:00:00.000Z',
|
||||||
stop: '2022-01-20T13:00:00.000Z',
|
stop: '2025-06-30T10:00:00.000Z',
|
||||||
title: '„Тази сутрин” - информационно предаване с водещи Златимир Йочеви Биляна Гавазова'
|
title: 'Купа на Франция: Еспали - Пари Сен Жермен'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
start: '2022-01-20T13:00:00.000Z',
|
start: '2025-06-30T10:00:00.000Z',
|
||||||
stop: '2022-01-21T03:30:00.000Z',
|
stop: '2025-06-30T12:00:00.000Z',
|
||||||
title: '„Доктор Чудо” - сериал, еп.71'
|
title: 'Ла Лига: Леганес - Реал Сосиедад'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
start: '2022-01-21T03:30:00.000Z',
|
start: '2025-06-30T12:00:00.000Z',
|
||||||
stop: '2022-01-21T04:00:00.000Z',
|
stop: '2025-06-30T13:00:00.000Z',
|
||||||
title: '„Лице в лице” /п./'
|
title: 'Пред Стадиона" - спортно шоу'
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
@@ -47,8 +48,7 @@ it('can handle empty guide', () => {
|
|||||||
const result = parser({
|
const result = parser({
|
||||||
date,
|
date,
|
||||||
channel,
|
channel,
|
||||||
content:
|
content: fs.readFileSync(path.resolve(__dirname, '__data__/no_data.html'))
|
||||||
'<!DOCTYPE html><html><head></head><body><div class="container" id="news"><div class="row"><div class="col-sm-12 col-md-5"><ul id="events"></ul></div></div></div></body></html>'
|
|
||||||
})
|
})
|
||||||
expect(result).toMatchObject([])
|
expect(result).toMatchObject([])
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user