source: python-cinema-club-bot/commands/movie.py

Last change on this file was 69dd60c, checked in by Mikhail Kirillov <w96k@…>, on Oct 11, 2024 at 1:53:24 AM

Task #30. Allow anyone create or edit upcoming event

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[6623428]1# This file is part of python-cinema-club-bot
2# contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>
3
4# To the extent possible under law, the author(s) have dedicated all copyright
5# and related and neighboring rights to this software to the public domain
6# worldwide. This software is distributed without any warranty.
7
8# You should have received a copy of the CC0 Public Domain Dedication along
9# with this software. If not, see:
10# <http://creativecommons.org/publicdomain/zero/1.0/>
11
12from telegram import Update, error
13from telegram.ext import ContextTypes
14from imdb import Cinemagoer
15
[69dd60c]16from utils import context_init, choose_next_user, normalize_username, \
17 init_new_event
18from predicates import has_finished_movie
[6623428]19from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \
20 MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \
[9be02d9]21 MOVIE_REMOVE, MOVIE_SET, MOVIES_LIST, NO_MOVIES, ADD_MORE_USERS
[6623428]22
23
24imdb = Cinemagoer()
25
26
27async def movie(
28 update: Update,
29 context: ContextTypes.DEFAULT_TYPE
30) -> None:
31 context_init(context)
32
[9be02d9]33 users = context.chat_data["users"]
34
35 if users == []:
36 raise error.TelegramError(ADD_MORE_USERS)
37
38 chooser = users[0]
[6623428]39 username = update.message.from_user.username
40
[69dd60c]41 if has_finished_movie(context):
42 users = context.chat_data["users"] = choose_next_user(context)
[9be02d9]43
[6623428]44 if "@"+username != chooser:
45 raise error.TelegramError(MOVIE_ANOTHER_USER.format(user=chooser))
46
47 if context.args == []:
48 raise error.TelegramError(MOVIE_NOT_PROVIDED)
49
50 if len(context.args) > 1:
51 raise error.TelegramError(EXPECTED_ONE_MOVIE)
52
53 movie_id = context.args[0]
54
55 await update.message.reply_text(FETCHING_MOVIE.format(id=movie_id))
56
57 try:
58 movie = imdb.get_movie(movie_id)
59 except:
60 raise error.TelegramError(FETCHING_ERROR)
[9be02d9]61
[6623428]62 movie_dict = dict(
63 title=movie.data.get("title"),
64 id=movie.getID(),
65 user=update.effective_user.username,
[9be02d9]66 rating=movie.data.get("rating"),
67 genres=movie.data.get("genres"),
68 runtime=movie.data.get("runtimes"),
[6623428]69 )
70
71 if len(context.chat_data["movies"]) > 0 and \
72 context.chat_data["movies"][-1]["user"] == username:
73 context.chat_data["movies"][-1] = movie_dict
[1f94544]74
75 last_event = context.chat_data["events"][-1] \
76 if len(context.chat_data["events"]) > 0 else None
77
78 if last_event and context.chat_data["events"][-1]["user"] == username:
79 last_event["title"] = movie_dict["title"]
80 context.chat_data["events"][-1] = last_event
[6623428]81 else:
[69dd60c]82 init_new_event(context, users[0], movie_dict["title"])
[6623428]83 context.chat_data["movies"].append(movie_dict)
84
85 await update.message.reply_text(
86 MOVIE_SET.format(title=movie_dict["title"], user=movie_dict["user"])
87 )
88
89
90async def movies(
91 update: Update,
92 context: ContextTypes.DEFAULT_TYPE
93) -> None:
94 context_init(context)
95
[9be02d9]96 movies = context.chat_data["movies"]
97
98 movies_formatted: str = ""
[6623428]99
100 for movie in movies:
[9be02d9]101 movies_formatted += MOVIES_LIST.format(
102 title=movie["title"],
103 user=movie["user"]
104 )
[6623428]105
[9be02d9]106 if movies_formatted == "":
107 movies_formatted = NO_MOVIES
108
109 await update.message.reply_text(movies_formatted)
[6623428]110
Note: See TracBrowser for help on using the repository browser.