💾 Archived View for ait.place › nix › extra › twitch.py captured on 2024-05-10 at 10:37:47.
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
#!/usr/bin/env python3 import sys import requests import os api_url = 'https://api.twitch.tv/helix' id_url = 'https://id.twitch.tv/oauth2/token' api_key = 'cotxsalhlctv8z572f7fant4b0sc3u' api_secret = 'gaofxvult280l3sbz8n6btvk5fdswp' api_file = os.getenv("XDG_CACHE_HOME") + '/twitch/api' name_file = os.getenv("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()