check match similarity and send message if below threshold

This commit is contained in:
2022-04-02 18:53:49 -04:00
parent dd2813332e
commit d1cd952d52

View File

@@ -11,6 +11,7 @@ from telegram import Update, ParseMode
import os import os
from datetime import datetime from datetime import datetime
import movie_check import movie_check
import difflib
tmdb_api_token = os.environ.get("TMDB_API_TOKEN") tmdb_api_token = os.environ.get("TMDB_API_TOKEN")
@@ -68,6 +69,11 @@ def movie_lookup(movie):
"Check your spelling and try again\.") "Check your spelling and try again\.")
return tg_reply return tg_reply
similarity = difflib.SequenceMatcher(None, movie, movie_title).ratio()
sim_percent = "{0:.0f}%".format(similarity * 100)
logger.info(f'Result was a {sim_percent} match.')
movie_title = movie_check.char_cleanup(movie_title) movie_title = movie_check.char_cleanup(movie_title)
movie_year = movie_check.char_cleanup(movie_year) movie_year = movie_check.char_cleanup(movie_year)
movie_rating = movie_check.char_cleanup(movie_rating) movie_rating = movie_check.char_cleanup(movie_rating)
@@ -93,14 +99,18 @@ 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 return tg_reply, similarity
def input_movie(update: Update, context: CallbackContext): def input_movie(update: Update, context: CallbackContext):
movie = update.message.text movie = update.message.text.title()
movie_info = movie_lookup(movie) movie_info, similarity = 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 similarity < .80:
followup_msg = "Not the movie you're looking for? Sorry, I have to implement a 'year' function\."
context.bot.send_message(chat_id=update.effective_chat.id,
text=followup_msg, parse_mode=ParseMode.MARKDOWN_V2)
def unknown(update: Update, context: CallbackContext): def unknown(update: Update, context: CallbackContext):