-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38d1851
commit dde49dc
Showing
2 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<html> | ||
<head> | ||
<title>Sponsor Finder| 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/sponsor_finder">Github</a> or just View the source instead</p> | ||
|
||
<a href="" id="authorize" target="barrysgithubtwitchauth">Authorize</a> | ||
<div id="loading"></div> | ||
|
||
<table id="streams"></table> | ||
|
||
<script type="text/javascript"> | ||
// go populate this with a client_id | ||
var client_id = 'hozgh446gdilj5knsrsxxz8tahr3koz'; | ||
var redirect = 'https://' + window.location.host + '/twitch_misc/'; | ||
// setup a memory space for the token | ||
var access_token = ''; | ||
var page_counter = 0; | ||
var total_streams = 0; | ||
var page_record_viewer = 0; | ||
|
||
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 Streams Page: ${page_counter} StreamCount: ${total_streams} ViewR0: ${page_record_viewer}`; | ||
|
||
getPage(); | ||
} | ||
|
||
function getPage(after) { | ||
page_counter++; | ||
fetch( | ||
'https://api.twitch.tv/helix/streams?first=100' + (after ? '&after='+after : ''), | ||
{ | ||
method: 'GET', | ||
headers: { | ||
"Client-ID": client_id, | ||
"Authorization": "Bearer " + access_token | ||
} | ||
} | ||
) | ||
.then(resp => resp.json()) | ||
.then(resp => { | ||
total_streams += resp.data.length; | ||
|
||
resp.data.forEach(stream => { | ||
let { title, user_name, viewer_count } = stream; | ||
|
||
let ltitle = title.toLowerCase(); | ||
if (ltitle.includes('#ad') || ltitle.includes('#sponsored') || ltitle.includes('#sponsored') || ltitle.includes('sponsor')) { | ||
let r = streams.insertRow(); | ||
var t = r.insertCell(); | ||
t.textContent = user_name; | ||
var t = r.insertCell(); | ||
t.textContent = title; | ||
var t = r.insertCell(); | ||
t.textContent = viewer_count; | ||
} | ||
}); | ||
|
||
document.getElementById('loading').textContent = `Loading Streams Page: ${page_counter} StreamCount: ${total_streams} ViewR0: ${page_record_viewer}`; | ||
|
||
if (resp.pagination && resp.pagination.cursor) { | ||
getPage(resp.pagination.cursor); | ||
|
||
drawTop(); | ||
} | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
document.getElementById('loading').textContent = 'Something went wrong'; | ||
}); | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters