-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathAPIHandler.py
57 lines (51 loc) · 2.21 KB
/
APIHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import logging
import requests
import config
import src.utils as utils
class APIHandler:
@staticmethod
def get_yt_playlist_size(playlist_id: str) -> int:
logging.info("Getting amount of playlist items")
try:
config.YT_API_KEY
except AttributeError:
logging.warning(
f"No YT_API_KEY provided in config.py -> return playlist_size of 0")
return 0
url = (
f"https://www.googleapis.com/youtube/v3/playlistItems"
)
payload = {"part": "id", "playlistId": playlist_id, "key": config.YT_API_KEY}
resp = requests.get(url, params=payload, headers={})
if resp.status_code == 200:
return resp.json()["pageInfo"]["totalResults"]
else:
logging.warning(f"Status Code: {resp.status_code}, {resp.json()}")
logging.warning(
f"Check if you inserted a correct PlayListID in your metadata_config -> return playlist_size of 0")
return 0
@staticmethod
def get_new_twitch_token() -> str:
logging.info("Getting new token from server")
url = "https://id.twitch.tv/oauth2/token"
payload = {"client_id": config.CLIENT_ID, "client_secret": config.CLIENT_SECRET,
"grant_type": "client_credentials"}
resp = requests.post(url, params=payload, headers={"Client-ID": config.CLIENT_ID})
if resp.status_code == 200:
with open("token", "w") as outfile:
outfile.write(resp.json()["access_token"])
return resp.json()["access_token"]
else:
logging.warning(f"Status Code: {resp.status_code}, {resp.json()}")
@staticmethod
def get_twitch_game_id(name: str) -> int:
url = f"https://api.twitch.tv/helix/games"
payload = {"name": name}
resp = requests.get(url, params=payload, headers=utils.get_headers())
if resp.status_code == 200:
try:
return resp.json()["data"][0]["id"]
except (IndexError, KeyError):
raise NameError("Game not found. Please provide a valid game name.")
else:
logging.warning(f"Status Code: {resp.status_code}, {resp.json()}")