From 9be02d9141c14a3e3ca28567dbaff671de83200f Mon Sep 17 00:00:00 2001 From: Mikhail Kirillov Date: Thu, 10 Oct 2024 04:12:00 +0400 Subject: Fix #9. Add events commands --- commands/movie.py | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'commands/movie.py') diff --git a/commands/movie.py b/commands/movie.py index b7be8cd..256156f 100644 --- a/commands/movie.py +++ b/commands/movie.py @@ -12,13 +12,12 @@ from telegram import Update, error from telegram.ext import ContextTypes from imdb import Cinemagoer -from pprint import pformat - -from utils import context_init +from utils import context_init, choose_next_user +from predicates import has_finished_event from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \ MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \ - MOVIE_REMOVE, MOVIE_SET + MOVIE_REMOVE, MOVIE_SET, MOVIES_LIST, NO_MOVIES, ADD_MORE_USERS imdb = Cinemagoer() @@ -30,9 +29,18 @@ async def movie( ) -> None: context_init(context) - chooser = context.chat_data["users"][0] or None + users = context.chat_data["users"] + + if users == []: + raise error.TelegramError(ADD_MORE_USERS) + + chooser = users[0] username = update.message.from_user.username + if has_finished_event(context, chooser): + users = context.chat_data["users"] = choose_next_user(users) + raise error.TelegramError(MOVIE_ANOTHER_USER.format(users[0])) + if "@"+username != chooser: raise error.TelegramError(MOVIE_ANOTHER_USER.format(user=chooser)) @@ -50,12 +58,15 @@ async def movie( 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") + poster=movie.data.get("cover url"), + rating=movie.data.get("rating"), + genres=movie.data.get("genres"), + runtime=movie.data.get("runtimes"), ) if len(context.chat_data["movies"]) > 0 and \ @@ -75,13 +86,21 @@ async def movies( ) -> None: context_init(context) - movies = context.chat_data["movies"].copy() + movies = context.chat_data["movies"] + + movies_formatted: str = "" for movie in movies: - if movie["poster"] or None: - del movie["poster"] + movies_formatted += MOVIES_LIST.format( + title=movie["title"], + id=movie["id"], + user=movie["user"] + ) - await update.message.reply_text(pformat(movies)) + if movies_formatted == "": + movies_formatted = NO_MOVIES + + await update.message.reply_text(movies_formatted) async def remove_movies( @@ -100,6 +119,5 @@ async def remove_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"]) + MOVIE_REMOVE.format(title=movie["title"], id=movie["id"]) ) - -- cgit v1.2.3