-
Notifications
You must be signed in to change notification settings - Fork 3
/
nitroCategories.js
93 lines (82 loc) · 2.19 KB
/
nitroCategories.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
/*
Lists Nitro categories, formats (known to the BBC as aggregations)
*/
const http = require('http');
const https = require('https');
function category_dump(obj) {
console.log('# Category list dump');
var first = true;
for (var i in obj.results) {
c = obj.results[i];
console.log(c.id+' '+c.type+' '+c.pip_id+' = '+c.title);
if (c.category_type == 'genre' || c.category_type == 'format') {
for (var j in c.child_categories) {
n = c.child_categories[j];
console.log(' '+n.id+' '+n.category_type+' '+c.pip_id+'/'+n.pip_id+' = '+n.title);
if (j.narrower && j.narrower.length>0) {
console.log('Recursive');
}
}
}
else {
console.log('?? '+c.type);
}
}
}
function list_categories(path) {
var options = {
hostname: 'rms.api.bbc.co.uk'
,port: 443
,path: path
,method: 'GET'
,headers: { 'Content-Type': 'application/json' }
};
var list = '';
var cat;
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (data) {
list += data;
});
res.on('end', function() {
try {
cat = JSON.parse(list);
category_dump(cat);
}
catch(err) {
if ((res.statusCode>=400) && (res.statusCode<600)) {
console.log(res.statusCode+' '+res.statusMessage);
}
else {
console.log('Something went wrong parsing the category/format JSON');
console.log(err);
console.log('** '+list);
}
}
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
console.log(e);
});
req.end();
}
//------------------------------------------------------------------------[main]
// was http://polling.bbc.co.uk/radio/categories.json
// was http://clifton.api.bbci.co.uk/aps/programmes/genres.json
// was http://clifton.api.bbci.co.uk/aps/programmes/formats.json
// https://rms.api.bbc.co.uk/categories
// https://rms.api.bbc.co.uk/v2/categories/container
var type = 'genre';
if (process.argv.length>2) {
type = process.argv[2];
}
if (type.startsWith('genre')) {
list_categories('/categories');
}
else if (type.startsWith('format')) {
list_categories('/aps/programmes/formats.json');
}
else {
console.log('Unknown type: genres|formats');
}