Changeset 9be02d9


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

Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • commands/__init__.py

    r6623428 r9be02d9  
    1414    chooser_user, who_is_next
    1515from .movie import movie, movies, remove_movies
     16from .event import create_event, list_events
  • 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 
  • commands/users.py

    r6623428 r9be02d9  
    1616from strings import USER_NOT_PROVIDED, USERS_ADDED, USERS_REMOVED, \
    1717    EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE, ADD_MORE_USERS, \
    18     NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE
    19 from utils import context_init, create_users_string
    20 
     18    NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE, NO_USERS, \
     19    EVENT_USER_HAD_EVENT
     20from utils import context_init, create_users_string, normalize_username, \
     21    choose_next_user
     22from predicates import has_finished_event
    2123
    2224async def set_users(
     
    3234
    3335    await update.message.reply_text(USER_SET)
     36
    3437
    3538async def add_users(
     
    5760    users = context.chat_data["users"]
    5861
    59     await update.message.reply_markdown(create_users_string(users))
     62    if users == []:
     63        await update.message.reply_text(NO_USERS)
     64    else:
     65        if has_finished_event(context, users[0]):
     66            users = context.chat_data["users"] = choose_next_user(users)
     67
     68        await update.message.reply_markdown(create_users_string(users))
    6069
    6170
     
    6473        context: ContextTypes.DEFAULT_TYPE
    6574) -> None:
     75    """
     76    This commands sets the next chooser if needed and shows current
     77    """
     78
    6679    context_init(context)
    6780
    6881    users = context.chat_data["users"]
    6982
    70     if len(users) > 0:
    71         await update.message.reply_text(NEXT_MOVIE_USER.format(user=users[0]))
    72     else:
    73         await update.message.reply_text(ADD_MORE_USERS)
     83    if users == []:
     84        raise error.TelegramError(ADD_MORE_USERS)
     85
     86    if has_finished_event(context, users[0]):
     87        users = context.chat_data["users"] = choose_next_user(users)
     88
     89    await update.message.reply_text(NEXT_MOVIE_USER.format(user=users[0]))
    7490
    7591
     
    119135    context.chat_data["users"] = users
    120136
     137    if has_finished_event(context, users[0]):
     138        await update.message.reply_text(
     139            EVENT_USER_HAD_EVENT.format(user=users[0])
     140        )
     141        users = context.chat_data["users"] = choose_next_user(users)
     142
    121143    await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
    122144
  • main.py

    r6623428 r9be02d9  
    3636    )
    3737
     38
    3839async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
    3940    logging.error("Exception:", exc_info=context.error)
     
    5556    app.add_handler(CommandHandler("about", commands.about))
    5657
     58    # Event commands
     59    app.add_handler(CommandHandler("event", commands.create_event))
     60    app.add_handler(CommandHandler("events", commands.list_events))
     61
    5762    # Movie commands
    5863    app.add_handler(CommandHandler("movie", commands.movie))
    5964    app.add_handler(CommandHandler("movies", commands.movies))
    6065    app.add_handler(CommandHandler("movies_remove", commands.remove_movies))
    61    
     66
    6267    # Users commands
    6368    app.add_handler(CommandHandler("set", commands.set_users))
    6469    app.add_handler(CommandHandler("add", commands.add_users))
    6570    app.add_handler(CommandHandler("list", commands.list_users))
     71    app.add_handler(CommandHandler("users", commands.list_users))
    6672    app.add_handler(CommandHandler("remove", commands.remove_users))
    6773    app.add_handler(CommandHandler("chooser", commands.chooser_user))
  • strings.py

    r6623428 r9be02d9  
    1313
    1414MOVIE_ANOTHER_USER = "Movie should choose another user ({user})"
    15 MOVIE_NOT_PROVIDED = "Movie is not provided"
     15MOVIE_NOT_PROVIDED = "Movie IMDB id is not provided. Use /movie <imdb_id>"
    1616MOVIE_NOT_FOUND = "Movie not found on IMDB"
    1717EXPECTED_ONE_MOVIE = "Expected only one movie"
     
    2020MOVIE_REMOVE = "Movie \"{title}\" with id {id} has been removed"
    2121MOVIE_SET = "Movie \"{title}\" proposed by {user} is succesfully set as next to watch"
     22MOVIES_LIST = "\"{title}\" IMDB:{id} by {user} \n"
     23NO_MOVIES = "No movies"
    2224
    2325USER_NOT_FOUND = "Provided user ({user}) not found. Check /list"
     
    2931USER_ADD = "User {user} has been added"
    3032USERS_ADDED = "Users have been added successfully. Use /list to view."
    31 
    3233USER_REMOVE = "User {user} has been removed. Use /list to view."
    3334USERS_REMOVED = "Users has been removed"
    34 
    3535USER_SET = "Users have been set successfully. Use /list to view."
    3636USER_CHOOSE = "Next movie should choose: {user}"
     37NO_USERS = "No user added. You can add users by /add <nickname1> <nickname2>"
     38
     39EVENT_INVALID_DATETIME = """
     40Can't parse provided datetime. It should comply template like this:
     41/event 1.08.2024/18:00 <where>
     42"""
     43EVENT_CANT_BE_IN_PAST = "Event can't happen in the past. You set {when}, but today is {today}"
     44EVENT_ARGS_NOT_PROVIDED = """
     45Event commands need arguments provided:
     46/event <when> <where>
     47/event 1.08.2024/18:00 84 Erevan Str 1st entrance 7 floor 451 apartment
     48"""
     49EVENT_WHERE_NOT_PROVIDED = """
     50Second <where> argument is not provided and previous event doesn't has it.
     51/event <when> <where>
     52/event 1.08.2024/18:00 84 Erevan Str 1st entrance 7 floor 451 apartment
     53"""
     54
     55EVENT_CREATED = "Event at {when} was created"
     56EVENT_EDITED = "Event at {when} was modified"
     57EVENTS_LIST = "[{finished}] \"{movie}\" by {user} at {when} in {where}\n"
     58EVENT_MOVIE_NOT_CHOOSEN = "You should choose a film first using /movie <imdb_id>"
     59EVENT_MOVIE_NOT_SET = "<None>"
     60EVENT_USER_HAD_EVENT = "User {user} have already finished event, so he can't choose twice. See /next or /list"
     61NO_EVENTS = "No events"
    3762
    3863UNDEFINED_ERROR = "Exception: something unexpected happened. Check the logs."
  • utils.py

    r6623428 r9be02d9  
    1111
    1212from telegram.ext import ContextTypes
    13 
     13from collections import deque
    1414
    1515def context_init(context: ContextTypes.DEFAULT_TYPE):
     
    2424        context.chat_data["movies"]: list[dict] = []
    2525
     26    if "events" not in context.chat_data:
     27        context.chat_data["events"]: list[dict] = []
     28
    2629    return context
    2730
     
    3336def create_users_string(users: list[str]) -> str:
    3437    return "`" + ", ".join(users) + "`"
     38
     39
     40def choose_next_user(users: list[dict]) -> list[dict]:
     41    users = deque(users)
     42    users.rotate(-1)  # -1 moves list to left by 1 element
     43
     44    return list(users)
Note: See TracChangeset for help on using the changeset viewer.