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
#Client ID: --***--- | |
#Client Secret: ---***--- | |
#Client ID and Client Secret are sensititve and you should not share them | |
client_id= <yourclientid> | |
client_secret= <yourclientsecret> | |
#Request for the access code using requests library | |
#I have chosen this method of authentication with my goal in mind |
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
import json | |
import requests | |
import pandas as pd | |
from pandas.io.json import json_normalize | |
import time | |
import threading |
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
# Getting data for Top 100 Games by number of viewers | |
# Default response is for 20 games so you will have to set the parameter 'first to 100' | |
headers = { | |
'Authorization' : 'Bearer '+str(access_token), | |
} | |
games_response = requests.get('https://api.twitch.tv/helix/games/top?first=100', headers=headers) | |
# The response will be a JSON which will include the response data and the pagination cursor | |
# We need to extract the data from the JSON and convert it into a pandas dataframe |
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
# I am getting only the top 25 streamers for the first game | |
headers = { | |
'Authorization' : 'Bearer '+str(access_token), | |
} | |
topstreamsforgame_response = requests.get('https://api.twitch.tv/helix/streams?game_id='+str(topgames_df['id'][0])+'&first=25', headers=headers) | |
# Load the JSON | |
topstreamsforgame_response_json = json.loads(topstreamsforgame_response.text) | |
# Extracting data from the JSON |
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
def twitch(): | |
threading.Timer(60.0, twitch).start() | |
# Top Games | |
headers = { | |
'Authorization' : 'Bearer '+str(access_token), | |
} | |
games_response = requests.get('https://api.twitch.tv/helix/games/top?first=100', headers=headers) | |
games_response_json = json.loads(games_response.text) |
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
import pandas as pd | |
import requests | |
import json | |
from pandas.io.json import json_normalize | |
import time | |
import base64 |
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
# Defining base64 encoding of the IDs | |
def base64_encode(client_id,client_secret): | |
encodedData = base64.b64encode(bytes(f"{client_id}:{client_secret}", "ISO-8859-1")).decode("ascii") | |
authorization_header_string = f"{encodedData}" | |
return(authorization_header_string) |
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
def accesstoken(client_id, client_secret): | |
header_string= base64_encode(client_id,client_secret) | |
headers = { | |
'Authorization': 'Basic '+header_string, | |
} | |
data = { | |
'grant_type': 'client_credentials' | |
} | |