Changeset 69dd60c
- Timestamp:
- Oct 11, 2024, 1:53:24 AM (5 weeks ago)
- Branches:
- master
- Children:
- ac7b16a
- Parents:
- 694d823
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
commands/announce.py
r694d823 r69dd60c 13 13 from telegram.ext import ContextTypes 14 14 15 from strings import ANNOUNCEMENT_TEMPLATE, ANNOUNCEMENT_FINISHED_EVENT 15 from strings import ANNOUNCEMENT_TEMPLATE, ANNOUNCEMENT_FINISHED_EVENT, \ 16 ANNOUNCEMENT_EVENT_NEEDS_EDITING 16 17 from utils import context_init, create_users_string, choose_next_user 17 18 from predicates import has_unfinished_event … … 32 33 last_movie = movies[-1] 33 34 34 genres = " #".join(last_movie["genres"]) 35 if last_event["when"] is None or last_event["where"] is None or last_event["movie"] is None: 36 raise error.TelegramError(ANNOUNCEMENT_EVENT_NEEDS_EDITING) 37 38 genres ="#" + " #".join(last_movie["genres"]) 35 39 36 40 await update.message.reply_markdown( -
commands/event.py
r694d823 r69dd60c 14 14 from datetime import datetime 15 15 16 from utils import context_init 17 from predicates import is_past, has_finished_ event16 from utils import context_init, normalize_username 17 from predicates import is_past, has_finished_movie, has_event_without_movie 18 18 from strings import EVENT_ARGS_NOT_PROVIDED, EVENT_INVALID_DATETIME, \ 19 19 EVENT_CREATED, EVENT_EDITED, EVENT_CANT_BE_IN_PAST, \ 20 20 EVENT_WHERE_NOT_PROVIDED, EVENT_MOVIE_NOT_CHOOSEN, EVENTS_LIST, \ 21 EVENT_ USER_HAD_EVENT, NO_EVENTS21 EVENT_FINISHED, NO_EVENTS, EVENTS_LIST_PLANNING, EVENTS_LIST_MOVIE_SET 22 22 23 23 … … 27 27 ) -> None: 28 28 context_init(context) 29 users = context.chat_data["users"] 29 30 events = context.chat_data["events"] 30 31 movies = context.chat_data["movies"] 31 32 32 username = update.message.from_user.username33 33 last_movie = movies[-1] if movies != [] else None 34 34 35 if has_finished_event(context, username):36 raise error.TelegramError(EVENT_ USER_HAD_EVENT)35 if last_movie is None: 36 raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN) 37 37 38 if last_movie is None or last_movie["user"] != username:39 raise error.TelegramError(EVENT_ MOVIE_NOT_CHOOSEN)38 if has_finished_movie(context): 39 raise error.TelegramError(EVENT_FINISHED) 40 40 41 41 if context.args == []: … … 62 62 raise error.TelegramError(EVENT_WHERE_NOT_PROVIDED) 63 63 64 event_where = " ".join(arguments_where) 64 old_when = events[-2]["when"] if len(events) >= 2 else None 65 old_where = events[-2]["where"] if len(events) >= 2 else None 65 66 66 if event_where == "": 67 event_where = events[-1]["where"] 67 event_where = " ".join(arguments_where) if arguments_where != [] else old_where 68 68 69 69 event_dict = dict( … … 74 74 ) 75 75 76 if events == [] or is_past(events[-1]["when"]): 77 events.append(event_dict) 78 await update.message.reply_text( 79 EVENT_CREATED.format( 80 when=event_when, 81 movie=event_dict["movie"], 82 ) 76 events[-1] = event_dict 77 78 await update.message.reply_text( 79 EVENT_EDITED.format( 80 old_when=old_when, 81 old_where=old_where, 82 when=event_when, 83 where=event_where, 84 movie=event_dict["movie"], 83 85 ) 84 else: 85 old_when = events[-1]["when"] 86 old_where = events[-1]["where"] 87 events[-1] = event_dict 88 89 await update.message.reply_text( 90 EVENT_EDITED.format( 91 old_when=old_when, 92 old_where=old_where, 93 when=event_when, 94 where=event_where, 95 movie=event_dict["movie"], 96 ) 97 ) 86 ) 98 87 99 88 … … 104 93 context_init(context) 105 94 95 users = context.chat_data["users"] 106 96 events = context.chat_data["events"] 97 last_event = events[-1] if events != [] else None 98 99 if last_event and last_event["movie"] is None: 100 last_event["user"] = normalize_username(users[0]) 107 101 108 102 events_formatted: str = "" 109 103 110 104 for event in events: 111 event_ finished= "FINISHED" \105 event_status = "FINISHED" \ 112 106 if is_past(event["when"]) else "PLANNED" 113 107 114 events_formatted += EVENTS_LIST.format( 115 movie=event["movie"], 116 user=event["user"], 117 when=event["when"], 118 where=event["where"], 119 finished=event_finished 120 ) 108 if event["movie"] is None: 109 breakpoint() 110 event_status = "PLANNING" 111 events_formatted += EVENTS_LIST_PLANNING.format( 112 user=event["user"], 113 status=event_status 114 ) 115 elif event["when"] is None or event["where"] is None: 116 event_status = "PLANNING" 117 events_formatted += EVENTS_LIST_MOVIE_SET.format( 118 user=event["user"], 119 movie=event["movie"], 120 status=event_status 121 ) 122 else: 123 events_formatted += EVENTS_LIST.format( 124 movie=event["movie"], 125 user=event["user"], 126 when=event["when"], 127 where=event["where"], 128 status=event_status 129 ) 121 130 122 131 if events_formatted == "": -
commands/movie.py
r694d823 r69dd60c 14 14 from imdb import Cinemagoer 15 15 16 from utils import context_init, choose_next_user 17 from predicates import has_finished_event 16 from utils import context_init, choose_next_user, normalize_username, \ 17 init_new_event 18 from predicates import has_finished_movie 18 19 from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \ 19 20 MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \ … … 38 39 username = update.message.from_user.username 39 40 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])) 41 if has_finished_movie(context): 42 users = context.chat_data["users"] = choose_next_user(context) 43 43 44 44 if "@"+username != chooser: … … 64 64 id=movie.getID(), 65 65 user=update.effective_user.username, 66 poster=movie.data.get("cover url"),67 66 rating=movie.data.get("rating"), 68 67 genres=movie.data.get("genres"), … … 81 80 context.chat_data["events"][-1] = last_event 82 81 else: 82 init_new_event(context, users[0], movie_dict["title"]) 83 83 context.chat_data["movies"].append(movie_dict) 84 84 … … 101 101 movies_formatted += MOVIES_LIST.format( 102 102 title=movie["title"], 103 id=movie["id"],104 103 user=movie["user"] 105 104 ) -
commands/users.py
r694d823 r69dd60c 17 17 EXPECTED_ONE_USER, USER_SET, USER_REMOVE, ADD_MORE_USERS, \ 18 18 NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE, NO_USERS, \ 19 EVENT_USER_HAD_EVENT, NEXT_MOVIE 20 from utils import context_init, create_users_string, choose_next_user 21 from predicates import has_finished_event 19 NEXT_MOVIE 20 from utils import context_init, create_users_string, choose_next_user, \ 21 normalize_username 22 from predicates import has_finished_movie, has_event_without_movie 22 23 23 24 … … 61 62 if users == []: 62 63 await update.message.reply_text(NO_USERS) 63 else: 64 if has_finished_event(context, users[0]): 65 users = context.chat_data["users"] = choose_next_user(users) 64 return 66 65 67 await update.message.reply_markdown(create_users_string(users)) 66 if has_finished_movie(context): 67 users = context.chat_data["users"] = choose_next_user(context) 68 69 await update.message.reply_markdown(create_users_string(users)) 68 70 69 71 … … 83 85 raise error.TelegramError(ADD_MORE_USERS) 84 86 85 if has_finished_ event(context, users[0]):86 users = context.chat_data["users"] = choose_next_user( users)87 if has_finished_movie(context): 88 users = context.chat_data["users"] = choose_next_user(context) 87 89 88 90 next_user_string = NEXT_MOVIE_USER.format(user=users[0]) 89 91 90 last_movie = context.chat_data["movies"][ 0] \92 last_movie = context.chat_data["movies"][-1] \ 91 93 if context.chat_data["movies"] != [] else None 92 94 93 if last_movie and last_movie["user"] == users[0]:95 if last_movie and last_movie["user"] == normalize_username(users[0]): 94 96 next_user_string += NEXT_MOVIE.format(movie=last_movie["title"]) 95 97 … … 142 144 context.chat_data["users"] = users 143 145 144 if has_finished_ event(context, users[0]):145 await update.message.reply_text(146 EVENT_USER_HAD_EVENT.format(user=users[0]) 147 )148 users = context.chat_data["users"] = choose_next_user(users)146 if has_finished_movie(context): 147 users = context.chat_data["users"] = choose_next_user(context) 148 149 if has_event_without_movie(context): 150 context.chat_data["events"][-1]["user"] = users[0] 149 151 150 152 await update.message.reply_text(USER_CHOOSE.format(user=users[0])) -
predicates.py
r694d823 r69dd60c 12 12 from telegram.ext import ContextTypes 13 13 from datetime import datetime 14 15 from utils import normalize_username 14 from typing import Optional 16 15 17 16 18 def is_past(dt: datetime) -> bool: 17 def is_past(dt: Optional[datetime]) -> bool: 18 if dt is None: 19 return False 20 19 21 return dt < datetime.today() 20 22 21 23 22 def has_finished_event( 23 context: ContextTypes.DEFAULT_TYPE, 24 username: str 25 ) -> bool: 24 def has_finished_movie(context: ContextTypes.DEFAULT_TYPE) -> bool: 25 events = context.chat_data["events"] 26 movies = context.chat_data["movies"] 27 28 last_event = events[-1] if events != [] else None 29 last_movie = movies[-1] if movies != [] else None 30 31 return last_event is not None and last_movie is not None and \ 32 last_event["user"] == last_movie["user"] and \ 33 is_past(last_event["when"]) 34 35 36 def has_event_without_movie(context: ContextTypes.DEFAULT_TYPE) -> bool: 26 37 events = context.chat_data["events"] 27 38 last_event = events[-1] if events != [] else None 28 39 29 return last_event is not None and \ 30 last_event["user"] == normalize_username(username) and \ 31 is_past(last_event["when"]) 40 return last_event and last_event["movie"] is None 32 41 33 42 … … 36 45 last_event = events[-1] if events != [] else None 37 46 38 return False if last_event is None else not is_past(last_event["when"]) 47 if last_event is None: 48 return False 49 50 if last_event["movie"] is None: 51 return False 52 53 return not is_past(last_event["when"]) 54 -
strings.py
r694d823 r69dd60c 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"22 MOVIES_LIST = "\"{title}\" by {user} \n" 23 23 NO_MOVIES = "No movies. You can add movie if you are next to choose (see /next). Too add a movie use /movie <imdb_id>" 24 24 … … 55 55 EVENT_CREATED = "Event \"{movie}\" at {when} was created" 56 56 EVENT_EDITED = "Event \"{movie}\" at {old_when} in {old_where} was modified to {when} in {where}" 57 EVENTS_LIST = "[{finished}] \"{movie}\" by {user} at {when} in {where}\n" 57 EVENTS_LIST = "[{status}] \"{movie}\" by {user} at {when} in {where}\n" 58 EVENTS_LIST_MOVIE_SET = "[{status}] \"{movie}\" by {user}\n" 59 EVENTS_LIST_PLANNING = "[{status}] by {user}\n" 58 60 EVENT_MOVIE_NOT_CHOOSEN = "You should choose a film first using /movie <imdb_id>" 59 61 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" 62 EVENT_USER_HAD_MOVIE = "User {user} can't set the sa, so he can't choose twice. See /next or /list" 63 EVENT_FINISHED = "Event is finished. Use /next to see who chooses a film now." 61 64 NO_EVENTS = "No events." 62 65 63 66 UNDEFINED_ERROR = "Exception: something unexpected happened. Check the logs." 64 67 68 ANNOUNCEMENT_EVENT_NEEDS_EDITING = "Event is missing either Movie, When or Where fields. See /event" 65 69 ANNOUNCEMENT_FINISHED_EVENT = "There is no planned events. Create one using /event <when> <where>" 66 70 ANNOUNCEMENT_TEMPLATE = """ -
utils.py
r694d823 r69dd60c 12 12 from telegram.ext import ContextTypes 13 13 from collections import deque 14 from typing import Optional 14 15 15 16 def context_init(context: ContextTypes.DEFAULT_TYPE): … … 38 39 39 40 40 def choose_next_user( users: list[dict]) -> list[dict]:41 users = deque( users)41 def choose_next_user(context: ContextTypes.DEFAULT_TYPE) -> list[dict]: 42 users = deque(context.chat_data["users"]) 42 43 users.rotate(-1) # -1 moves list to left by 1 element 43 44 45 init_new_event(context, users[0]) 46 44 47 return list(users) 48 49 50 def init_new_event(context: ContextTypes.DEFAULT_TYPE, user: dict, movie: Optional[str] = None): 51 events = context.chat_data["events"] 52 53 last_event = events[-1] if events != [] else None 54 55 init_event = dict( 56 when=None, 57 where=None, 58 movie=movie, 59 user=user 60 ) 61 62 if last_event and last_event["movie"] is None: 63 events[-1] = init_event 64 else: 65 events.append(init_event)
Note:
See TracChangeset
for help on using the changeset viewer.