Changeset 9be02d9
- Timestamp:
- Oct 10, 2024, 4:12:00 AM (5 weeks ago)
- Branches:
- master
- Children:
- 8d2d5ac
- Parents:
- 6623428
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
commands/__init__.py
r6623428 r9be02d9 14 14 chooser_user, who_is_next 15 15 from .movie import movie, movies, remove_movies 16 from .event import create_event, list_events -
commands/movie.py
r6623428 r9be02d9 13 13 from telegram.ext import ContextTypes 14 14 from imdb import Cinemagoer 15 from pprint import pformat16 15 17 from utils import context_init 18 16 from utils import context_init, choose_next_user 17 from predicates import has_finished_event 19 18 from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \ 20 19 MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \ 21 MOVIE_REMOVE, MOVIE_SET 20 MOVIE_REMOVE, MOVIE_SET, MOVIES_LIST, NO_MOVIES, ADD_MORE_USERS 22 21 23 22 … … 31 30 context_init(context) 32 31 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] 34 38 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])) 35 43 36 44 if "@"+username != chooser: … … 51 59 except: 52 60 raise error.TelegramError(FETCHING_ERROR) 53 61 54 62 movie_dict = dict( 55 63 title=movie.data.get("title"), 56 64 id=movie.getID(), 57 65 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"), 59 70 ) 60 71 … … 76 87 context_init(context) 77 88 78 movies = context.chat_data["movies"].copy() 89 movies = context.chat_data["movies"] 90 91 movies_formatted: str = "" 79 92 80 93 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 ) 83 99 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) 85 104 86 105 … … 101 120 context.chat_data["movies"].remove(movie) 102 121 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"]) 104 123 ) 105 -
commands/users.py
r6623428 r9be02d9 16 16 from strings import USER_NOT_PROVIDED, USERS_ADDED, USERS_REMOVED, \ 17 17 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 20 from utils import context_init, create_users_string, normalize_username, \ 21 choose_next_user 22 from predicates import has_finished_event 21 23 22 24 async def set_users( … … 32 34 33 35 await update.message.reply_text(USER_SET) 36 34 37 35 38 async def add_users( … … 57 60 users = context.chat_data["users"] 58 61 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)) 60 69 61 70 … … 64 73 context: ContextTypes.DEFAULT_TYPE 65 74 ) -> None: 75 """ 76 This commands sets the next chooser if needed and shows current 77 """ 78 66 79 context_init(context) 67 80 68 81 users = context.chat_data["users"] 69 82 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])) 74 90 75 91 … … 119 135 context.chat_data["users"] = users 120 136 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 121 143 await update.message.reply_text(USER_CHOOSE.format(user=users[0])) 122 144 -
main.py
r6623428 r9be02d9 36 36 ) 37 37 38 38 39 async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None: 39 40 logging.error("Exception:", exc_info=context.error) … … 55 56 app.add_handler(CommandHandler("about", commands.about)) 56 57 58 # Event commands 59 app.add_handler(CommandHandler("event", commands.create_event)) 60 app.add_handler(CommandHandler("events", commands.list_events)) 61 57 62 # Movie commands 58 63 app.add_handler(CommandHandler("movie", commands.movie)) 59 64 app.add_handler(CommandHandler("movies", commands.movies)) 60 65 app.add_handler(CommandHandler("movies_remove", commands.remove_movies)) 61 66 62 67 # Users commands 63 68 app.add_handler(CommandHandler("set", commands.set_users)) 64 69 app.add_handler(CommandHandler("add", commands.add_users)) 65 70 app.add_handler(CommandHandler("list", commands.list_users)) 71 app.add_handler(CommandHandler("users", commands.list_users)) 66 72 app.add_handler(CommandHandler("remove", commands.remove_users)) 67 73 app.add_handler(CommandHandler("chooser", commands.chooser_user)) -
strings.py
r6623428 r9be02d9 13 13 14 14 MOVIE_ANOTHER_USER = "Movie should choose another user ({user})" 15 MOVIE_NOT_PROVIDED = "Movie is not provided"15 MOVIE_NOT_PROVIDED = "Movie IMDB id is not provided. Use /movie <imdb_id>" 16 16 MOVIE_NOT_FOUND = "Movie not found on IMDB" 17 17 EXPECTED_ONE_MOVIE = "Expected only one movie" … … 20 20 MOVIE_REMOVE = "Movie \"{title}\" with id {id} has been removed" 21 21 MOVIE_SET = "Movie \"{title}\" proposed by {user} is succesfully set as next to watch" 22 MOVIES_LIST = "\"{title}\" IMDB:{id} by {user} \n" 23 NO_MOVIES = "No movies" 22 24 23 25 USER_NOT_FOUND = "Provided user ({user}) not found. Check /list" … … 29 31 USER_ADD = "User {user} has been added" 30 32 USERS_ADDED = "Users have been added successfully. Use /list to view." 31 32 33 USER_REMOVE = "User {user} has been removed. Use /list to view." 33 34 USERS_REMOVED = "Users has been removed" 34 35 35 USER_SET = "Users have been set successfully. Use /list to view." 36 36 USER_CHOOSE = "Next movie should choose: {user}" 37 NO_USERS = "No user added. You can add users by /add <nickname1> <nickname2>" 38 39 EVENT_INVALID_DATETIME = """ 40 Can't parse provided datetime. It should comply template like this: 41 /event 1.08.2024/18:00 <where> 42 """ 43 EVENT_CANT_BE_IN_PAST = "Event can't happen in the past. You set {when}, but today is {today}" 44 EVENT_ARGS_NOT_PROVIDED = """ 45 Event 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 """ 49 EVENT_WHERE_NOT_PROVIDED = """ 50 Second <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 55 EVENT_CREATED = "Event at {when} was created" 56 EVENT_EDITED = "Event at {when} was modified" 57 EVENTS_LIST = "[{finished}] \"{movie}\" by {user} at {when} in {where}\n" 58 EVENT_MOVIE_NOT_CHOOSEN = "You should choose a film first using /movie <imdb_id>" 59 EVENT_MOVIE_NOT_SET = "<None>" 60 EVENT_USER_HAD_EVENT = "User {user} have already finished event, so he can't choose twice. See /next or /list" 61 NO_EVENTS = "No events" 37 62 38 63 UNDEFINED_ERROR = "Exception: something unexpected happened. Check the logs." -
utils.py
r6623428 r9be02d9 11 11 12 12 from telegram.ext import ContextTypes 13 13 from collections import deque 14 14 15 15 def context_init(context: ContextTypes.DEFAULT_TYPE): … … 24 24 context.chat_data["movies"]: list[dict] = [] 25 25 26 if "events" not in context.chat_data: 27 context.chat_data["events"]: list[dict] = [] 28 26 29 return context 27 30 … … 33 36 def create_users_string(users: list[str]) -> str: 34 37 return "`" + ", ".join(users) + "`" 38 39 40 def 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.