mirror of
https://github.com/skoobasteeve/telegram-moviebot.git
synced 2026-03-20 03:28:57 +00:00
error handling for invalid api tokens
This commit is contained in:
@@ -19,7 +19,12 @@ def tmdb_lookup(tmdb_url, tmdb_headers, movie, year=None):
|
|||||||
tmdb_params["primary_release_year"] = year
|
tmdb_params["primary_release_year"] = year
|
||||||
|
|
||||||
tmdb_search = requests.get(f"{tmdb_url}/search/movie", params=tmdb_params,
|
tmdb_search = requests.get(f"{tmdb_url}/search/movie", params=tmdb_params,
|
||||||
headers=tmdb_headers).json()
|
headers=tmdb_headers)
|
||||||
|
|
||||||
|
if tmdb_search.status_code == 401:
|
||||||
|
return "401", "401", "401", "401"
|
||||||
|
|
||||||
|
tmdb_search = tmdb_search.json()
|
||||||
|
|
||||||
if not tmdb_search["results"]:
|
if not tmdb_search["results"]:
|
||||||
return "404", "404", "404", "404"
|
return "404", "404", "404", "404"
|
||||||
@@ -50,7 +55,9 @@ def sa_lookup(sa_url, sa_headers, movie_id):
|
|||||||
sa_request = requests.request("GET", sa_url, headers=sa_headers,
|
sa_request = requests.request("GET", sa_url, headers=sa_headers,
|
||||||
params=sa_params)
|
params=sa_params)
|
||||||
|
|
||||||
if sa_request.status_code == 404:
|
if sa_request.status_code == 401:
|
||||||
|
sa_response = "401"
|
||||||
|
elif sa_request.status_code == 404:
|
||||||
sa_response = "404"
|
sa_response = "404"
|
||||||
else:
|
else:
|
||||||
sa_response = sa_request.json()
|
sa_response = sa_request.json()
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ from datetime import datetime
|
|||||||
import movie_check
|
import movie_check
|
||||||
import difflib
|
import difflib
|
||||||
|
|
||||||
|
|
||||||
tmdb_api_token = os.environ.get("TMDB_API_TOKEN")
|
tmdb_api_token = os.environ.get("TMDB_API_TOKEN")
|
||||||
sa_api_token = os.environ.get("SA_API_TOKEN")
|
sa_api_token = os.environ.get("SA_API_TOKEN")
|
||||||
bot_token = os.environ.get("TG_BOT_TOKEN")
|
bot_token = os.environ.get("TG_BOT_TOKEN")
|
||||||
@@ -43,6 +42,11 @@ logging.basicConfig(
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def shutdown():
|
||||||
|
updater.stop()
|
||||||
|
updater.is_idle = False
|
||||||
|
|
||||||
|
|
||||||
def start(update: Update, context: CallbackContext):
|
def start(update: Update, context: CallbackContext):
|
||||||
movie_handler = MessageHandler(Filters.text & (~Filters.command),
|
movie_handler = MessageHandler(Filters.text & (~Filters.command),
|
||||||
input_movie)
|
input_movie)
|
||||||
@@ -72,7 +76,16 @@ def movie_lookup(movie):
|
|||||||
"Check your spelling and try again\.")
|
"Check your spelling and try again\.")
|
||||||
logger.info('Movie not found in TMDB.')
|
logger.info('Movie not found in TMDB.')
|
||||||
similarity = 0
|
similarity = 0
|
||||||
return tg_reply, similarity
|
error_response = False
|
||||||
|
return tg_reply, similarity, error_response
|
||||||
|
|
||||||
|
if movie_id == "401":
|
||||||
|
tg_reply = ("Invalid TMDB API token\. " +
|
||||||
|
"Bot shutting down until restarted\.\.\.")
|
||||||
|
logger.info('Invalid TMDB API token. Exiting...')
|
||||||
|
similarity = 0
|
||||||
|
error_response = True
|
||||||
|
return tg_reply, similarity, error_response
|
||||||
|
|
||||||
sa_response, services = movie_check.sa_lookup(sa_url, sa_headers, movie_id)
|
sa_response, services = movie_check.sa_lookup(sa_url, sa_headers, movie_id)
|
||||||
if sa_response == "404":
|
if sa_response == "404":
|
||||||
@@ -80,7 +93,16 @@ def movie_lookup(movie):
|
|||||||
"Check your spelling and try again\.")
|
"Check your spelling and try again\.")
|
||||||
logger.info('Movie not found by the Streaming Availability API.')
|
logger.info('Movie not found by the Streaming Availability API.')
|
||||||
similarity = 0
|
similarity = 0
|
||||||
return tg_reply, similarity
|
error_response = False
|
||||||
|
return tg_reply, similarity, error_response
|
||||||
|
|
||||||
|
if sa_response == "401":
|
||||||
|
tg_reply = ("Invalid Streaming Availability API token\. " +
|
||||||
|
"Bot shutting down until restarted\.\.\.")
|
||||||
|
logger.info('Invalid Streaming Availability API token. Exiting...')
|
||||||
|
similarity = 0
|
||||||
|
error_response = True
|
||||||
|
return tg_reply, similarity, error_response
|
||||||
|
|
||||||
similarity = difflib.SequenceMatcher(None, movie, movie_title).ratio()
|
similarity = difflib.SequenceMatcher(None, movie, movie_title).ratio()
|
||||||
sim_percent = "{0:.0f}%".format(similarity * 100)
|
sim_percent = "{0:.0f}%".format(similarity * 100)
|
||||||
@@ -112,14 +134,17 @@ def movie_lookup(movie):
|
|||||||
|
|
||||||
tg_reply = tg_reply + f"\n[Watch here]({link})"
|
tg_reply = tg_reply + f"\n[Watch here]({link})"
|
||||||
|
|
||||||
return tg_reply, similarity
|
error_response = False
|
||||||
|
return tg_reply, similarity, error_response
|
||||||
|
|
||||||
|
|
||||||
def input_movie(update: Update, context: CallbackContext):
|
def input_movie(update: Update, context: CallbackContext):
|
||||||
movie = update.message.text.title()
|
movie = update.message.text.title()
|
||||||
movie_info, similarity = movie_lookup(movie)
|
movie_info, similarity, error_response = movie_lookup(movie)
|
||||||
context.bot.send_message(chat_id=update.effective_chat.id,
|
context.bot.send_message(chat_id=update.effective_chat.id,
|
||||||
text=movie_info, parse_mode=ParseMode.MARKDOWN_V2)
|
text=movie_info, parse_mode=ParseMode.MARKDOWN_V2)
|
||||||
|
if error_response:
|
||||||
|
shutdown()
|
||||||
if similarity < .80 and similarity != 0:
|
if similarity < .80 and similarity != 0:
|
||||||
logger.info("Result accuracy was below the threshold. Sending follow-up message.")
|
logger.info("Result accuracy was below the threshold. Sending follow-up message.")
|
||||||
followup_msg = ("Not the movie you're looking for? " +
|
followup_msg = ("Not the movie you're looking for? " +
|
||||||
|
|||||||
Reference in New Issue
Block a user