Skip to content

Commit af932a9

Browse files
freearheytohenk
authored andcommitted
Update tvarenasport.hr.config.js
Move code from tvarenasport.com/tvarenasport.com.config.js
1 parent 7237a62 commit af932a9

File tree

1 file changed

+128
-4
lines changed

1 file changed

+128
-4
lines changed
Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,132 @@
1-
const tvarenasport = require('../tvarenasport.com/tvarenasport.com.config')
1+
const cheerio = require('cheerio')
2+
const axios = require('axios')
3+
const dayjs = require('dayjs')
4+
const utc = require('dayjs/plugin/utc')
5+
const timezone = require('dayjs/plugin/timezone')
6+
const customParseFormat = require('dayjs/plugin/customParseFormat')
27

3-
module.exports = Object.assign(tvarenasport, {
8+
dayjs.extend(utc)
9+
dayjs.extend(timezone)
10+
dayjs.extend(customParseFormat)
11+
12+
module.exports = {
413
site: 'tvarenasport.hr',
514
tz: 'Europe/Budapest',
615
lang: 'hr',
7-
url: 'https://tvarenaprogram.com/live/v2/hr',
8-
})
16+
url: 'https://tvarenaprogram.com/live/v2/hr',
17+
days: 2,
18+
request: {
19+
cache: {
20+
ttl: 24 * 60 * 60 * 1000 // 1 day
21+
}
22+
},
23+
parser({ content, channel, date }) {
24+
const programs = []
25+
const expectedDate = date.format('YYYY-MM-DD')
26+
if (content) {
27+
const dates = []
28+
const $ = cheerio.load(content)
29+
const parent = $(
30+
`.tv-scheme-chanel-header img[src*="chanel-${channel.site_id}.png"]`
31+
).parents('div')
32+
parent
33+
.siblings('.tv-scheme-days')
34+
.find('a')
35+
.toArray()
36+
.forEach(el => {
37+
const a = $(el)
38+
const dt = a.find('span:nth-child(3)').text()
39+
dates.push(dayjs(dt + date.year(), 'DD.MM.YYYY'))
40+
})
41+
parent
42+
.siblings('.tv-scheme-new-slider-wrapper')
43+
.find('.tv-scheme-new-slider-item')
44+
.toArray()
45+
.forEach((el, i) => {
46+
programs.push(...parseSchedules($(el), dates[i], module.exports.tz))
47+
})
48+
programs.forEach((s, i) => {
49+
if (i < programs.length - 2) {
50+
s.stop = programs[i + 1].start
51+
} else {
52+
s.stop = s.start.startOf('d').add(1, 'd')
53+
}
54+
})
55+
}
56+
57+
return programs.filter(
58+
p =>
59+
p.start.format('YYYY-MM-DD') === expectedDate ||
60+
p.stop.format('YYYY-MM-DD') === expectedDate
61+
)
62+
},
63+
async channels() {
64+
const channels = []
65+
const data = await axios
66+
.get(this.url)
67+
.then(r => r.data)
68+
.catch(console.error)
69+
70+
if (data) {
71+
// channel naming rule
72+
const names = id => {
73+
let match = id.match(/^\d+$/)
74+
if (match) {
75+
return `Arena Sport ${parseInt(id)}`
76+
}
77+
match = id.match(/^\d/)
78+
if (match) {
79+
return `Arena Sport ${id}`
80+
}
81+
match = id.match(/^a(\d+)(p)?/)
82+
if (match) {
83+
return `Arena ${parseInt(match[1])}${match[2] === 'p' ? ' Premium' : ''}`
84+
}
85+
return `Arena ${id}`
86+
}
87+
const $ = cheerio.load(data)
88+
const items = $('.tv-scheme-chanel-header img').toArray()
89+
for (const item of items) {
90+
const [, id] = $(item)
91+
.attr('src')
92+
.match(/chanel-([a-z0-9]+)\.png/) || [null, null]
93+
if (id) {
94+
channels.push({
95+
lang: this.lang,
96+
site_id: id,
97+
name: names(id)
98+
})
99+
}
100+
}
101+
}
102+
103+
return channels
104+
}
105+
}
106+
107+
function parseSchedules($s, date, tz) {
108+
const schedules = []
109+
const $ = $s._make
110+
$s.find('.slider-content')
111+
.toArray()
112+
.forEach(el => {
113+
schedules.push(parseSchedule($(el), date, tz))
114+
})
115+
116+
return schedules
117+
}
118+
119+
function parseSchedule($s, date, tz) {
120+
const time = $s.find('.slider-content-top span').text()
121+
const start = dayjs.tz(`${date.format('YYYY-MM-DD')} ${time}`, 'YYYY-MM-DD HH:mm', tz)
122+
const category = $s.find('.slider-content-middle span').text()
123+
const title = $s.find('.slider-content-bottom p').text()
124+
const description = $s.find('.slider-content-bottom span:first').text()
125+
126+
return {
127+
title: description ? description : title,
128+
description: description ? title : description,
129+
category,
130+
start
131+
}
132+
}

0 commit comments

Comments
 (0)