summaryrefslogtreecommitdiff
path: root/commands
diff options
context:
space:
mode:
authorMikhail Kirillov <w96k@runbox.com>2024-10-10 04:12:00 +0400
committerMikhail Kirillov <w96k@runbox.com>2024-10-10 04:12:00 +0400
commit9be02d9141c14a3e3ca28567dbaff671de83200f (patch)
treeecc8c2f7023654179d51e2fdee68b1d688c131d8 /commands
parent662342863e37e8661f818218df7bb72bc18bf4eb (diff)
Fix #9. Add events commands
Diffstat (limited to 'commands')
-rw-r--r--commands/__init__.py1
-rw-r--r--commands/event.py110
-rw-r--r--commands/movie.py44
-rw-r--r--commands/users.py38
4 files changed, 172 insertions, 21 deletions
diff --git a/commands/__init__.py b/commands/__init__.py
index 09c0867..2e0e009 100644
--- a/commands/__init__.py
+++ b/commands/__init__.py
@@ -13,3 +13,4 @@ from .meta import about
from .users import set_users, add_users, list_users, remove_users, \
chooser_user, who_is_next
from .movie import movie, movies, remove_movies
+from .event import create_event, list_events
diff --git a/commands/event.py b/commands/event.py
new file mode 100644
index 0000000..47c34ea
--- /dev/null
+++ b/commands/event.py
@@ -0,0 +1,110 @@
+# This file is part of python-cinema-club-bot
+# contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>
+
+# To the extent possible under law, the author(s) have dedicated all copyright
+# and related and neighboring rights to this software to the public domain
+# worldwide. This software is distributed without any warranty.
+
+# You should have received a copy of the CC0 Public Domain Dedication along
+# with this software. If not, see:
+# <http://creativecommons.org/publicdomain/zero/1.0/>
+
+from telegram import Update, error
+from telegram.ext import ContextTypes
+from datetime import datetime
+
+from utils import context_init
+from predicates import is_past, has_finished_event
+from strings import EVENT_ARGS_NOT_PROVIDED, EVENT_INVALID_DATETIME, \
+ EVENT_CREATED, EVENT_EDITED, EVENT_CANT_BE_IN_PAST, \
+ EVENT_WHERE_NOT_PROVIDED, EVENT_MOVIE_NOT_CHOOSEN, EVENTS_LIST, \
+ EVENT_USER_HAD_EVENT, NO_EVENTS
+
+
+async def create_event(
+ update: Update,
+ context: ContextTypes.DEFAULT_TYPE
+) -> None:
+ context_init(context)
+ events = context.chat_data["events"]
+ movies = context.chat_data["movies"]
+
+ username = update.message.from_user.username
+ last_movie = movies[-1] if movies != [] else None
+
+ if has_finished_event(context, username):
+ raise error.TelegramError(EVENT_USER_HAD_EVENT)
+
+ if last_movie is None or last_movie["user"] != username:
+ raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN)
+
+ if context.args == []:
+ raise error.TelegramError(EVENT_ARGS_NOT_PROVIDED)
+
+ argument_when = context.args[0]
+
+ try:
+ event_when = datetime.strptime(argument_when, "%d.%m.%Y/%H:%M")
+ except ValueError:
+ raise error.TelegramError(EVENT_INVALID_DATETIME)
+
+ if is_past(event_when):
+ raise error.TelegramError(
+ EVENT_CANT_BE_IN_PAST.format(
+ when=event_when,
+ today=datetime.today(),
+ )
+ )
+
+ arguments_where = context.args[1:]
+
+ if arguments_where == [] and events == []:
+ raise error.TelegramError(EVENT_WHERE_NOT_PROVIDED)
+
+ event_where = " ".join(arguments_where)
+
+ if event_where == "":
+ event_where = events[-1]["where"]
+
+ event_dict = dict(
+ when=event_when,
+ where=event_where,
+ movie=last_movie["title"],
+ user=last_movie["user"]
+ )
+
+ if events == [] or is_past(events[-1]["when"]):
+ events.append(event_dict)
+ await update.message.reply_text(EVENT_CREATED.format(when=event_when))
+ else:
+ events[-1] = event_dict
+ await update.message.reply_text(EVENT_EDITED.format(when=event_when))
+
+
+async def list_events(
+ update: Update,
+ context: ContextTypes.DEFAULT_TYPE
+) -> None:
+ context_init(context)
+
+ events = context.chat_data["events"]
+ movies = context.chat_data["movies"]
+
+ events_formatted: str = ""
+
+ for event in events:
+ event_finished = "FINISHED" \
+ if is_past(event["when"]) else "PLANNED"
+
+ events_formatted += EVENTS_LIST.format(
+ movie=event["movie"],
+ user=event["user"],
+ when=event["when"],
+ where=event["where"],
+ finished=event_finished
+ )
+
+ if events_formatted == "":
+ events_formatted = NO_EVENTS
+
+ await update.message.reply_text(events_formatted)
diff --git a/commands/movie.py b/commands/movie.py
index b7be8cd..256156f 100644
--- a/commands/movie.py
+++ b/commands/movie.py
@@ -12,13 +12,12 @@
from telegram import Update, error
from telegram.ext import ContextTypes
from imdb import Cinemagoer
-from pprint import pformat
-
-from utils import context_init
+from utils import context_init, choose_next_user
+from predicates import has_finished_event
from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \
MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \
- MOVIE_REMOVE, MOVIE_SET
+ MOVIE_REMOVE, MOVIE_SET, MOVIES_LIST, NO_MOVIES, ADD_MORE_USERS
imdb = Cinemagoer()
@@ -30,9 +29,18 @@ async def movie(
) -> None:
context_init(context)
- chooser = context.chat_data["users"][0] or None
+ users = context.chat_data["users"]
+
+ if users == []:
+ raise error.TelegramError(ADD_MORE_USERS)
+
+ chooser = users[0]
username = update.message.from_user.username
+ if has_finished_event(context, chooser):
+ users = context.chat_data["users"] = choose_next_user(users)
+ raise error.TelegramError(MOVIE_ANOTHER_USER.format(users[0]))
+
if "@"+username != chooser:
raise error.TelegramError(MOVIE_ANOTHER_USER.format(user=chooser))
@@ -50,12 +58,15 @@ async def movie(
movie = imdb.get_movie(movie_id)
except:
raise error.TelegramError(FETCHING_ERROR)
-
+
movie_dict = dict(
title=movie.data.get("title"),
id=movie.getID(),
user=update.effective_user.username,
- poster=movie.data.get("cover url")
+ poster=movie.data.get("cover url"),
+ rating=movie.data.get("rating"),
+ genres=movie.data.get("genres"),
+ runtime=movie.data.get("runtimes"),
)
if len(context.chat_data["movies"]) > 0 and \
@@ -75,13 +86,21 @@ async def movies(
) -> None:
context_init(context)
- movies = context.chat_data["movies"].copy()
+ movies = context.chat_data["movies"]
+
+ movies_formatted: str = ""
for movie in movies:
- if movie["poster"] or None:
- del movie["poster"]
+ movies_formatted += MOVIES_LIST.format(
+ title=movie["title"],
+ id=movie["id"],
+ user=movie["user"]
+ )
- await update.message.reply_text(pformat(movies))
+ if movies_formatted == "":
+ movies_formatted = NO_MOVIES
+
+ await update.message.reply_text(movies_formatted)
async def remove_movies(
@@ -100,6 +119,5 @@ async def remove_movies(
if movie["id"] == movie_id:
context.chat_data["movies"].remove(movie)
await update.message.reply_text(
- MOVIE_REMOVE.format(title=movie["title"], id=["movie.id"])
+ MOVIE_REMOVE.format(title=movie["title"], id=movie["id"])
)
-
diff --git a/commands/users.py b/commands/users.py
index 497d7dd..9dcc125 100644
--- a/commands/users.py
+++ b/commands/users.py
@@ -15,9 +15,11 @@ from collections import deque
from strings import USER_NOT_PROVIDED, USERS_ADDED, USERS_REMOVED, \
EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE, ADD_MORE_USERS, \
- NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE
-from utils import context_init, create_users_string
-
+ NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE, NO_USERS, \
+ EVENT_USER_HAD_EVENT
+from utils import context_init, create_users_string, normalize_username, \
+ choose_next_user
+from predicates import has_finished_event
async def set_users(
update: Update,
@@ -32,6 +34,7 @@ async def set_users(
await update.message.reply_text(USER_SET)
+
async def add_users(
update: Update,
context: ContextTypes.DEFAULT_TYPE
@@ -56,21 +59,34 @@ async def list_users(
users = context.chat_data["users"]
- await update.message.reply_markdown(create_users_string(users))
+ if users == []:
+ await update.message.reply_text(NO_USERS)
+ else:
+ if has_finished_event(context, users[0]):
+ users = context.chat_data["users"] = choose_next_user(users)
+
+ await update.message.reply_markdown(create_users_string(users))
async def who_is_next(
update: Update,
context: ContextTypes.DEFAULT_TYPE
) -> None:
+ """
+ This commands sets the next chooser if needed and shows current
+ """
+
context_init(context)
users = context.chat_data["users"]
- if len(users) > 0:
- await update.message.reply_text(NEXT_MOVIE_USER.format(user=users[0]))
- else:
- await update.message.reply_text(ADD_MORE_USERS)
+ if users == []:
+ raise error.TelegramError(ADD_MORE_USERS)
+
+ if has_finished_event(context, users[0]):
+ users = context.chat_data["users"] = choose_next_user(users)
+
+ await update.message.reply_text(NEXT_MOVIE_USER.format(user=users[0]))
async def remove_users(
@@ -118,6 +134,12 @@ async def chooser_user(
context.chat_data["users"] = users
+ if has_finished_event(context, users[0]):
+ await update.message.reply_text(
+ EVENT_USER_HAD_EVENT.format(user=users[0])
+ )
+ users = context.chat_data["users"] = choose_next_user(users)
+
await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
await update.message.reply_markdown(create_users_string(users))