Changeset 9be02d9 in python-cinema-club-bot for commands/movie.py


Ignore:
Timestamp:
Oct 10, 2024, 4:12:00 AM (5 weeks ago)
Author:
Mikhail Kirillov <w96k@…>
Branches:
master
Children:
8d2d5ac
Parents:
6623428
Message:

Fix #9. Add events commands

File:
1 edited

Legend:

Unmodified
Added
Removed
  • commands/movie.py

    r6623428 r9be02d9  
    1313from telegram.ext import ContextTypes
    1414from imdb import Cinemagoer
    15 from pprint import pformat
    1615
    17 from utils import context_init
    18 
     16from utils import context_init, choose_next_user
     17from predicates import has_finished_event
    1918from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \
    2019    MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \
    21     MOVIE_REMOVE, MOVIE_SET
     20    MOVIE_REMOVE, MOVIE_SET, MOVIES_LIST, NO_MOVIES, ADD_MORE_USERS
    2221
    2322
     
    3130    context_init(context)
    3231
    33     chooser = context.chat_data["users"][0] or None
     32    users = context.chat_data["users"]
     33
     34    if users == []:
     35        raise error.TelegramError(ADD_MORE_USERS)
     36
     37    chooser = users[0]
    3438    username = update.message.from_user.username
     39
     40    if has_finished_event(context, chooser):
     41        users = context.chat_data["users"] = choose_next_user(users)
     42        raise error.TelegramError(MOVIE_ANOTHER_USER.format(users[0]))
    3543
    3644    if "@"+username != chooser:
     
    5159    except:
    5260        raise error.TelegramError(FETCHING_ERROR)
    53    
     61
    5462    movie_dict = dict(
    5563        title=movie.data.get("title"),
    5664        id=movie.getID(),
    5765        user=update.effective_user.username,
    58         poster=movie.data.get("cover url")
     66        poster=movie.data.get("cover url"),
     67        rating=movie.data.get("rating"),
     68        genres=movie.data.get("genres"),
     69        runtime=movie.data.get("runtimes"),
    5970    )
    6071
     
    7687    context_init(context)
    7788
    78     movies = context.chat_data["movies"].copy()
     89    movies = context.chat_data["movies"]
     90
     91    movies_formatted: str = ""
    7992
    8093    for movie in movies:
    81         if movie["poster"] or None:
    82             del movie["poster"]
     94        movies_formatted += MOVIES_LIST.format(
     95            title=movie["title"],
     96            id=movie["id"],
     97            user=movie["user"]
     98        )
    8399
    84     await update.message.reply_text(pformat(movies))
     100    if movies_formatted == "":
     101        movies_formatted = NO_MOVIES
     102
     103    await update.message.reply_text(movies_formatted)
    85104
    86105
     
    101120                context.chat_data["movies"].remove(movie)
    102121                await update.message.reply_text(
    103                     MOVIE_REMOVE.format(title=movie["title"], id=["movie.id"])
     122                    MOVIE_REMOVE.format(title=movie["title"], id=movie["id"])
    104123                )
    105 
Note: See TracChangeset for help on using the changeset viewer.