💾 Archived View for ait.place › dot › bin › .local › bin › twitch.py.txt captured on 2022-06-03 at 22:57:18.
⬅️ Previous capture (2022-04-28)
-=-=-=-=-=-=-
#!/bin/python3 import sys import requests import xdg.BaseDirectory api_url = 'https://api.twitch.tv/helix' id_url = 'https://id.twitch.tv/oauth2/token' api_key = 'cotxsalhlctv8z572f7fant4b0sc3u' api_secret = 'gaofxvult280l3sbz8n6btvk5fdswp' api_file = xdg.BaseDirectory.xdg_cache_home + "/twitch/api" name_file = xdg.BaseDirectory.xdg_config_home + "/twitch/names" def get_api_token(): data = { "client_id": api_key, "client_secret": api_secret, "grant_type": "client_credentials" } r = requests.post(id_url, data=data) if not r.ok: sys.exit("api request not valid") return r.json()['access_token'] def main(): try: # try to create file and write access_token f = open(api_file, "x") json_response = get_api_token() f.write(json_response) access_token = json_response f.close() except FileExistsError: # if file does exist then read the file and assign the access_token f = open(api_file, "r") access_token = f.read() f.close() try: f = open(name_file, "r") content = f.read() streamers = content.splitlines() f.close() except FileNotFoundError: sys.exit("twitch names file does not exist") if len(sys.argv) > 1: if sys.argv[1] == "top": streamers = "" # puts each word piped in into array #streamers = sys.stdin.read().rsplit() # make the url to send tosend = api_url + "/streams?first=100&" for streamer in streamers: tosend += "user_login=" + streamer + "&" headers = { "Client-ID": api_key, "Authorization": "Bearer " + access_token } r = requests.get(tosend, headers=headers) if not r.ok: f = open(api_file, "w") json_response = get_api_token() f.write(json_response) access_token = json_response f.close() headers = { "Client-ID": api_key, "Authorization": "Bearer " + access_token } r = requests.get(tosend, headers=headers) if not r.ok: sys.exit("request failed") for entry in r.json()['data']: print(entry['user_login']) main()