-
Notifications
You must be signed in to change notification settings - Fork 2
/
hw4.js
37 lines (32 loc) · 836 Bytes
/
hw4.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
// 拿到最受歡迎的遊戲列表(Get Top Games)
const request = require('request');
const bookUrl = 'https://api.twitch.tv/kraken/games/top';
const options = {
url: bookUrl,
headers: {
Accept: 'application/vnd.twitchtv.v5+json',
'Client-ID': '0n4il3nmibawzxq23hqdbid338v15p',
},
qs: {
limit: 100,
},
};
function callback(error, response, body) {
if (response.statusCode >= 200 && response.statusCode < 300) {
try {
const mBody = JSON.parse(body);
const gameList = mBody.top;
for (let i = 0; i < gameList.length; i += 1) {
console.log(`${gameList[i].viewers} ${gameList[i].game.name}`);
}
} catch (e) {
console.log(e);
}
} else {
console.log('伺服器錯誤');
}
}
function getTopGames() {
request.get(options, callback);
}
getTopGames();