-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_functions.js
109 lines (85 loc) · 2.75 KB
/
example_functions.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
var http = require("http");
var https = require("https");
const API_KEY = `YOUR_API_KEY`;
var categoriesById = {};
var categoriesByName = {};
function getCategoriesAndRunExamples() {
var options = {
host: 'developers.medal.tv',
port: 443,
path: '/v1/categories',
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': API_KEY
}
};
getJSON(options, function(statusCode, result) {
result.forEach(function(category) {
categoriesById[category.categoryId] = category;
categoriesByName[category.categoryName.toLowerCase()] = category;
})
getTopClipsForSpecificGame(2, "pubg")
getLatestClipsForSpecificGame(2, "fortnite")
});
}
function getTopClipsForSpecificGame(amount, game) {
var options = {
host: 'developers.medal.tv',
port: 443,
path: '/v1/trending?limit='+amount+"&categoryId="+categoriesByName[game.toLowerCase()].categoryId,
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': API_KEY
}
};
getJSON(options, function(statusCode, result) {
console.log(amount + " top clips for " + game + " ####################################################");
console.log(result);
});
}
function getLatestClipsForSpecificGame(amount, game) {
var options = {
host: 'developers.medal.tv',
port: 443,
path: '/v1/latest?limit='+amount+"&categoryId="+categoriesByName[game.toLowerCase()].categoryId,
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': API_KEY
}
};
getJSON(options, function(statusCode, result) {
console.log(amount + " top clips for " + game + " ####################################################");
console.log(result);
});
}
/**
* getJSON: REST get request returning JSON object(s)
* @param options: http options object
* @param callback: callback to pass the results JSON object(s) back
*/
getJSON = function(options, onResult)
{
console.log("rest::getJSON");
var prot = options.port == 443 ? https : http;
var req = prot.request(options, function(res)
{
var output = '';
console.log(options.host + ':' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', function() {
var obj = JSON.parse(output);
onResult(res.statusCode, obj);
});
});
req.on('error', function(err) {
//res.send('error: ' + err.message);
});
req.end();
};
getCategoriesAndRunExamples();