-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
mtel.ba.config.js
100 lines (85 loc) · 2.52 KB
/
mtel.ba.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const axios = require('axios')
const dayjs = require('dayjs')
const timezone = require('dayjs/plugin/timezone')
dayjs.extend(timezone)
module.exports = {
site: 'mtel.ba',
days: 2,
url: function ({ channel, date }) {
const [position] = channel.site_id.split('#')
return `https://mtel.ba/oec/epg/program?date=${date.format('YYYY-MM-DD')}&position=${position}`
},
request: {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
},
parser: function ({ content, channel }) {
let programs = []
const items = parseItems(content, channel)
items.forEach(item => {
if (item.title === 'Nema informacija o programu') return
programs.push({
title: item.title,
description: item.description,
category: item.category,
image: item.image,
start: parseStart(item).toJSON(),
stop: parseStop(item).toJSON()
})
})
return programs
},
async channels() {
let channels = []
const totalPages = await getTotalPageCount()
const pages = Array.from(Array(totalPages).keys())
for (let page of pages) {
const data = await axios
.get(`https://mtel.ba/oec/epg/program`, {
params: { page, date: dayjs().format('YYYY-MM-DD') },
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(r => r.data)
.catch(console.log)
data.channels.forEach(item => {
channels.push({
lang: 'bs',
site_id: `${item.position}#${item.id}`,
name: item.name
})
})
}
return channels
}
}
async function getTotalPageCount() {
const data = await axios
.get(`https://mtel.ba/oec/epg/program`, {
params: { page: 0, date: dayjs().format('YYYY-MM-DD') },
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(r => r.data)
.catch(console.log)
return data.total_pages
}
function parseStart(item) {
return dayjs.tz(item.full_start, 'Europe/Sarajevo')
}
function parseStop(item) {
return dayjs.tz(item.full_end, 'Europe/Sarajevo')
}
function parseContent(content, channel) {
const [, channelId] = channel.site_id.split('#')
const data = JSON.parse(content)
if (!data || !Array.isArray(data.channels)) return null
return data.channels.find(i => i.id === channelId)
}
function parseItems(content, channel) {
const data = parseContent(content, channel)
return data ? data.items : []
}