Skip to content

Commit

Permalink
Mystery examples
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryCarlyon committed Aug 8, 2023
1 parent 38d1851 commit dde49dc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
82 changes: 82 additions & 0 deletions examples/sponsor_finder/index.html
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>
6 changes: 3 additions & 3 deletions examples/tag_counter/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<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>Get the code for this example on <a href="https://github.com/BarryCarlyon/twitch_misc/tree/main/examples/tag_counter">Github</a> or just View the source instead</p>

<a href="" id="authorize" target="barrysgithubtwitchauth">Authorize</a>
<div id="loading"></div>
Expand Down Expand Up @@ -79,7 +79,7 @@ <h3>All Tags</h3>
.then(resp => resp.json())
.then(resp => {
total_streams += resp.data.length;

resp.data.forEach(stream => {
let { tags, viewer_count } = stream;
page_record_viewer = viewer_count;
Expand Down Expand Up @@ -114,7 +114,7 @@ <h3>All Tags</h3>
});

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);

Expand Down

0 comments on commit dde49dc

Please sign in to comment.