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, normalize_username, \
|
---|
17 | init_new_event
|
---|
18 | from predicates import has_finished_movie
|
---|
19 | from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \
|
---|
20 | MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \
|
---|
21 | MOVIE_REMOVE, MOVIE_SET, MOVIES_LIST, NO_MOVIES, ADD_MORE_USERS
|
---|
22 |
|
---|
23 |
|
---|
24 | imdb = Cinemagoer()
|
---|
25 |
|
---|
26 |
|
---|
27 | async def movie(
|
---|
28 | update: Update,
|
---|
29 | context: ContextTypes.DEFAULT_TYPE
|
---|
30 | ) -> None:
|
---|
31 | context_init(context)
|
---|
32 |
|
---|
33 | users = context.chat_data["users"]
|
---|
34 |
|
---|
35 | if users == []:
|
---|
36 | raise error.TelegramError(ADD_MORE_USERS)
|
---|
37 |
|
---|
38 | chooser = users[0]
|
---|
39 | username = update.message.from_user.username
|
---|
40 |
|
---|
41 | if has_finished_movie(context):
|
---|
42 | users = context.chat_data["users"] = choose_next_user(context)
|
---|
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 | rating=movie.data.get("rating"),
|
---|
67 | genres=movie.data.get("genres"),
|
---|
68 | runtime=movie.data.get("runtimes"),
|
---|
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
|
---|
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
|
---|
81 | else:
|
---|
82 | init_new_event(context, users[0], movie_dict["title"])
|
---|
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 |
|
---|
90 | async def movies(
|
---|
91 | update: Update,
|
---|
92 | context: ContextTypes.DEFAULT_TYPE
|
---|
93 | ) -> None:
|
---|
94 | context_init(context)
|
---|
95 |
|
---|
96 | movies = context.chat_data["movies"]
|
---|
97 |
|
---|
98 | movies_formatted: str = ""
|
---|
99 |
|
---|
100 | for movie in movies:
|
---|
101 | movies_formatted += MOVIES_LIST.format(
|
---|
102 | title=movie["title"],
|
---|
103 | user=movie["user"]
|
---|
104 | )
|
---|
105 |
|
---|
106 | if movies_formatted == "":
|
---|
107 | movies_formatted = NO_MOVIES
|
---|
108 |
|
---|
109 | await update.message.reply_text(movies_formatted)
|
---|
110 |
|
---|