mirror of
https://github.com/skoobasteeve/telegram-moviebot.git
synced 2026-03-22 04:28:57 +00:00
-input sanitization
-return similarity 0 if movie not found
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
def tmdb_lookup(tmdb_url, tmdb_headers, movie):
|
def tmdb_lookup(tmdb_url, tmdb_headers, movie):
|
||||||
|
|
||||||
|
movie = re.sub('[^a-zA-Z.\d\s]', '', movie)
|
||||||
tmdb_params = {
|
tmdb_params = {
|
||||||
"language": "en-US",
|
"language": "en-US",
|
||||||
"query": movie,
|
"query": movie,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import os
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import movie_check
|
import movie_check
|
||||||
import difflib
|
import difflib
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
tmdb_api_token = os.environ.get("TMDB_API_TOKEN")
|
tmdb_api_token = os.environ.get("TMDB_API_TOKEN")
|
||||||
@@ -59,15 +60,17 @@ def movie_lookup(movie):
|
|||||||
movie_check.tmdb_lookup(tmdb_url, tmdb_headers, movie))
|
movie_check.tmdb_lookup(tmdb_url, tmdb_headers, movie))
|
||||||
|
|
||||||
if movie_id == "404":
|
if movie_id == "404":
|
||||||
tg_reply = ("I'm having trouble finding that movie\." +
|
tg_reply = ("I'm having trouble finding that movie\. " +
|
||||||
"Check your spelling and try again\.")
|
"Check your spelling and try again\.")
|
||||||
return tg_reply
|
similarity = 0
|
||||||
|
return tg_reply, similarity
|
||||||
|
|
||||||
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":
|
||||||
tg_reply = ("I'm having trouble finding that movie\." +
|
tg_reply = ("I'm having trouble finding that movie\. " +
|
||||||
"Check your spelling and try again\.")
|
"Check your spelling and try again\.")
|
||||||
return tg_reply
|
similarity = 0
|
||||||
|
return tg_reply, similarity
|
||||||
|
|
||||||
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)
|
||||||
@@ -83,7 +86,7 @@ def movie_lookup(movie):
|
|||||||
logger.info(f'Returning movie: "{movie_title}: ({movie_year})"')
|
logger.info(f'Returning movie: "{movie_title}: ({movie_year})"')
|
||||||
|
|
||||||
if not services:
|
if not services:
|
||||||
tg_reply = tg_reply + "\n\nStreaming not available :("
|
tg_reply = tg_reply + "\n\nStreaming not available :\("
|
||||||
else:
|
else:
|
||||||
for s in services:
|
for s in services:
|
||||||
leaving_epoch = sa_response["streamingInfo"][s]["us"]["leaving"]
|
leaving_epoch = sa_response["streamingInfo"][s]["us"]["leaving"]
|
||||||
@@ -107,7 +110,7 @@ def input_movie(update: Update, context: CallbackContext):
|
|||||||
movie_info, similarity = 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:
|
if similarity < .80 and similarity != 0:
|
||||||
followup_msg = "Not the movie you're looking for? Sorry, I have to implement a 'year' function\."
|
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,
|
context.bot.send_message(chat_id=update.effective_chat.id,
|
||||||
text=followup_msg, parse_mode=ParseMode.MARKDOWN_V2)
|
text=followup_msg, parse_mode=ParseMode.MARKDOWN_V2)
|
||||||
|
|||||||
Reference in New Issue
Block a user