[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 |
|
---|
| 12 | from telegram import Update, error
|
---|
| 13 | from telegram.ext import ContextTypes
|
---|
| 14 | from imdb import Cinemagoer
|
---|
| 15 |
|
---|
[9be02d9] | 16 | from utils import context_init, choose_next_user
|
---|
| 17 | from predicates import has_finished_event
|
---|
[6623428] | 18 | from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \
|
---|
| 19 | MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \
|
---|
[9be02d9] | 20 | MOVIE_REMOVE, MOVIE_SET, MOVIES_LIST, NO_MOVIES, ADD_MORE_USERS
|
---|
[6623428] | 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 |
|
---|
[9be02d9] | 32 | users = context.chat_data["users"]
|
---|
| 33 |
|
---|
| 34 | if users == []:
|
---|
| 35 | raise error.TelegramError(ADD_MORE_USERS)
|
---|
| 36 |
|
---|
| 37 | chooser = users[0]
|
---|
[6623428] | 38 | username = update.message.from_user.username
|
---|
| 39 |
|
---|
[9be02d9] | 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 |
|
---|
[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 | poster=movie.data.get("cover url"),
|
---|
| 67 | rating=movie.data.get("rating"),
|
---|
| 68 | genres=movie.data.get("genres"),
|
---|
| 69 | runtime=movie.data.get("runtimes"),
|
---|
[6623428] | 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
|
---|
[1f94544] | 75 |
|
---|
| 76 | last_event = context.chat_data["events"][-1] \
|
---|
| 77 | if len(context.chat_data["events"]) > 0 else None
|
---|
| 78 |
|
---|
| 79 | if last_event and context.chat_data["events"][-1]["user"] == username:
|
---|
| 80 | last_event["title"] = movie_dict["title"]
|
---|
| 81 | context.chat_data["events"][-1] = last_event
|
---|
[6623428] | 82 | else:
|
---|
| 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 |
|
---|
[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 | id=movie["id"],
|
---|
| 104 | user=movie["user"]
|
---|
| 105 | )
|
---|
[6623428] | 106 |
|
---|
[9be02d9] | 107 | if movies_formatted == "":
|
---|
| 108 | movies_formatted = NO_MOVIES
|
---|
| 109 |
|
---|
| 110 | await update.message.reply_text(movies_formatted)
|
---|
[6623428] | 111 |
|
---|