From dd2813332e372e02bc29931797d3ffa0fbbfc932 Mon Sep 17 00:00:00 2001 From: Ray Lyon Date: Sat, 2 Apr 2022 17:49:09 -0400 Subject: [PATCH] -escape characters for tg markdown -function to escape characters in response variables -responds when movie isn't found in tmdb or sa --- telegram-moviebot/movie_check.py | 28 ++++++++++++++++---------- telegram-moviebot/telegram-moviebot.py | 24 +++++++++++++++++----- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/telegram-moviebot/movie_check.py b/telegram-moviebot/movie_check.py index 804cecd..084db0c 100644 --- a/telegram-moviebot/movie_check.py +++ b/telegram-moviebot/movie_check.py @@ -16,9 +16,7 @@ def tmdb_lookup(tmdb_url, tmdb_headers, movie): headers=tmdb_headers).json() if not tmdb_search["results"]: - print("I'm having trouble finding that movie. " + - "Check your spelling and try again.") - exit() + return "404", "404", "404", "404" movie_id = tmdb_search['results'][0]['id'] movie_title = tmdb_search['results'][0]['title'] @@ -47,12 +45,10 @@ def sa_lookup(sa_url, sa_headers, movie_id): params=sa_params) if sa_request.status_code == 404: - print("I'm having trouble finding that movie on streaming. " + - "Check your spelling and try again.") - exit() - - sa_response = sa_request.json() - services = sa_response["streamingInfo"] + sa_response = "404" + else: + sa_response = sa_request.json() + services = sa_response["streamingInfo"] return sa_response, services @@ -69,9 +65,9 @@ def services_speller(service): elif service == "disney": service_proper = "Disney+" elif service == "apple": - service_proper = "Apple TV+" + service_proper = "Apple TV\+" elif service == "paramount": - service_proper = "Paramount+" + service_proper = "Paramount\+" elif service == "starz": service_proper = "STARZ" elif service == "showtime": @@ -81,3 +77,13 @@ def services_speller(service): else: return service return service_proper + + +def char_cleanup(variable): + variable = str(variable).replace('-', '\-') + variable = str(variable).replace('(', '\(') + variable = str(variable).replace(')', '\)') + variable = str(variable).replace('+', '\+') + variable = str(variable).replace('.', '\.') + + return variable diff --git a/telegram-moviebot/telegram-moviebot.py b/telegram-moviebot/telegram-moviebot.py index 73c97d4..54792fa 100644 --- a/telegram-moviebot/telegram-moviebot.py +++ b/telegram-moviebot/telegram-moviebot.py @@ -1,6 +1,5 @@ #!/usr/bin/python3 -import telegram from telegram.ext import ( Updater, CommandHandler, @@ -58,14 +57,27 @@ def movie_lookup(movie): movie_id, movie_title, movie_year, movie_rating = ( movie_check.tmdb_lookup(tmdb_url, tmdb_headers, movie)) - movie_rating = str(movie_rating).replace('.', '\.') + if movie_id == "404": + tg_reply = ("I'm having trouble finding that movie\." + + "Check your spelling and try again\.") + return tg_reply + sa_response, services = movie_check.sa_lookup(sa_url, sa_headers, movie_id) + if sa_response == "404": + tg_reply = ("I'm having trouble finding that movie\." + + "Check your spelling and try again\.") + return tg_reply + + movie_title = movie_check.char_cleanup(movie_title) + movie_year = movie_check.char_cleanup(movie_year) + movie_rating = movie_check.char_cleanup(movie_rating) + tg_reply = (f"{movie_title} \({movie_year}\)\nRating: {movie_rating}" + f"\n[TMDB]({tmdb_page}{movie_id})") logger.info(f'Returning movie: "{movie_title}: ({movie_year})"') if not services: - tg_reply = tg_reply + "\n\nStreaming not available :\(" + tg_reply = tg_reply + "\n\nStreaming not available :(" else: for s in services: leaving_epoch = sa_response["streamingInfo"][s]["us"]["leaving"] @@ -77,16 +89,18 @@ def movie_lookup(movie): tg_reply = tg_reply + f"\n\nAvailable on *{s_pretty}*" if leaving_epoch != 0: - tg_reply = tg_reply + f"Will be leaving on {leaving_date}" + tg_reply = tg_reply + f"\nWill be leaving on {leaving_date}" tg_reply = tg_reply + f"\n[Watch here]({link})" + return tg_reply def input_movie(update: Update, context: CallbackContext): movie = update.message.text movie_info = movie_lookup(movie) - context.bot.send_message(chat_id=update.effective_chat.id, text=movie_info, parse_mode=telegram.ParseMode.MARKDOWN_V2) + context.bot.send_message(chat_id=update.effective_chat.id, + text=movie_info, parse_mode=ParseMode.MARKDOWN_V2) def unknown(update: Update, context: CallbackContext):