| @@ -1,19 +1,24 @@ | |||||
| #!/usr/bin/env python | |||||
| # -*- coding: utf-8 -*- | |||||
| # This program is dedicated to the public domain under the CC0 license. | |||||
| # Telegram - Betaseries | |||||
| import logging | import logging | ||||
| import requests | import requests | ||||
| import json | import json | ||||
| from config import config | from config import config | ||||
| #Love Is War / Une serie pour tester le programme. | |||||
| thetvdb_id= '354198' | |||||
| import os.path | |||||
| from os import path | |||||
| token = config['token'] | |||||
| client_id = config['client_id'] | client_id = config['client_id'] | ||||
| login = config['login'] | login = config['login'] | ||||
| password = config['password'] | password = config['password'] | ||||
| def login_beta(): | def login_beta(): | ||||
| global betauser | |||||
| # L'API nécéssite le client_id, le login + le mot de passe en MD5. A partir de là, nous récupérons le TOKEN Betaseries qui nous permettra de communiquer avec notre compte Betaseries | |||||
| global token | |||||
| print("Connexion a Betaseries en cours...") | print("Connexion a Betaseries en cours...") | ||||
| API_auth = 'https://api.betaseries.com/members/auth' | API_auth = 'https://api.betaseries.com/members/auth' | ||||
| API_auth += '?client_id=' + client_id +'&login=' + login + '&password=' + password | API_auth += '?client_id=' + client_id +'&login=' + login + '&password=' + password | ||||
| @@ -21,12 +26,63 @@ def login_beta(): | |||||
| if str(request) != '<Response [200]>' : | if str(request) != '<Response [200]>' : | ||||
| return False | return False | ||||
| data = request.json() | data = request.json() | ||||
| betauser = str(data['token']) | |||||
| return betauser | |||||
| token = str(data['token']) | |||||
| return token | |||||
| def create_api(choice): | |||||
| def create_api(choice, thetvdb_id): | |||||
| #Cette fonction permet de créer l'api Betaseries (que nous allons interroger) à partir des informations dans config.py | |||||
| #La séparer dans une autre fonction permet de choisir QUELLE api créer et pour quel thetvdb_id | |||||
| if choice == 'latest' : | if choice == 'latest' : | ||||
| base_url = 'https://api.betaseries.com/episodes/latest?' | base_url = 'https://api.betaseries.com/episodes/latest?' | ||||
| API = base_url + 'token=' + token + '&client_id=' + client_id + '&thetvdb_id=' + thetvdb_id | API = base_url + 'token=' + token + '&client_id=' + client_id + '&thetvdb_id=' + thetvdb_id | ||||
| return API | |||||
| return API | |||||
| else : | |||||
| return False | |||||
| def check_latest(thetvdb_id): | |||||
| # Nous utilisons le thetvdb_id et le fournissons à create_api. à partir de là nous récupérons le dernier épisode de la série. Et nous regardons s'il est déjà enregistré [WIP] | |||||
| API = create_api('latest', thetvdb_id) | |||||
| if API == False : | |||||
| print("ERREUR DURANT LA CREATION DE L'API !") | |||||
| return | |||||
| request = requests.get(API) | |||||
| if str(request) != '<Response [200]>' : | |||||
| print("Mauvaise réponse de l'api ! ") | |||||
| return False | |||||
| show = request.json() | |||||
| episode = show['episode'] | |||||
| title = episode['show'] | |||||
| print("Titre de la serie : ", str(title['title'])) | |||||
| print("Dernier episode sorti : Episode : ", episode['episode'] , " Saison :", episode['season']) | |||||
| if path.exists('latest') == False : | |||||
| print("Le dossier 'latest' n'existe pas, creez le") | |||||
| #On créé le dictionnaire contenant les informations de l'épisode | |||||
| episode_info = { | |||||
| "serie_title" : title['title'], | |||||
| "season " : episode['season'], | |||||
| "episode" : episode['episode'] | |||||
| } | |||||
| #On choisi créé l'emplacement du fichier. On utilisera l'id thetvdb car utiliser le nom de la série risquerait des fichiers trop long | |||||
| file_path = '' | |||||
| file_path = 'latest/' + thetvdb_id + '.json' | |||||
| print("Path a essayer : ", file_path) | |||||
| if path.exists(file_path) == False : | |||||
| print("Fichier inexistant.. creation en cours...") | |||||
| with open(file_path, "w") as write_file: | |||||
| json.dump(episode_info, write_file) | |||||
| else : | |||||
| print("Fichier deja existant... On compare") | |||||