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 |
|
---|
12 | from telegram import Update, error
|
---|
13 | from telegram.ext import ContextTypes
|
---|
14 | from imdb import Cinemagoer
|
---|
15 |
|
---|
16 | from utils import context_init, choose_next_user
|
---|
17 | from predicates import has_finished_event
|
---|
18 | from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \
|
---|
19 | MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \
|
---|
20 | MOVIE_REMOVE, MOVIE_SET, MOVIES_LIST, NO_MOVIES, ADD_MORE_USERS
|
---|
21 |
|
---|
22 |
|
---|
23 | imdb = Cinemagoer()
|
---|
24 |
|
---|
25 |
|
---|
26 | async def movie(
|
---|
27 | update: Update,
|
---|
28 | context: ContextTypes.DEFAULT_TYPE
|
---|
29 | ) -> None:
|
---|
30 | context_init(context)
|
---|
31 |
|
---|
32 | users = context.chat_data["users"]
|
---|
33 |
|
---|
34 | if users == []:
|
---|
35 | raise error.TelegramError(ADD_MORE_USERS)
|
---|
36 |
|
---|
37 | chooser = users[0]
|
---|
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]))
|
---|
43 |
|
---|
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)
|
---|
61 |
|
---|
62 | movie_dict = dict(
|
---|
63 | title=movie.data.get("title"),
|
---|
64 | id=movie.getID(),
|
---|
65 | user=update.effective_user.username,
|
---|
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"),
|
---|
70 | )
|
---|
71 |
|
---|
72 | if len(context.chat_data["movies"]) > 0 and \
|
---|
73 | context.chat_data["movies"][-1]["user"] == username:
|
---|
74 | context.chat_data["movies"][-1] = movie_dict
|
---|
75 | else:
|
---|
76 | context.chat_data["movies"].append(movie_dict)
|
---|
77 |
|
---|
78 | await update.message.reply_text(
|
---|
79 | MOVIE_SET.format(title=movie_dict["title"], user=movie_dict["user"])
|
---|
80 | )
|
---|
81 |
|
---|
82 |
|
---|
83 | async def movies(
|
---|
84 | update: Update,
|
---|
85 | context: ContextTypes.DEFAULT_TYPE
|
---|
86 | ) -> None:
|
---|
87 | context_init(context)
|
---|
88 |
|
---|
89 | movies = context.chat_data["movies"]
|
---|
90 |
|
---|
91 | movies_formatted: str = ""
|
---|
92 |
|
---|
93 | for movie in movies:
|
---|
94 | movies_formatted += MOVIES_LIST.format(
|
---|
95 | title=movie["title"],
|
---|
96 | id=movie["id"],
|
---|
97 | user=movie["user"]
|
---|
98 | )
|
---|
99 |
|
---|
100 | if movies_formatted == "":
|
---|
101 | movies_formatted = NO_MOVIES
|
---|
102 |
|
---|
103 | await update.message.reply_text(movies_formatted)
|
---|
104 |
|
---|
105 |
|
---|
106 | async def remove_movies(
|
---|
107 | update: Update,
|
---|
108 | context: ContextTypes.DEFAULT_TYPE
|
---|
109 | ) -> None:
|
---|
110 | context_init(context)
|
---|
111 |
|
---|
112 | if context.args == []:
|
---|
113 | raise error.TelegramError(MOVIE_NOT_PROVIDED)
|
---|
114 |
|
---|
115 | movies = context.chat_data["movies"]
|
---|
116 |
|
---|
117 | for movie_id in context.args:
|
---|
118 | for movie in movies:
|
---|
119 | if movie["id"] == movie_id:
|
---|
120 | context.chat_data["movies"].remove(movie)
|
---|
121 | await update.message.reply_text(
|
---|
122 | MOVIE_REMOVE.format(title=movie["title"], id=movie["id"])
|
---|
123 | )
|
---|