Merge pull request #2 from skoobasteeve/docker-compose

Docker compose
This commit is contained in:
Ray Lyon
2022-04-23 14:53:10 -04:00
committed by GitHub
3 changed files with 63 additions and 8 deletions

11
docker-compose.yml Normal file
View File

@@ -0,0 +1,11 @@
---
version: 3
services:
telegram-moviebot:
image: skoobasteeve/telegram-moviebot:main
environment:
- TMDB_API_TOKEN=""
- SA_API_TOKEN=""
- TG_BOT_TOKEN=""
- TG_BOT_USER="" # (optional) Limits access to the bot to a single Telegram user
restart: always

View File

@@ -19,7 +19,12 @@ def tmdb_lookup(tmdb_url, tmdb_headers, movie, year=None):
tmdb_params["primary_release_year"] = year
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"]:
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,
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"
else:
sa_response = sa_request.json()

View File

@@ -13,12 +13,11 @@ from datetime import datetime
import movie_check
import difflib
tmdb_api_token = os.environ.get("TMDB_API_TOKEN")
sa_api_token = os.environ.get("SA_API_TOKEN")
bot_token = os.environ.get("TG_BOT_TOKEN")
filter_user = ""
filter_user = os.environ.get("TG_BOT_USER")
tmdb_url = "https://api.themoviedb.org/3"
tmdb_headers = {
@@ -43,6 +42,11 @@ logging.basicConfig(
logger = logging.getLogger(__name__)
def shutdown():
updater.stop()
updater.is_idle = False
def start(update: Update, context: CallbackContext):
movie_handler = MessageHandler(Filters.text & (~Filters.command),
input_movie)
@@ -72,7 +76,16 @@ def movie_lookup(movie):
"Check your spelling and try again\.")
logger.info('Movie not found in TMDB.')
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)
if sa_response == "404":
@@ -80,7 +93,16 @@ def movie_lookup(movie):
"Check your spelling and try again\.")
logger.info('Movie not found by the Streaming Availability API.')
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()
sim_percent = "{0:.0f}%".format(similarity * 100)
@@ -112,14 +134,17 @@ def movie_lookup(movie):
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):
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,
text=movie_info, parse_mode=ParseMode.MARKDOWN_V2)
if error_response:
shutdown()
if similarity < .80 and similarity != 0:
logger.info("Result accuracy was below the threshold. Sending follow-up message.")
followup_msg = ("Not the movie you're looking for? " +
@@ -135,6 +160,18 @@ def unknown(update: Update, context: CallbackContext):
def main():
if not tmdb_api_token:
print("ERROR: TMDB API token not provided. Exiting...")
exit()
if not sa_api_token:
print("ERROR: Streaming Availability API token not provided. Exiting...")
exit()
if not bot_token:
print("ERROR: Telegram bot token not provided. Exiting...")
exit()
if filter_user:
start_handler = CommandHandler('start', start,
Filters.user(username=filter_user))