-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.html
169 lines (143 loc) · 7.31 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<title>Browse Categories | Twitch API Example</title>
<link rel="stylesheet" href="/twitch_misc/style.css" />
</head>
<body>
<p>This example first uses <a href="https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-implicit-code-flow" target="_blank">Implicit Auth</a> to get a token to use then will return a page similar to <a href="https://www.twitch.tv/directory" target="_blank">The Directorty</a>. Generally calls will be done/cached server side with an <a href="https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-client-credentials-flow" target="_blank">App Access Token</a></p>
<p>Get the code for this example on <a href="https://github.com/BarryCarlyon/twitch_misc/tree/main/examples/browse_categories">Github</a> or just View the source instead</p>
<p>After authenticating to get a Key, it calls <a href="https://dev.twitch.tv/docs/api/reference#get-top-games" target="_blank">Get Top Games</a> then calls the first few pages of <a href="https://dev.twitch.tv/docs/api/reference#get-streams" target="_blank">Get Streams</a> for that game to get an approx viewer count.</p>
<a href="" id="authorize" target="barrysgithubtwitchauth">Authorize</a>
<div id="loading"></div>
<h3>Page Loading Status</h3>
<p>Status output as each page of streams for that gameID loads</p>
<table id="loader">
<tr>
<th>GameID</th>
<th>Status</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>...</th>
</tr>
</table>
<h3>The Directory</h3>
<table id="categories"></table>
<script type="text/javascript">
// go populate this with a client_id
var client_id = 'hozgh446gdilj5knsrsxxz8tahr3koz';
var redirect = 'https://barrycarlyon.github.io/twitch_misc/';
// setup a memory space for the token
var access_token = '';
document.getElementById('authorize').setAttribute('href', 'https://id.twitch.tv/oauth2/authorize?client_id=' + client_id + '&redirect_uri=' + encodeURIComponent(redirect) + '&response_type=token')
function processToken(token) {
access_token = token;
document.getElementById('loading').textContent = 'Loading Categories';
// Get top 20 categories
fetch(
'https://api.twitch.tv/helix/games/top?first=20',
{
"headers": {
"Client-ID": client_id,
"Authorization": "Bearer " + access_token
}
}
)
.then(resp => resp.json())
.then(resp => {
var tr = false;
document.getElementById('loading').textContent = 'Got ' + resp.data.length + ' Categories';
// dumb table maker
for (var x=0;x<resp.data.length;x++) {
var r = document.createElement('tr');
r.setAttribute('id', 'loader_row_' + resp.data[x].id);
document.getElementById('loader').append(r);
var d = document.createElement('td');
d.textContent = resp.data[x].id;
r.append(d);
var d = document.createElement('td');
d.setAttribute('id', 'loader_' + resp.data[x].id);
r.append(d);
if (!tr || (x % 10) === 0) {
tr = document.createElement('tr');
}
document.getElementById('categories').append(tr);
var td = document.createElement('td');
td.style.textAlign = 'center';
tr.append(td);
var img = document.createElement('img');
// how to handle image sizing
// substitute {width} and {height} as needed
//img.setAttribute('src', resp.data[x].box_art_url.replace('{width}x{height}', '285x380'));
img.setAttribute('src', resp.data[x].box_art_url.replace('{width}x{height}', '100x150'));
td.append(img);
var p = document.createElement('p');
p.textContent = resp.data[x].name;
td.append(p);
var d = document.createElement('span');
d.setAttribute('id', 'count_' + resp.data[x].id);
d.textContent = '0';
td.append(d);
var d = document.createElement('span');
d.textContent = ' viewers';
td.append(d);
// parraelelelellle call
fetchStreams(resp.data[x].id);
}
})
.catch(err => {
console.log(err);
document.getElementById('loading').textContent = 'Something went wrong';
});
}
// max number of pages to stop
// you don't want to go too deep
// as in the first 20/30/50 pages you'll collect MOST of the statistically significant channels
// to get an approximation of the category
var limit = 50;
function fetchStreams(game_id, page, after, tot) {
page = page ? page : 0;
document.getElementById('loader_' + game_id).textContent = 'Loading Page: ' + page + ' Current ' + tot;
fetch(
'https://api.twitch.tv/helix/streams?first=100&game_id=' + game_id + (after ? '&after=' + after : ''),
{
"headers": {
"Client-ID": client_id,
"Authorization": "Bearer " + access_token
}
}
)
.then(resp => resp.json())
.then(resp => {
document.getElementById('loader_' + game_id).textContent = 'Processing Page: ' + page;
var total = parseInt(document.getElementById('count_' + game_id).textContent);
for (var x=0;x<resp.data.length;x++) {
total += resp.data[x].viewer_count;
}
document.getElementById('count_' + game_id).textContent = total;
var d = document.createElement('td');
d.textContent = resp.data.length;
document.getElementById('loader_row_' + game_id).append(d);
// loop if we got a cursor
if (resp.hasOwnProperty('pagination') && resp.pagination.hasOwnProperty('cursor')) {
page++;
// do a page limit check
if (page >= limit) {
document.getElementById('loader_' + game_id).textContent = 'Stopped at Page: ' + page + ' - ' + resp.data.length;
return;
}
fetchStreams(game_id, page, resp.pagination.cursor, total);
} else {
document.getElementById('loader_' + game_id).textContent = 'Last Page: ' + page + ' - ' + resp.data.length;
}
})
.catch(err => {
console.log(err);
document.getElementById('loading').textContent = 'Something went wrong';
});
}
</script>
</body>
</html>