-
Notifications
You must be signed in to change notification settings - Fork 1
/
raiapi.js
257 lines (221 loc) · 7.61 KB
/
raiapi.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Created by massimilianocannarozzo on 13/04/14.
*/
/* eslint-env node */
const ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36';
const baseURL = 'https://www.raiplay.it';
const axios = require('axios').create({
baseURL,
headers: {
'User-Agent': ua,
},
});
const urlRegex = /.*(\/podcast.*)_((,\d+)+).*/;
const moment = require('moment-timezone').tz.setDefault('Europe/Rome');
const mongodb = require('mongodb');
const createError = require('http-errors');
const eNF = createError.NotFound('Dati non disponibili');
const hosts = [
'creativemedia?.rai.it',
'creativemedia?-rai-it.akamaized.net',
'download?.rai.it',
'download?-geo.rai.it',
'creativemediax?.rai.it',
]
const servers = hosts.map(host => [...Array(10).keys()].map(i => host.replace('?', i))).flat();
const {
env: {
MONGO_URL,
},
} = process;
let mongoDb;
if (MONGO_URL) {
mongodb.MongoClient.connect(MONGO_URL, { useUnifiedTopology: true })
.then(c => {
console.log('Connected to mongodb');
return c.db();
})
.then(db => mongoDb = db);
}
let channelMap = {
Rai1: 'rai-1',
Rai2: 'rai-2',
Rai3: 'rai-3',
Rai4: 'rai-4',
'Rai Gulp': 'rai-gulp',
Rai5: 'rai-5',
'Rai Premium': 'rai-premium',
'Rai Yoyo': 'rai-yoyo',
'Rai Movie': 'rai-movie',
'Rai Storia': 'rai-storia',
'Rai Scuola': 'rai-scuola',
'Rai News 24': 'rai-news-24',
'Rai Sport Piu': 'rai-sport-piu-hd',
'Rai Sport': 'rai-sport',
};
const getCanali = () => Object.keys(channelMap);
const getChannelIdentifier = (idCanale) => Object.values(channelMap)[idCanale];
const getDocumentIndex = (idCanale, data) => `${getChannelIdentifier(idCanale)}:${moment(data).format('YYYY:MM:DD')}`;
const getVideoUrl = url => {
return Promise.resolve()
.then(proxy => axios({
proxy,
url,
method: 'HEAD',
headers: {
'User-Agent': 'rai',
},
}))
.then(({ request: { res: { responseUrl: fileUrl } } }) => fileUrl.endsWith('video_no_available.mp4') ? url : fileUrl)
.catch(() => url);
};
const getEffectiveUrl = (url, requestedQuality = Number.MAX_SAFE_INTEGER) => {
return getVideoUrl(url)
.then(fileUrl => {
const matches = fileUrl.match(urlRegex);
if (matches) {
const qualities = matches[2].split(',').filter(Boolean);
const quality = Math.min(requestedQuality, qualities.length - 1);
return Promise.any(
servers.map(server => axios({
method: 'HEAD',
url: `https://${server}${matches[1]}_${qualities[quality]}.mp4`,
}))
)
.then(({ config: { url } }) => url)
.catch(() => fileUrl.replace('http://', 'https://'));
}
return fileUrl.replace('http://', 'https://');
});
};
const fetchPage = (idCanale, data) => {
const canale = getChannelIdentifier([idCanale]);
const m = moment(data);
const url = `/palinsesto/app/${canale}/${m.format('DD-MM-YYYY')}.json`;
if (idCanale > getCanali().length) {
throw createError.BadRequest('Canale non valido');
}
return axios({
url,
})
.then(({ data: body }) => {
const channelData = body['events'];
if (!channelData) {
return Promise.resolve([]);
}
return Promise.allSettled(channelData
.filter(({ has_video }) => has_video === true)
.map(programma => axios({
url: programma['path_id'],
})))
})
.then(programmi => programmi.filter(({ status }) => status === 'fulfilled').map(({ value: { data } }) => data))
.then(programmi => !mongoDb
? Promise.resolve(programmi)
: mongoDb.collection('programmi')
.updateOne(
{ _id: getDocumentIndex(idCanale, data) },
{
$set: {
programmi,
createdAt: new Date(),
}
},
{ upsert: true }
)
.then(() => programmi)
)
};
const getData = (idCanale, data) => {
return !mongoDb
? fetchPage(idCanale, data)
: mongoDb.collection('programmi')
.findOne({ _id: getDocumentIndex(idCanale, data) }, { projection: { _id: false, createdAt: false } })
.then(d => d ? Object.values(d.programmi) : fetchPage(idCanale, data));
}
class RaiApi {
getAll(idCanale, data) {
return getData(idCanale, data)
.then(programmi => {
if (programmi.length === 0) {
return [];
}
return Promise.all(programmi
.map(({ name, time_published: orario, weblink: url }) => ({
name,
orario,
url: `${baseURL}${url}`,
})
))
})
}
getFileUrl(idCanale, data, idProgramma, quality) {
return getData(idCanale, data)
.then(programmi => {
if (programmi.length === 0) {
throw eNF;
}
const programma = programmi[idProgramma];
if (!programma) {
throw eNF;
}
if (!programma.video) {
throw eNF;
}
const url = programma.video['content_url'];
if (!url) {
throw eNF;
}
return getEffectiveUrl(url, quality);
});
}
listQualita(idCanale, data, idProgramma) {
return getData(idCanale, data)
.then(programmi => {
if (programmi.length === 0) {
throw eNF;
}
const programma = programmi[idProgramma];
if (!programma) {
throw eNF;
}
if (!programma.video) {
throw eNF;
}
const url = programma.video['content_url'];
if (!url) {
throw eNF;
}
return getVideoUrl(url)
.then(fileUrl => {
const matches = fileUrl.match(urlRegex);
const qualities = matches ? matches[2].split(',').filter(Boolean) : ['1800'];
return qualities.map((quality, id) => ({
id,
name: `h264 ${quality}`
}));
});
});
}
listProgrammi(idCanale, data) {
return getData(idCanale, data)
.then (programmi => {
if (programmi.length === 0) {
throw eNF;
}
return programmi.map(({ name = '-', description, images }, i) => ({
id: i,
name: name.trim(),
image: images['landscape'] ? `https://www.raiplay.it${images.landscape}` : undefined,
description,
}));
});
}
static listCanali() {
return Promise.resolve(getCanali().map((name, id) => ({
id: id,
name: name,
})));
}
}
module.exports = RaiApi;