-
-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathprogramme-tv.vini.pf.config.js
90 lines (80 loc) · 2.25 KB
/
programme-tv.vini.pf.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
const dayjs = require('dayjs')
const axios = require('axios')
const apiUrl = 'https://programme-tv.vini.pf/programmesJSON'
module.exports = {
site: 'programme-tv.vini.pf',
days: 2,
url: apiUrl,
request: {
method: 'POST',
data({ date }) {
return {
dateDebut: `${date.subtract(10, 'h').format('YYYY-MM-DDTHH:mm:ss')}-10:00`
}
}
},
parser: async function ({ content, channel, date }) {
const programs = []
const items = parseItems(content, channel)
if (items.length) {
for (let hours of [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]) {
const nextContent = await loadNextItems(date, hours)
const nextItems = parseItems(nextContent, channel)
for (let item of nextItems) {
if (!items.find(i => i.nidP === item.nidP)) {
items.push(item)
}
}
}
}
items.forEach(item => {
programs.push({
title: item.titreP,
description: item.desc,
category: item.categorieP,
image: item.srcP,
start: dayjs.unix(item.timestampDeb),
stop: dayjs.unix(item.timestampFin)
})
})
return programs
},
async channels() {
const data = await axios
.post('https://programme-tv.vini.pf/programmesJSON')
.then(r => r.data)
.catch(console.log)
return data.programmes.map(item => {
const site_id = item.url.replace('/', '')
const name = site_id.replace(/-/gi, ' ')
return {
lang: 'fr',
site_id,
name
}
})
}
}
async function loadNextItems(date, hours) {
date = date.add(hours, 'h')
return axios
.post(
apiUrl,
{
dateDebut: `${date.subtract(10, 'h').format('YYYY-MM-DDTHH:mm:ss')}-10:00`
},
{
responseType: 'arraybuffer'
}
)
.then(res => res.data.toString())
.catch(console.log)
}
function parseItems(content, channel) {
if (!content) return []
const data = JSON.parse(content)
if (!data || !Array.isArray(data.programmes)) return []
const channelData = data.programmes.find(i => i.url === `/${channel.site_id}`)
if (!channelData) return []
return channelData.programmes || []
}