-
-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathtv.nu.config.js
87 lines (76 loc) · 2.37 KB
/
tv.nu.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
const dayjs = require('dayjs')
module.exports = {
site: 'tv.nu',
days: 2,
url: function ({ channel, date }) {
return `https://web-api.tv.nu/channels/${channel.site_id}/schedule?date=${date.format(
'YYYY-MM-DD'
)}&fullDay=true`
},
parser: function ({ content }) {
let programs = []
const items = parseItems(content)
items.forEach(item => {
programs.push({
title: item.title,
description: item.description,
category: Array.isArray(item.genres) ? item.genres.map(genre => genre.name) : null,
season: item.seasonNumber || null,
episode: item.episodeNumber || null,
start: parseStart(item),
stop: parseStop(item)
})
})
return programs
},
async channels() {
const channels = []
const axios = require('axios')
const result = await axios
.get('https://www.tv.nu/alla-kanaler')
.then(response => response.data)
.catch(console.error)
if (result) {
const [, data] = result.match(/\\"allModules\\":(\[(.*?)\])/i) || [null, null]
const modules = JSON.parse(data.replace(/\\/g, ''))
if (Array.isArray(modules) && modules.length) {
let offset = 0
while (offset !== undefined) {
const data = await axios
.get('https://web-api.tv.nu/tableauLinearChannels', {
params: {
modules,
date: dayjs().format('YYYY-MM-DD'),
limit: 12,
offset
}
})
.then(r => r.data)
.catch(console.error)
data.data.modules.forEach(item => {
channels.push({
lang: 'sv',
name: item.content.name,
site_id: item.content.slug
})
})
offset = data.data.nextOffset
}
}
}
return channels
}
}
function parseStart(item) {
if (!item.broadcast || !item.broadcast.startTime) return null
return dayjs(item.broadcast.startTime)
}
function parseStop(item) {
if (!item.broadcast || !item.broadcast.endTime) return null
return dayjs(item.broadcast.endTime)
}
function parseItems(content) {
const data = JSON.parse(content)
if (!data || !data.data || !Array.isArray(data.data.broadcasts)) return []
return data.data.broadcasts
}