1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# 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 imdb import Cinemagoer
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, MOVIES_LIST, NO_MOVIES, ADD_MORE_USERS
imdb = Cinemagoer()
async def movie(
update: Update,
context: ContextTypes.DEFAULT_TYPE
) -> None:
context_init(context)
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))
if context.args == []:
raise error.TelegramError(MOVIE_NOT_PROVIDED)
if len(context.args) > 1:
raise error.TelegramError(EXPECTED_ONE_MOVIE)
movie_id = context.args[0]
await update.message.reply_text(FETCHING_MOVIE.format(id=movie_id))
try:
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"),
rating=movie.data.get("rating"),
genres=movie.data.get("genres"),
runtime=movie.data.get("runtimes"),
)
if len(context.chat_data["movies"]) > 0 and \
context.chat_data["movies"][-1]["user"] == username:
context.chat_data["movies"][-1] = movie_dict
else:
context.chat_data["movies"].append(movie_dict)
await update.message.reply_text(
MOVIE_SET.format(title=movie_dict["title"], user=movie_dict["user"])
)
async def movies(
update: Update,
context: ContextTypes.DEFAULT_TYPE
) -> None:
context_init(context)
movies = context.chat_data["movies"]
movies_formatted: str = ""
for movie in movies:
movies_formatted += MOVIES_LIST.format(
title=movie["title"],
id=movie["id"],
user=movie["user"]
)
if movies_formatted == "":
movies_formatted = NO_MOVIES
await update.message.reply_text(movies_formatted)
async def remove_movies(
update: Update,
context: ContextTypes.DEFAULT_TYPE
) -> None:
context_init(context)
if context.args == []:
raise error.TelegramError(MOVIE_NOT_PROVIDED)
movies = context.chat_data["movies"]
for movie_id in context.args:
for movie in 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"])
)
|