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 | from pprint import pformat
|
---|
16 |
|
---|
17 | from utils import context_init
|
---|
18 |
|
---|
19 | from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \
|
---|
20 | MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \
|
---|
21 | MOVIE_REMOVE, MOVIE_SET
|
---|
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 | chooser = context.chat_data["users"][0] or None
|
---|
34 | username = update.message.from_user.username
|
---|
35 |
|
---|
36 | if "@"+username != chooser:
|
---|
37 | raise error.TelegramError(MOVIE_ANOTHER_USER.format(user=chooser))
|
---|
38 |
|
---|
39 | if context.args == []:
|
---|
40 | raise error.TelegramError(MOVIE_NOT_PROVIDED)
|
---|
41 |
|
---|
42 | if len(context.args) > 1:
|
---|
43 | raise error.TelegramError(EXPECTED_ONE_MOVIE)
|
---|
44 |
|
---|
45 | movie_id = context.args[0]
|
---|
46 |
|
---|
47 | await update.message.reply_text(FETCHING_MOVIE.format(id=movie_id))
|
---|
48 |
|
---|
49 | try:
|
---|
50 | movie = imdb.get_movie(movie_id)
|
---|
51 | except:
|
---|
52 | raise error.TelegramError(FETCHING_ERROR)
|
---|
53 |
|
---|
54 | movie_dict = dict(
|
---|
55 | title=movie.data.get("title"),
|
---|
56 | id=movie.getID(),
|
---|
57 | user=update.effective_user.username,
|
---|
58 | poster=movie.data.get("cover url")
|
---|
59 | )
|
---|
60 |
|
---|
61 | if len(context.chat_data["movies"]) > 0 and \
|
---|
62 | context.chat_data["movies"][-1]["user"] == username:
|
---|
63 | context.chat_data["movies"][-1] = movie_dict
|
---|
64 | else:
|
---|
65 | context.chat_data["movies"].append(movie_dict)
|
---|
66 |
|
---|
67 | await update.message.reply_text(
|
---|
68 | MOVIE_SET.format(title=movie_dict["title"], user=movie_dict["user"])
|
---|
69 | )
|
---|
70 |
|
---|
71 |
|
---|
72 | async def movies(
|
---|
73 | update: Update,
|
---|
74 | context: ContextTypes.DEFAULT_TYPE
|
---|
75 | ) -> None:
|
---|
76 | context_init(context)
|
---|
77 |
|
---|
78 | movies = context.chat_data["movies"].copy()
|
---|
79 |
|
---|
80 | for movie in movies:
|
---|
81 | if movie["poster"] or None:
|
---|
82 | del movie["poster"]
|
---|
83 |
|
---|
84 | await update.message.reply_text(pformat(movies))
|
---|
85 |
|
---|
86 |
|
---|
87 | async def remove_movies(
|
---|
88 | update: Update,
|
---|
89 | context: ContextTypes.DEFAULT_TYPE
|
---|
90 | ) -> None:
|
---|
91 | context_init(context)
|
---|
92 |
|
---|
93 | if context.args == []:
|
---|
94 | raise error.TelegramError(MOVIE_NOT_PROVIDED)
|
---|
95 |
|
---|
96 | movies = context.chat_data["movies"]
|
---|
97 |
|
---|
98 | for movie_id in context.args:
|
---|
99 | for movie in movies:
|
---|
100 | if movie["id"] == movie_id:
|
---|
101 | context.chat_data["movies"].remove(movie)
|
---|
102 | await update.message.reply_text(
|
---|
103 | MOVIE_REMOVE.format(title=movie["title"], id=["movie.id"])
|
---|
104 | )
|
---|
105 |
|
---|