Final Code for Twitch Dashboard
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) | |
topgames_data = games_response_json['data'] | |
topgames_df = pd.DataFrame.from_dict(json_normalize(topgames_data), orient='columns') | |
# Top Streamers | |
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) | |
topstreamsforgame_response_json = json.loads(topstreamsforgame_response.text) | |
topstreamsforgame_data = topstreamsforgame_response_json['data'] | |
topstreamsforgame_df = pd.DataFrame.from_dict(json_normalize(topstreamsforgame_data), orient='columns') | |
for i in range(1,19) : | |
headers = { | |
'Authorization' : 'Bearer '+str(access_token), | |
} | |
topstreamsforgame_response = requests.get('https://api.twitch.tv/helix/streams?game_id='+str(topgames_df['id'][i])+'&first=25', headers=headers) | |
topstreamsforgame_response_json = json.loads(topstreamsforgame_response.text) | |
topstreamsforgame_data = topstreamsforgame_response_json['data'] | |
topstreamsforgame_df_temp = pd.DataFrame.from_dict(json_normalize(topstreamsforgame_data), orient='columns') | |
frames = [topstreamsforgame_df, topstreamsforgame_df_temp] | |
topstreamsforgame_df = pd.concat(frames, ignore_index=True) | |
# Now that the FOR loop is exited and we have all our data, we export it into a csv | |
export_topgames_csv = topgames_df.to_csv (r'<filepath>.csv', index = None, header=True) #Don't forget to add '.csv' at the end of the path | |
export_topstreamsforgame_csv = topstreamsforgame_df.to_csv (r'<filepath>.csv', index = None, header=True) | |
# Our function is defined and it overwrites the CSV every 60 seconds. Now, we call it. | |
twitch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment