forked from iptv-org/epg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mncvision.id.config.js
173 lines (147 loc) · 4.72 KB
/
mncvision.id.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const _ = require('lodash')
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)
const languages = { en: 'english', id: 'indonesia' }
const cookies = {}
const timeout = 30000
module.exports = {
site: 'mncvision.id',
days: 2,
url: 'https://www.mncvision.id/schedule/table',
request: {
method: 'POST',
data({ channel, date }) {
const formData = new URLSearchParams()
formData.append('search_model', 'channel')
formData.append('af0rmelement', 'aformelement')
formData.append('fdate', date.format('YYYY-MM-DD'))
formData.append('fchannel', channel.site_id)
formData.append('submit', 'Search')
return formData
},
async headers({ channel }) {
const headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
if (channel) {
if (!cookies[channel.lang]) {
cookies[channel.lang] = await loadLangCookies(channel)
}
if (cookies[channel.lang]) {
headers.Cookie = cookies[channel.lang]
}
}
return headers
},
jar: null
},
async parser({ content, headers, date, channel }) {
const programs = []
if (!cookies[channel.lang]) {
cookies[channel.lang] = parseCookies(headers)
}
const [$, items] = parseItems(content)
for (const item of items) {
const $item = $(item)
const start = parseStart($item, date)
const duration = parseDuration($item)
const stop = start.add(duration, 'm')
const description = await loadDescription($item, cookies[channel.lang])
programs.push({
title: parseTitle($item),
season: parseSeason($item),
episode: parseEpisode($item),
description,
start,
stop
})
}
return programs
},
async channels({ lang = 'id' }) {
const axios = require('axios')
const cheerio = require('cheerio')
const result = await axios
.get('https://www.mncvision.id/schedule')
.then(response => response.data)
.catch(console.error)
const $ = cheerio.load(result)
const items = $('select[name="fchannel"] option').toArray()
const channels = items.map(item => {
const $item = $(item)
return {
lang,
site_id: $item.attr('value'),
name: $item.text().split(' - ')[0].trim()
}
})
return channels
}
}
function parseSeason($item) {
const title = parseTitle($item)
const [, season] = title.match(/ S(\d+)/) || [null, null]
return season ? parseInt(season) : null
}
function parseEpisode($item) {
const title = parseTitle($item)
const [, episode] = title.match(/ Ep (\d+)/) || [null, null]
return episode ? parseInt(episode) : null
}
function parseDuration($item) {
let duration = $item.find('td:nth-child(3)').text()
const match = duration.match(/(\d{2}):(\d{2})/)
const hours = parseInt(match[1])
const minutes = parseInt(match[2])
return hours * 60 + minutes
}
function parseStart($item, date) {
let time = $item.find('td:nth-child(1)').text()
time = `${date.format('DD/MM/YYYY')} ${time}`
return dayjs.tz(time, 'DD/MM/YYYY HH:mm', 'Asia/Jakarta')
}
function parseTitle($item) {
return $item.find('td:nth-child(2) > a').text()
}
function parseItems(content) {
const $ = cheerio.load(content)
return [$, $('tr[valign="top"]').toArray()]
}
function loadLangCookies(channel) {
const url = `https://www.mncvision.id/language_switcher/setlang/${languages[channel.lang]}/`
return axios
.get(url, { timeout })
.then(r => parseCookies(r.headers))
.catch(error => console.error(error.message))
}
async function loadDescription($item, cookies) {
const url = $item.find('a').attr('href')
if (!url) return null
const content = await axios
.get(url, {
headers: { 'X-Requested-With': 'XMLHttpRequest', Cookie: cookies },
timeout
})
.then(r => r.data)
.catch(error => console.error(error.message))
if (!content) return null
const $page = cheerio.load(content)
const description = $page('.synopsis').text().trim()
return description !== '-' ? description : null
}
function parseCookies(headers) {
const cookies = []
if (Array.isArray(headers['set-cookie'])) {
headers['set-cookie'].forEach(cookie => {
cookies.push(cookie.split('; ')[0])
})
}
return cookies.length ? cookies.join('; ') : null
}