From f0cb8bf77f542a832348d80e86fca2aebf385776 Mon Sep 17 00:00:00 2001 From: administrator Date: Sat, 2 May 2020 13:04:24 +0200 Subject: [PATCH] modified: betaseries.py - Ajout de get_episode_code, seen, et to_see modified: bot.py - correction de bug + ajout des commandes /check / list / seen modified: notification.py - Correction de bug --- betaseries.py | 44 ++++++++++++++++++++------------ bot.py | 67 ++++++++++++++++++++++++++++++++++++------------- notification.py | 26 +++++++++++-------- 3 files changed, 94 insertions(+), 43 deletions(-) diff --git a/betaseries.py b/betaseries.py index bf991db..f2de70f 100644 --- a/betaseries.py +++ b/betaseries.py @@ -6,7 +6,7 @@ import logging import requests import json -from config import config +from config import config, series import os # import time from os import path @@ -40,16 +40,16 @@ def create_api(choice, thetvdb_id): if choice == 'latest' : base_url = 'https://api.betaseries.com/episodes/latest?' - API = base_url + 'token=' + token + '&client_id=' + client_id + '&thetvdb_id=' + thetvdb_id - return API + API_latest = base_url + 'token=' + token + '&client_id=' + str(client_id) + '&thetvdb_id=' + thetvdb_id + return API_latest if choice == 'info' : base_url = 'https://api.betaseries.com/shows/display?' - API = base_url + 'token=' + token + '&client_id=' + client_id + '&thetvdb_id=' + thetvdb_id - return API + API_info = base_url + 'token=' + token + '&client_id=' + str(client_id) + '&thetvdb_id=' + str(thetvdb_id) + return API_info if choice == 'seen': base_url = 'https://api.betaseries.com/episodes/watched?' - API = base_url + 'token=' + token + '&client_id=' + client_id + '&id=' - return API + API_seen = base_url + 'token=' + token + '&client_id=' + str(client_id) + '&id=' + return API_seen return False @@ -61,7 +61,6 @@ def check_latest(thetvdb_id): return request = requests.get(API) - if str(request) != '' : print("Mauvaise réponse de l'api !(Check_Latest) ") return False @@ -124,6 +123,7 @@ def check_latest(thetvdb_id): return if data['seen'] == False : + print("Vous n'avez pas vu le dernier episode de cette series : ", get_title_only(thetvdb_id)) os.remove(file_path) create_file(file_path, episode_info) return @@ -155,14 +155,12 @@ def to_see(thetvdb_id): file_path = 'latest/' + thetvdb_id + '.json' with open(file_path) as json_file: data = json.load(json_file) - print(data) + if data[u'seen'] == False : message = data['serie_title'] + ' ! ' + '\n' + 'Episode: ' + str(data['episode']) + ' Saison:' + str(data['season ']) + '\n' + '\n' return message - else : - print(u"Vous etes à jour") - - return + print(u"Vous etes à jour") + return False def seen(thetvdb_id): # Permet de mettre un episode en 'vu' sur betaseries @@ -178,13 +176,27 @@ def seen(thetvdb_id): API = create_api('seen', None) API += str(data['id']) print(API) - + # request = requests.post(API) + # if str(request) == '' : + # send_message(u"Impossible de marquer l'episode comme vu(déjà vu?)") + # print(request) + # return + send_message('Done !') + return + +def get_episode_code(thetvdb_id): + file_path = 'latest/' + thetvdb_id + '.json' + with open(file_path) as json_file: + data = json.load(json_file) + code = 'S' + str(data['season ']) + 'E' + str(data['episode']) + return code def main(): #Only here for debug git - login_beta() - print(get_title_only('354198')) + for i in range(len(series)): + id = series[i] + print(get_episode_code(id)) if __name__ == '__main__': diff --git a/bot.py b/bot.py index 2ae39e4..b5378bd 100644 --- a/bot.py +++ b/bot.py @@ -8,7 +8,7 @@ import json from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import Updater, CommandHandler, CallbackQueryHandler from config import config, series -from betaseries import login_beta, get_title_only, to_see, seen +from betaseries import login_beta, get_title_only, to_see, seen, get_episode_code from telegram_api import send_message # Activer / Désactiver le debuggage {Projet : pas encore utilisé} @@ -92,11 +92,14 @@ def repeat(update, context): return -def check_seen(update, context): - message = 'à voir \n' +def latest(update, context): + message = 'à voir : \n' for i in range(len(series)): thetvdb_id = series[i] - message += '- ' + str(to_see(thetvdb_id)) + episode = to_see(thetvdb_id) + + if episode != False: + message += '- ' + str(episode) send_message(message) @@ -111,43 +114,73 @@ def liste(update, context): -def coding(update, context): +def ive_seen(update, context): if type(series) == str : file_path = 'latest/' + series + '.json' print(file_path) with open(file_path) as json_file: data = json.load(json_file) - # message = u'Episode que vous allez marquer comme vu : ' + data['serie_title'] + ' - S' + str(data['season ']) + 'E' + str(data['episode']) - # send_message(message) - keyboard = [[InlineKeyboardButton(data['serie_title'] + '|' + 'S' + str(data['season ']) + 'E' + str(data['episode']), callback_data='1')]] reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text('Vous avez vu :', reply_markup=reply_markup) return + else : - keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'), - InlineKeyboardButton("Option 2", callback_data='2'), - InlineKeyboardButton("Option 3", callback_data='3')], - [InlineKeyboardButton("Option 4", callback_data='4')] ] + if len(series) == 2 : + keyboard = [[InlineKeyboardButton(get_title_only(series[0]), callback_data='1'), + InlineKeyboardButton(get_title_only(series[1]), callback_data='2')]] + + if len(series) == 3 : + keyboard = [ + [InlineKeyboardButton(get_title_only(series[0]), callback_data='1'), InlineKeyboardButton(get_title_only(series[1]), callback_data='2')], + [InlineKeyboardButton(get_title_only(series[2]), callback_data='3')] + ] + if len(series) == 4 : + keyboard = [ + [InlineKeyboardButton(get_title_only(series[0]), callback_data='1'), InlineKeyboardButton(get_title_only(series[1]), callback_data='2') ], + [InlineKeyboardButton(get_title_only(series[2]), callback_data='3'), InlineKeyboardButton(get_title_only(series[3]), callback_data='4') ] + ] + if len(series) == 5 : + keyboard = [ + [ InlineKeyboardButton(get_title_only(series[0]), callback_data='1'), InlineKeyboardButton(get_title_only(series[1]), callback_data='2')], + [ InlineKeyboardButton(get_title_only(series[2]), callback_data='3'), InlineKeyboardButton(get_title_only(series[3]), callback_data='4')], + [ InlineKeyboardButton(get_title_only(series[4]), callback_data='5')] + ] + reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text('Selectionnez une reponse', reply_markup=reply_markup) return - + def button(update, context): query = update.callback_query query.answer() print(type(query['data'])) + # Si jamais l'utilisateur fait le 1er choix, on vérifie si jamais 'series' n'est pas un str (qui serait donc le cas ou l'utilisateur n'a qu'une serie if query['data'] == u'1' : if type(series) == str : seen(series) - + episode = get_title_only(series) + ' | ' + get_episode_code(series) + query.edit_message_text(text="Choix de la series : {}".format(episode)) + return + else : + seen(series[0]) + episode = get_title_only(series[0]) + ' | ' + get_episode_code(series[0]) + query.edit_message_text(text="Choix de la series : {}".format(episode)) + return + + + number = int(query['data']) - 1 + + query.edit_message_text(text="Choix de la series : ".format(get_title_only(number) )) + - query.edit_message_text(text="Selected option: {}".format(query.data)) + # query.edit_message_text(data['serie_title'] + '|' + 'S' + str(data['season ']) + 'E' + str(data['episode'] )) + #query.edit_message_text(text="Selected option: {}".format(query.data)) @@ -174,9 +207,9 @@ def main(): dp.add_handler(CommandHandler("liste", liste)) - dp.add_handler(CommandHandler("check", check_seen)) + dp.add_handler(CommandHandler("latest", latest)) - dp.add_handler(CommandHandler("code", coding)) + dp.add_handler(CommandHandler("seen", ive_seen)) updater.dispatcher.add_handler(CallbackQueryHandler(button)) diff --git a/notification.py b/notification.py index bf5d9ef..f18c0b5 100644 --- a/notification.py +++ b/notification.py @@ -13,7 +13,7 @@ from config import series import os.path from os import path import datetime - +import time def main(): @@ -28,16 +28,19 @@ def main(): print("Pour arreter le script : CTRL+Z") - now = datetime.datetime.now() - print (now.strftime("%Y-%m-%d %H:%M:%S")) - if type(series) == str : - check_latest(series) + while 1 : + now = datetime.datetime.now() + print (now.strftime("%Y-%m-%d %H:%M:%S")) + + if type(series) == str : + check_latest(series) - else: - for i in range(len(series)): - thetvdb_id = series[i] - check_latest(thetvdb_id) + else: + for i in range(len(series)): + thetvdb_id = series[i] + check_latest(thetvdb_id) + time.sleep(1800) @@ -47,4 +50,7 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + try : + main() + except : + main() \ No newline at end of file