summaryrefslogtreecommitdiff
path: root/commands/movie.py
diff options
context:
space:
mode:
authorMikhail Kirillov <w96k@runbox.com>2024-10-09 04:53:26 +0400
committerMikhail Kirillov <w96k@runbox.com>2024-10-09 22:46:19 +0400
commit662342863e37e8661f818218df7bb72bc18bf4eb (patch)
tree79ff9b3884a36e7afec820a8b7c3f235f2e6f1f1 /commands/movie.py
parent0d48ea28aeb8a6290d2aeab202efa9c8d6e30fe3 (diff)
Add movies commands
Diffstat (limited to 'commands/movie.py')
-rw-r--r--commands/movie.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/commands/movie.py b/commands/movie.py
new file mode 100644
index 0000000..b7be8cd
--- /dev/null
+++ b/commands/movie.py
@@ -0,0 +1,105 @@
+# This file is part of python-cinema-club-bot
+# contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>
+
+# To the extent possible under law, the author(s) have dedicated all copyright
+# and related and neighboring rights to this software to the public domain
+# worldwide. This software is distributed without any warranty.
+
+# You should have received a copy of the CC0 Public Domain Dedication along
+# with this software. If not, see:
+# <http://creativecommons.org/publicdomain/zero/1.0/>
+
+from telegram import Update, error
+from telegram.ext import ContextTypes
+from imdb import Cinemagoer
+from pprint import pformat
+
+from utils import context_init
+
+from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \
+ MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \
+ MOVIE_REMOVE, MOVIE_SET
+
+
+imdb = Cinemagoer()
+
+
+async def movie(
+ update: Update,
+ context: ContextTypes.DEFAULT_TYPE
+) -> None:
+ context_init(context)
+
+ chooser = context.chat_data["users"][0] or None
+ username = update.message.from_user.username
+
+ if "@"+username != chooser:
+ raise error.TelegramError(MOVIE_ANOTHER_USER.format(user=chooser))
+
+ if context.args == []:
+ raise error.TelegramError(MOVIE_NOT_PROVIDED)
+
+ if len(context.args) > 1:
+ raise error.TelegramError(EXPECTED_ONE_MOVIE)
+
+ movie_id = context.args[0]
+
+ await update.message.reply_text(FETCHING_MOVIE.format(id=movie_id))
+
+ try:
+ movie = imdb.get_movie(movie_id)
+ except:
+ raise error.TelegramError(FETCHING_ERROR)
+
+ movie_dict = dict(
+ title=movie.data.get("title"),
+ id=movie.getID(),
+ user=update.effective_user.username,
+ poster=movie.data.get("cover url")
+ )
+
+ if len(context.chat_data["movies"]) > 0 and \
+ context.chat_data["movies"][-1]["user"] == username:
+ context.chat_data["movies"][-1] = movie_dict
+ else:
+ context.chat_data["movies"].append(movie_dict)
+
+ await update.message.reply_text(
+ MOVIE_SET.format(title=movie_dict["title"], user=movie_dict["user"])
+ )
+
+
+async def movies(
+ update: Update,
+ context: ContextTypes.DEFAULT_TYPE
+) -> None:
+ context_init(context)
+
+ movies = context.chat_data["movies"].copy()
+
+ for movie in movies:
+ if movie["poster"] or None:
+ del movie["poster"]
+
+ await update.message.reply_text(pformat(movies))
+
+
+async def remove_movies(
+ update: Update,
+ context: ContextTypes.DEFAULT_TYPE
+) -> None:
+ context_init(context)
+
+ if context.args == []:
+ raise error.TelegramError(MOVIE_NOT_PROVIDED)
+
+ movies = context.chat_data["movies"]
+
+ for movie_id in context.args:
+ for movie in movies:
+ if movie["id"] == movie_id:
+ context.chat_data["movies"].remove(movie)
+ await update.message.reply_text(
+ MOVIE_REMOVE.format(title=movie["title"], id=["movie.id"])
+ )
+