LORDCHANNEL2.0/utils/thetvdbapi.py

107 lines
3.4 KiB
Python

import requests
"""
Dichiarazione della chiave API di TheTVDB e di tutti gli endpoint
Documentazione : https://api.thetvdb.com/swagger
"""
API_KEY = "4ce19cf20c1a9988b9574597c17d00c0"
USER_KEY = "8SWHSW1X4P7TFUPC"
LOGIN_ENDPOINT = "https://api.thetvdb.com/login"
REFRESH_TOKEN = "https://api.thetvdb.com/refresh_token"
SEARCH_ENDPOINT = "https://api.thetvdb.com/search/series"
SERIES_ENDPOINT = "https://api.thetvdb.com/series/"
USERNAME = "MickSlash"
LANGUAGE = "it"
IMAGE_ENDPOINT = "https://artworks.thetvdb.com"
def image_Endpoint():
return IMAGE_ENDPOINT
# Definizione Funzione per trovare la Serie TV
def search_tv_show(serie_tv):
try:
r_token = get_token(token)
headers = {"Authorization": "Bearer " + r_token,"Accept-Language":LANGUAGE}
params = {"name":serie_tv}
get_tv_show = requests.get(SEARCH_ENDPOINT,headers=headers,params=params)
if get_tv_show.ok:
# print(get_tv_show.json())
data_tv_show = get_tv_show.json()
return data_tv_show
# titolo = data_tv_show['data'][0]['seriesName']
# id = data_tv_show['data'][0]['id']
# print(titolo)
else:
print("Status Code ",get_tv_show.status_code)
except:
data_tv_show = None
return data_tv_show
# Ottengo le informazioni sulla Serie TV
def get_tvshow_info(data_tv_show):
try:
r_token = get_token(token)
headers = {"Authorization": "Bearer " + r_token,"Accept-Language":LANGUAGE}
id = str(data_tv_show['data'][0]['id'])
tv_info = requests.get(SERIES_ENDPOINT + id,headers=headers)
if tv_info.ok:
EPISODES_ENDPOINT = f"https://api.thetvdb.com/series/{id}/episodes/query?page=1"
tv_episodes = requests.get(EPISODES_ENDPOINT,headers=headers)
data = tv_info.json()
if tv_episodes.ok:
episodes_data = tv_episodes.json()
return data,episodes_data
# stagioni = data_tvinfo['data']['airedSeasons']
# print(stagioni)
else:
print("Status Code ",tv_info.status_code)
except:
data = None
episodes_data = None
return data,episodes_data
# Ottengo il token e lo aggiorno ogni 24 ore per estenderlo
def get_token(token):
try:
headers = {"Authorization": "Bearer " + token}
get_refresh_token = requests.get(REFRESH_TOKEN,headers=headers)
if get_refresh_token.ok:
token_data = get_refresh_token.json()
new_token = token_data['token']
return new_token
# print(f"New Token : {new_token}")
else:
print("Errore")
print(f"{get_refresh_token.content}")
except:
new_token = None
return new_token
def login(API_KEY,USER_KEY,LOGIN_ENDPOINT,USERNAME):
try:
params = {"apikey":API_KEY,"userkey":USER_KEY,"username":USERNAME}
response = requests.post(LOGIN_ENDPOINT,json=params)
if response.ok:
data = response.json()
token = data['token']
return token
else:
print(f"Status Code {response.status_code}")
except:
token = None
return token
# serie_tv = "The Shield"
token = login(API_KEY,USER_KEY,LOGIN_ENDPOINT,USERNAME)
get_token(token)
# data_tv_show = search_tv_show(serie_tv)
# get_tvshow_info(data_tv_show)