Changeset 6623428 in python-cinema-club-bot


Ignore:
Timestamp:
Oct 9, 2024, 10:46:19 PM (5 weeks ago)
Author:
Mikhail Kirillov <w96k@…>
Branches:
master
Children:
9be02d9
Parents:
0d48ea2
git-author:
Mikhail Kirillov <w96k@…> (10/09/24 04:53:26)
git-committer:
Mikhail Kirillov <w96k@…> (10/09/24 22:46:19)
Message:

Add movies commands

Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • commands/__init__.py

    r0d48ea2 r6623428  
    1111
    1212from .meta import about
    13 from .users import set_users, add_users, list_users, remove_users, chooser_user
     13from .users import set_users, add_users, list_users, remove_users, \
     14    chooser_user, who_is_next
     15from .movie import movie, movies, remove_movies
  • commands/users.py

    r0d48ea2 r6623428  
    1515
    1616from strings import USER_NOT_PROVIDED, USERS_ADDED, USERS_REMOVED, \
    17     EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE
    18 from utils import context_init
     17    EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE, ADD_MORE_USERS, \
     18    NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE
     19from utils import context_init, create_users_string
    1920
    2021
     
    5455    context_init(context)
    5556
    56     await update.message.reply_text(context.chat_data["users"])
     57    users = context.chat_data["users"]
     58
     59    await update.message.reply_markdown(create_users_string(users))
     60
     61
     62async def who_is_next(
     63        update: Update,
     64        context: ContextTypes.DEFAULT_TYPE
     65) -> None:
     66    context_init(context)
     67
     68    users = context.chat_data["users"]
     69
     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)
    5774
    5875
     
    92109    users = deque(context.chat_data["users"])
    93110
    94     chooser_index = users.index(chooser)
     111    try:
     112        chooser_index = users.index(chooser)
     113    except ValueError:
     114        raise error.TelegramError(USER_NOT_FOUND.format(user=chooser))
     115
    95116    users.rotate(-chooser_index)
     117    users = list(users)
    96118
    97     context.chat_data["users"] = list(users)
     119    context.chat_data["users"] = users
    98120
    99     await update.message.reply_text(context.chat_data["users"])
     121    await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
     122
     123    await update.message.reply_markdown(create_users_string(users))
  • main.py

    r0d48ea2 r6623428  
    1717import logging
    1818
    19 from strings import INVALID_COMMAND
     19from strings import INVALID_COMMAND, UNDEFINED_ERROR
    2020from persistence import Persistence
    2121import commands
     
    3939    logging.error("Exception:", exc_info=context.error)
    4040
     41    error_message = context.error.message \
     42        if hasattr(context.error, "message") else UNDEFINED_ERROR
     43
    4144    await context.bot.send_message(
    4245        chat_id=update.effective_chat.id,
    43         text=context.error.message
     46        text=error_message
    4447    )
    4548
     
    5356
    5457    # Movie commands
     58    app.add_handler(CommandHandler("movie", commands.movie))
     59    app.add_handler(CommandHandler("movies", commands.movies))
     60    app.add_handler(CommandHandler("movies_remove", commands.remove_movies))
     61   
     62    # Users commands
    5563    app.add_handler(CommandHandler("set", commands.set_users))
    5664    app.add_handler(CommandHandler("add", commands.add_users))
     
    5866    app.add_handler(CommandHandler("remove", commands.remove_users))
    5967    app.add_handler(CommandHandler("chooser", commands.chooser_user))
     68    app.add_handler(CommandHandler("next", commands.who_is_next))
    6069
    6170    app.add_handler(MessageHandler(filters.COMMAND, unknown))
  • requirements.txt

    r0d48ea2 r6623428  
    11python-dotenv==0.20.0
    22python-telegram-bot==21.6
     3cinemagoer==2023.5.1
  • strings.py

    r0d48ea2 r6623428  
    1111
    1212INVALID_COMMAND = "Invalid command. Available commands: /add /list /remove /chooser /about"
     13
     14MOVIE_ANOTHER_USER = "Movie should choose another user ({user})"
     15MOVIE_NOT_PROVIDED = "Movie is not provided"
     16MOVIE_NOT_FOUND = "Movie not found on IMDB"
     17EXPECTED_ONE_MOVIE = "Expected only one movie"
     18FETCHING_MOVIE = "Movie with ID {id} is being fetched: https://imdb.com/title/tt{id}/"
     19FETCHING_ERROR = "Couldn't fetch movie or it is not found. Provide IMDB id, for example: 0133093"
     20MOVIE_REMOVE = "Movie \"{title}\" with id {id} has been removed"
     21MOVIE_SET = "Movie \"{title}\" proposed by {user} is succesfully set as next to watch"
     22
     23USER_NOT_FOUND = "Provided user ({user}) not found. Check /list"
    1324USER_NOT_PROVIDED = "User(s) is not provided"
    1425EXPECTED_ONE_USER = "Expected only one user"
     26ADD_MORE_USERS = "There is no users added. You can add people who can choose movies using /add nickname"
     27NEXT_MOVIE_USER = "Next movie choice is up to {user}"
     28
    1529USER_ADD = "User {user} has been added"
    1630USERS_ADDED = "Users have been added successfully. Use /list to view."
     31
    1732USER_REMOVE = "User {user} has been removed. Use /list to view."
    1833USERS_REMOVED = "Users has been removed"
     34
    1935USER_SET = "Users have been set successfully. Use /list to view."
     36USER_CHOOSE = "Next movie should choose: {user}"
    2037
     38UNDEFINED_ERROR = "Exception: something unexpected happened. Check the logs."
  • utils.py

    r0d48ea2 r6623428  
    1414
    1515def context_init(context: ContextTypes.DEFAULT_TYPE):
    16     '''
     16    """
    1717    Initialize chat context with starting values
    18     '''
     18    """
    1919
    2020    if "users" not in context.chat_data:
    2121        context.chat_data["users"]: list[str] = []
    2222
     23    if "movies" not in context.chat_data:
     24        context.chat_data["movies"]: list[dict] = []
     25
    2326    return context
     27
     28
     29def normalize_username(username: str):
     30    return username.replace("@", "")
     31
     32
     33def create_users_string(users: list[str]) -> str:
     34    return "`" + ", ".join(users) + "`"
Note: See TracChangeset for help on using the changeset viewer.