This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
160 lines (141 loc) · 3.85 KB
/
index.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
'use strict';
const jsonist = require('jsonist');
const nrkTvApi = process.env.NRK_TV_API || 'https://tv.nrk.no';
const nrkPsApi = process.env.NRK_PS_API || 'http://v8.psapi.nrk.no';
const nrkTvMobilApi = process.env.NRK_TV_MOBIL || 'https://tvapi.nrk.no/v1';
module.exports.headers = function headers(agent) {
if (agent === 'app') {
return {
headers: {
'User-Agent': 'NRK%20TV/43 CFNetwork/711.5.6 Darwin/14.0.0',
accept: '*/*',
'app-version-ios': '43',
'Accept-Language': 'en-us',
},
};
}
return {
headers: {
'user-agent': [
'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X)',
'AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0',
'Mobile/10A5376e Safari/8536.25',
].join(' '),
accept: 'application/json, text/javascript, */*; q=0.01',
'x-requested-with': 'XMLHttpRequest',
},
};
};
module.exports.tv = {};
module.exports.tv.mobil = {};
/**
* Get TV categories
*
* @param cb - callback function (err, data, res)
*
* @return Array of categories
*/
module.exports.tv.mobil.categories = function categories(cb) {
const url = `${nrkTvMobilApi}/categories`;
jsonist.get(url, module.exports.headers('app'), cb);
};
/**
* Get one ore more TV program
*
* @param pid - program id
* @param cb - callback function (err, data, res)
*
* @return Array of programs
*/
module.exports.tv.mobil.programs = function programs(pid, cb) {
let url;
if (!pid) {
url = `${nrkTvMobilApi}/programs`;
} else {
url = `${nrkTvMobilApi}/programs/${pid}`;
}
jsonist.get(url, module.exports.headers('app'), cb);
};
/**
* Get one ore more TV series
*
* @param sid - series id
* @param cb - callback function (err, data, res)
*
* @return Array of series
*/
module.exports.tv.mobil.series = function series(sid, cb) {
let url;
if (!sid) {
url = `${nrkTvMobilApi}/series`;
} else {
url = `${nrkTvMobilApi}/series/${sid}`;
}
jsonist.get(url, module.exports.headers('app'), cb);
};
/**
* Search for TV programs
*
* @param str - string to search for
* @param cb - callback function (err, data, res)
*
* @return Array of matching programs
*/
module.exports.tv.mobil.search = function search(str, cb) {
const url = `${nrkTvMobilApi}/search/${encodeURIComponent(str)}`;
jsonist.get(url, module.exports.headers('app'), cb);
};
/**
* Get programs matching string (autocomplete)
*
* @param str - string to autcomplete
* @param cb - callback function (err, data, res)
*
* @return Array of matching programs
*/
module.exports.tv.autocomplete = function autocomplete(str, cb) {
const url = `${nrkTvApi}/autocomplete?query=${str}`;
jsonist.get(url, module.exports.headers(), cb);
};
/**
* Get all programs starting with a given letter
*
* @param letter - letter to filter programs ("a"…"z" + "0-9")
* @param category - optionaly category name
* @param cb - callback function (err, data, res)
*
* @return Array of matching programs
*/
module.exports.tv.programs = function programs(letter, category, cb) {
let url;
if (category) {
url = `${nrkTvApi}/programmer/${category}/${letter}`;
} else {
url = `${nrkTvApi}/programmer/${letter}`;
}
jsonist.get(url, module.exports.headers(), cb);
};
/**
* Get program / episode details by ID
*
* @param pid - program id
* @param cb - callback function (err, data, res)
*
* @return Object with details
*/
module.exports.tv.program = function program(pid, cb) {
const url = `${nrkPsApi}/mediaelement/${pid}`;
jsonist.get(url, module.exports.headers(), cb);
};
/**
* Get series details by ID
*
* @param sid - series id
* @param cb - callback function (err, data, res)
*
* @return Object with details
*/
module.exports.tv.series = function series(sid, cb) {
const url = `${nrkPsApi}/series/${sid}/latestornextepisode/mediaelement`;
jsonist.get(url, module.exports.headers(), cb);
};