2024-11-28 09:03:49 +00:00
|
|
|
"""
|
2020-06-22 15:22:28 +01:00
|
|
|
This file is part of tmdb-bot.
|
|
|
|
|
|
|
|
tmdb-bot is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License version 3 as published by
|
|
|
|
the Free Software Foundation.
|
|
|
|
|
|
|
|
tmdb-bot is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with tmdb-bot. If not, see <https://www.gnu.org/licenses/>.
|
2024-11-28 09:03:49 +00:00
|
|
|
"""
|
2020-06-22 12:15:28 +01:00
|
|
|
|
2024-11-28 09:03:49 +00:00
|
|
|
# pylint: disable=missing-class-docstring,missing-function-docstring
|
2020-06-22 12:15:28 +01:00
|
|
|
|
|
|
|
|
2024-11-28 09:03:49 +00:00
|
|
|
import json
|
|
|
|
import re
|
|
|
|
from html import escape
|
|
|
|
from typing import Callable
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2024-11-28 09:03:49 +00:00
|
|
|
from maubot.handlers import command, event
|
|
|
|
from maubot.plugin_base import Plugin
|
|
|
|
from mautrix.types import (
|
|
|
|
EventType,
|
|
|
|
Format,
|
|
|
|
ImageInfo,
|
|
|
|
MediaMessageEventContent,
|
|
|
|
MessageType,
|
|
|
|
TextMessageEventContent,
|
|
|
|
)
|
|
|
|
from mautrix.types.event.message import MessageEvent
|
|
|
|
|
|
|
|
from tmdb.database import Database, Engine
|
|
|
|
from tmdb.tmdb_api import Movie, MoviePopular, TmdbApi, TvShow
|
|
|
|
|
|
|
|
|
|
|
|
class MessageConstructor:
|
2021-03-16 15:35:02 +00:00
|
|
|
def __init__(self, movie: TmdbApi):
|
2021-01-16 08:23:51 +00:00
|
|
|
self.movie = movie
|
2024-11-28 09:03:49 +00:00
|
|
|
self.overview_length = 1024 * 32
|
2021-01-16 08:23:51 +00:00
|
|
|
self.cast_length = 3
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2021-01-16 08:23:51 +00:00
|
|
|
def three_dotts(self):
|
|
|
|
if len(self.movie.overview) > self.overview_length:
|
|
|
|
return " [...]"
|
2024-11-28 09:03:49 +00:00
|
|
|
return ""
|
2021-01-16 08:23:51 +00:00
|
|
|
|
|
|
|
def cast(self):
|
|
|
|
cast = "Acting: "
|
2024-11-28 09:03:49 +00:00
|
|
|
for actor in self.movie.cast[: self.cast_length]:
|
|
|
|
cast += f"{actor}, "
|
2021-01-16 08:23:51 +00:00
|
|
|
return cast[:-2]
|
|
|
|
|
|
|
|
def construct_html_message(self) -> str:
|
2024-11-28 09:03:49 +00:00
|
|
|
html_message = (
|
|
|
|
f'<p><a href="{self.movie.web_url}"><b>{escape(self.movie.title)}</b></a> - '
|
|
|
|
f"{str(int(self.movie.vote_average*10))}%</p>"
|
|
|
|
f"<p>{escape(self.movie.overview)[:self.overview_length]}{self.three_dotts()}</p>"
|
|
|
|
f"<p>{self.cast()}</p>"
|
|
|
|
f"<p>Taken from www.themoviedb.org</p>"
|
|
|
|
)
|
2021-01-16 08:23:51 +00:00
|
|
|
return html_message
|
|
|
|
|
2020-06-23 19:12:43 +01:00
|
|
|
|
2020-06-22 15:21:18 +01:00
|
|
|
class TmdbBot(Plugin):
|
2024-11-28 09:03:49 +00:00
|
|
|
database: Engine
|
2020-06-22 15:21:18 +01:00
|
|
|
db: Database
|
2024-11-28 09:03:49 +00:00
|
|
|
api = None
|
2020-06-22 13:41:31 +01:00
|
|
|
|
|
|
|
async def start(self) -> None:
|
|
|
|
await super().start()
|
|
|
|
self.db = Database(self.database)
|
2021-09-23 21:09:49 +01:00
|
|
|
self.api = None
|
2020-06-23 19:12:43 +01:00
|
|
|
|
2024-11-28 09:03:49 +00:00
|
|
|
async def send_html_message(
|
|
|
|
self, evt: MessageEvent, text_message: str, html_message: str, data=None
|
|
|
|
) -> None:
|
2020-06-22 12:15:28 +01:00
|
|
|
content = TextMessageEventContent(
|
2024-11-28 09:03:49 +00:00
|
|
|
msgtype=MessageType.TEXT,
|
|
|
|
format=Format.HTML,
|
2020-06-22 12:15:28 +01:00
|
|
|
body=f"{text_message}",
|
2024-11-28 09:03:49 +00:00
|
|
|
formatted_body=f"{html_message}",
|
|
|
|
)
|
2022-12-20 18:45:57 +00:00
|
|
|
event_id = await evt.respond(content)
|
|
|
|
if data:
|
|
|
|
self.db.set_message(event_id, json.dumps(data))
|
2020-06-23 14:47:40 +01:00
|
|
|
|
2020-07-03 14:06:33 +01:00
|
|
|
async def send_notice(self, evt: MessageEvent, message: str = "") -> None:
|
|
|
|
content = TextMessageEventContent(
|
2024-11-28 09:03:49 +00:00
|
|
|
msgtype=MessageType.NOTICE, format=Format.HTML, body=message
|
|
|
|
)
|
2022-12-22 21:38:56 +00:00
|
|
|
await evt.respond(content)
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-26 16:43:13 +01:00
|
|
|
async def send_help(self, evt: MessageEvent) -> None:
|
2024-11-28 09:03:49 +00:00
|
|
|
html = (
|
|
|
|
"<p>Use <b>!tmdb movie {title} [y:{release year}]</b> to get movie detail based "
|
|
|
|
"on the given title.</p>"
|
|
|
|
"<p>Use <b>!tmdb tvshow {title}</b> to get detail about a tv show based on the "
|
|
|
|
"given title.</p>"
|
|
|
|
"<p>Use <b>!tmdb popular [{rating}]</b> to get most popular movies. Get details "
|
|
|
|
"about any one movie in the list by adding the {rating}.</p>"
|
|
|
|
"<p>Use <b>!tmdb language {language}</b> to set your prefered language.</p>"
|
|
|
|
"<p>Use <b>!tmdb poster_size [{size}]</b> to set your prefered poster size. With "
|
|
|
|
"empty {size} all available sizes are listed.</p>"
|
|
|
|
)
|
2020-06-26 16:43:13 +01:00
|
|
|
content = TextMessageEventContent(
|
2024-11-28 09:03:49 +00:00
|
|
|
msgtype=MessageType.TEXT,
|
|
|
|
format=Format.HTML,
|
2021-03-16 15:40:00 +00:00
|
|
|
body="Help for TMDB Bot",
|
2024-11-28 09:03:49 +00:00
|
|
|
formatted_body=f"{html}",
|
|
|
|
)
|
2020-06-26 16:43:13 +01:00
|
|
|
await evt.respond(content)
|
|
|
|
|
2020-06-24 10:17:19 +01:00
|
|
|
async def send_image(self, evt: MessageEvent, title, image) -> None:
|
2021-02-16 14:00:01 +00:00
|
|
|
if image:
|
2021-03-06 20:28:55 +00:00
|
|
|
mxc_uri = await self.client.upload_media(image)
|
2021-02-16 14:00:01 +00:00
|
|
|
content = MediaMessageEventContent(
|
|
|
|
msgtype=MessageType.IMAGE,
|
|
|
|
body=f"Image {title}",
|
|
|
|
url=f"{mxc_uri}",
|
2024-11-28 09:03:49 +00:00
|
|
|
info=ImageInfo(mimetype="image/jpg"),
|
|
|
|
)
|
2021-02-16 14:00:01 +00:00
|
|
|
await evt.respond(content)
|
2020-06-22 12:15:28 +01:00
|
|
|
|
2024-11-28 09:03:49 +00:00
|
|
|
def split_title_year(self, message: str) -> tuple[str, int]:
|
|
|
|
m = re.search(r"^(.*) (y:\d\d\d\d)", message)
|
2020-06-26 15:41:44 +01:00
|
|
|
if m:
|
|
|
|
title = m.group(1)
|
|
|
|
year = int(m.group(2)[2:])
|
|
|
|
return (title, year)
|
2024-11-28 09:03:49 +00:00
|
|
|
return (message, 0)
|
2020-06-26 15:41:44 +01:00
|
|
|
|
2021-03-16 15:35:02 +00:00
|
|
|
def set_language(self, evt: MessageEvent, movie: TmdbApi):
|
2020-09-11 10:15:04 +01:00
|
|
|
language = self.db.get_language(evt.sender)
|
|
|
|
if language:
|
|
|
|
movie.set_language(language)
|
|
|
|
|
2021-03-16 15:35:02 +00:00
|
|
|
def poster_size(self, evt: MessageEvent, movie: TmdbApi):
|
2020-09-11 10:15:04 +01:00
|
|
|
size = self.db.get_poster_size(evt.sender)
|
|
|
|
if size:
|
|
|
|
movie.set_poster_size(size)
|
|
|
|
|
2020-06-23 14:47:40 +01:00
|
|
|
async def send_movie_info(self, evt: MessageEvent, movie) -> None:
|
2021-01-16 08:23:51 +00:00
|
|
|
constructor = MessageConstructor(movie)
|
|
|
|
html_message = constructor.construct_html_message()
|
2024-11-28 09:03:49 +00:00
|
|
|
await self.send_html_message(evt, f"{movie.title}", html_message)
|
2021-09-23 12:39:40 +01:00
|
|
|
if movie.get_image_binary():
|
|
|
|
await self.send_image(evt, movie.title, movie.get_image_binary())
|
2020-06-23 14:47:40 +01:00
|
|
|
|
2021-09-23 12:39:40 +01:00
|
|
|
async def init_movie(self):
|
2021-09-23 21:09:49 +01:00
|
|
|
self.api = Movie()
|
|
|
|
await self.api.load_parameters()
|
|
|
|
return self.api
|
2021-09-23 12:39:40 +01:00
|
|
|
|
|
|
|
async def init_tvshow(self):
|
|
|
|
show = TvShow()
|
|
|
|
await show.load_parameters()
|
|
|
|
return show
|
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def init_moviepopular(self):
|
|
|
|
movie = MoviePopular()
|
|
|
|
await movie.load_parameters()
|
|
|
|
return movie
|
|
|
|
|
2021-09-23 12:39:40 +01:00
|
|
|
async def movie_id(self, evt: MessageEvent, message: str = "") -> None:
|
|
|
|
movie = await self.init_movie()
|
2020-09-11 10:15:04 +01:00
|
|
|
self.poster_size(evt, movie)
|
2020-06-22 15:21:18 +01:00
|
|
|
language = self.db.get_language(evt.sender)
|
|
|
|
if language:
|
|
|
|
movie.set_language(language)
|
2021-09-23 12:39:40 +01:00
|
|
|
await movie.query_details(message)
|
2020-06-22 12:15:28 +01:00
|
|
|
await self.send_movie_info(evt, movie)
|
|
|
|
|
2020-07-03 14:06:33 +01:00
|
|
|
@command.new("movie-id", help="Movie lookup by id")
|
2020-06-22 12:15:28 +01:00
|
|
|
@command.argument("message", pass_raw=True, required=True)
|
2020-07-03 14:06:33 +01:00
|
|
|
async def command_movie_id(self, evt: MessageEvent, message: str = "") -> None:
|
|
|
|
await self.movie_id(evt, message)
|
|
|
|
|
2021-03-16 20:45:27 +00:00
|
|
|
async def movie_popular(self, evt: MessageEvent, message: str = "") -> None:
|
2021-09-23 21:09:49 +01:00
|
|
|
popular = await self.init_moviepopular()
|
2020-06-22 13:41:31 +01:00
|
|
|
language = self.db.get_language(evt.sender)
|
2021-11-18 20:48:13 +00:00
|
|
|
self.poster_size(evt, popular)
|
2020-06-22 13:41:31 +01:00
|
|
|
if language:
|
2021-03-16 20:45:27 +00:00
|
|
|
popular.set_language(language)
|
|
|
|
length = 5
|
2021-11-08 21:40:34 +00:00
|
|
|
|
2021-11-08 21:28:34 +00:00
|
|
|
await popular.query()
|
2021-11-18 20:48:13 +00:00
|
|
|
|
2024-11-28 09:03:49 +00:00
|
|
|
m = re.search(r"([1-5])", message)
|
2021-11-18 20:48:13 +00:00
|
|
|
if m:
|
|
|
|
number = m.group(1)
|
|
|
|
movie = await popular.getMovieByNumber(number)
|
|
|
|
if language:
|
|
|
|
movie.set_language(language)
|
|
|
|
await self.send_movie_info(evt, movie)
|
|
|
|
else:
|
|
|
|
text = popular.getListText(length)
|
2024-11-28 09:03:49 +00:00
|
|
|
html = (
|
|
|
|
"<p><b>Currently most popular at "
|
|
|
|
'<a href="https://www.themoviedb.org">www.themoviedb.org</a>:</b></p>'
|
|
|
|
)
|
2022-12-22 21:38:56 +00:00
|
|
|
html += popular.getListHtml(length)
|
2024-11-28 09:03:49 +00:00
|
|
|
html += (
|
|
|
|
"<p>For details reply to this message with the ranking number "
|
|
|
|
"from this list</p>"
|
|
|
|
)
|
2022-12-20 18:45:57 +00:00
|
|
|
results = popular.getDict(length)
|
|
|
|
await self.send_html_message(evt, text, html, results)
|
2020-06-22 13:41:31 +01:00
|
|
|
|
2020-07-03 14:06:33 +01:00
|
|
|
@command.new("movie-search", help="Movie lookup by Title")
|
2020-06-22 13:41:31 +01:00
|
|
|
@command.argument("message", pass_raw=True, required=True)
|
2020-07-03 14:06:33 +01:00
|
|
|
async def command_movie_search(self, evt: MessageEvent, message: str = "") -> None:
|
|
|
|
await self.movie_search(evt, message)
|
|
|
|
|
2020-06-22 13:41:31 +01:00
|
|
|
async def movie_language(self, evt: MessageEvent, message: str = "") -> None:
|
|
|
|
self.db.set_language(evt.sender, message)
|
|
|
|
content = TextMessageEventContent(
|
2024-11-28 09:03:49 +00:00
|
|
|
msgtype=MessageType.NOTICE,
|
|
|
|
format=Format.HTML,
|
|
|
|
body=f"Language set to {message}!",
|
|
|
|
)
|
2020-09-09 14:18:22 +01:00
|
|
|
await evt.respond(content)
|
|
|
|
|
2024-11-28 09:03:49 +00:00
|
|
|
async def set_poster_size(self, evt: MessageEvent, message: str = "") -> None:
|
2021-09-23 12:39:40 +01:00
|
|
|
movie = await self.init_movie()
|
2020-09-11 10:15:04 +01:00
|
|
|
poster_sizes = ""
|
|
|
|
for x in movie.poster_sizes:
|
|
|
|
poster_sizes += x + " "
|
2020-09-09 14:18:22 +01:00
|
|
|
if message:
|
|
|
|
size = movie.set_poster_size(message)
|
2020-09-11 10:15:04 +01:00
|
|
|
if size:
|
|
|
|
self.db.set_poster_size(evt.sender, size)
|
|
|
|
await self.send_notice(evt, f"Set default poster size to {size}")
|
|
|
|
else:
|
2024-11-28 09:03:49 +00:00
|
|
|
await self.send_notice(
|
|
|
|
evt,
|
|
|
|
f"Failed setting poster size. Valid sizes are {poster_sizes}.",
|
|
|
|
)
|
2020-09-09 14:18:22 +01:00
|
|
|
else:
|
2020-09-11 10:15:04 +01:00
|
|
|
await self.send_notice(evt, f"Valid sizes are {poster_sizes}.")
|
2020-06-24 10:17:19 +01:00
|
|
|
|
2020-07-03 14:06:33 +01:00
|
|
|
@command.new("movie-language", help="Set language for lookup")
|
|
|
|
@command.argument("message", pass_raw=True, required=True)
|
2024-11-28 09:03:49 +00:00
|
|
|
async def command_movie_language(
|
|
|
|
self,
|
|
|
|
evt: MessageEvent,
|
|
|
|
message: str = "",
|
|
|
|
) -> None:
|
2020-07-03 14:06:33 +01:00
|
|
|
await self.movie_language(evt, message)
|
|
|
|
|
2020-06-26 16:43:13 +01:00
|
|
|
@command.new("movie-help", help="Help for TMDB Bot")
|
2024-11-28 09:03:49 +00:00
|
|
|
async def movie_help(self, evt: MessageEvent) -> None:
|
2020-06-26 16:43:13 +01:00
|
|
|
await self.send_help(evt)
|
|
|
|
|
|
|
|
@command.new("tvshow-help", help="Help for TMDB Bot")
|
2024-11-28 09:03:49 +00:00
|
|
|
async def tvshow_help(self, evt: MessageEvent) -> None:
|
2020-06-26 16:43:13 +01:00
|
|
|
await self.send_help(evt)
|
|
|
|
|
2020-06-24 10:17:19 +01:00
|
|
|
async def tvshow_search(self, evt: MessageEvent, message: str = "") -> None:
|
2021-09-23 12:39:40 +01:00
|
|
|
movie = await self.init_tvshow()
|
2020-09-11 10:15:04 +01:00
|
|
|
self.poster_size(evt, movie)
|
2020-06-24 10:17:19 +01:00
|
|
|
language = self.db.get_language(evt.sender)
|
|
|
|
if language:
|
|
|
|
movie.set_language(language)
|
2021-12-12 10:26:28 +00:00
|
|
|
title, year = self.split_title_year(message)
|
|
|
|
await movie.search_title(title, year)
|
2020-06-24 10:17:19 +01:00
|
|
|
if movie.valid:
|
|
|
|
await self.send_movie_info(evt, movie)
|
|
|
|
else:
|
2020-09-11 10:15:04 +01:00
|
|
|
await self.send_notice(evt, "No tv show found!")
|
2020-07-03 14:06:33 +01:00
|
|
|
|
|
|
|
@command.new("tvshow-search", help="TV Show lookup by Title")
|
|
|
|
@command.argument("message", pass_raw=True, required=True)
|
|
|
|
async def command_tvshow_search(self, evt: MessageEvent, message: str = "") -> None:
|
|
|
|
await self.tvshow_search(evt, message)
|
|
|
|
|
2021-03-16 20:45:27 +00:00
|
|
|
async def movie_search(self, evt: MessageEvent, message: str = "") -> None:
|
2021-09-23 21:09:49 +01:00
|
|
|
await self.init_movie()
|
2024-11-28 09:03:49 +00:00
|
|
|
if not self.api:
|
|
|
|
return
|
2021-09-23 21:09:49 +01:00
|
|
|
self.poster_size(evt, self.api)
|
2021-03-16 20:45:27 +00:00
|
|
|
language = self.db.get_language(evt.sender)
|
|
|
|
if language:
|
2021-09-23 21:09:49 +01:00
|
|
|
self.api.set_language(language)
|
2021-03-16 20:45:27 +00:00
|
|
|
title, year = self.split_title_year(message)
|
2021-09-23 21:09:49 +01:00
|
|
|
await self.api.search_title(title, year)
|
|
|
|
if self.api.valid:
|
|
|
|
await self.send_movie_info(evt, self.api)
|
2021-03-16 20:45:27 +00:00
|
|
|
else:
|
|
|
|
content = TextMessageEventContent(
|
2024-11-28 09:03:49 +00:00
|
|
|
msgtype=MessageType.NOTICE, format=Format.HTML, body="No movie found!"
|
|
|
|
)
|
2021-03-16 20:45:27 +00:00
|
|
|
await evt.respond(content)
|
|
|
|
|
2020-07-03 14:06:33 +01:00
|
|
|
@command.new("tmdb", help="TMDB Bot")
|
|
|
|
@command.argument("message", pass_raw=True, required=True)
|
|
|
|
async def command_dispatcher(self, evt: MessageEvent, message: str = "") -> None:
|
2024-11-28 09:03:49 +00:00
|
|
|
m = re.search(r"^([^\s]*)\s*(.*)", message)
|
2020-07-03 14:06:33 +01:00
|
|
|
if m:
|
2024-11-28 09:03:49 +00:00
|
|
|
cmd = m.group(1)
|
2020-07-03 14:06:33 +01:00
|
|
|
parameters = m.group(2)
|
2024-11-28 09:03:49 +00:00
|
|
|
match cmd.lower():
|
|
|
|
case "help":
|
|
|
|
await self.send_help(evt)
|
|
|
|
case "movie":
|
|
|
|
await self.movie_search(evt, parameters)
|
|
|
|
case "popular":
|
|
|
|
await self.movie_popular(evt, parameters)
|
|
|
|
case "language":
|
|
|
|
await self.movie_language(evt, parameters)
|
|
|
|
case "poster_size":
|
|
|
|
await self.set_poster_size(evt, parameters)
|
|
|
|
case "tvshow":
|
|
|
|
await self.tvshow_search(evt, parameters)
|
|
|
|
case _:
|
|
|
|
await self.send_help(evt)
|
2020-07-03 14:06:33 +01:00
|
|
|
else:
|
2021-01-16 08:23:51 +00:00
|
|
|
await self.send_help(evt)
|
2021-09-23 21:09:49 +01:00
|
|
|
if self.api:
|
2022-11-14 15:15:23 +00:00
|
|
|
await self.api.close_session()
|
2022-12-20 18:45:57 +00:00
|
|
|
|
2024-11-28 09:03:49 +00:00
|
|
|
@event.on(EventType.ROOM_MESSAGE) # pylint: disable=no-member
|
2022-12-20 18:45:57 +00:00
|
|
|
async def handle_reply(self, evt: MessageEvent) -> None:
|
2024-11-28 09:03:49 +00:00
|
|
|
reply_to = evt.content.get_reply_to
|
|
|
|
if isinstance(reply_to, Callable):
|
|
|
|
self.log.info(f"{evt.event_id} received. Reply to {reply_to}")
|
|
|
|
result_json = self.db.get_message(reply_to())
|
2022-12-20 18:45:57 +00:00
|
|
|
if result_json:
|
2024-11-28 09:03:49 +00:00
|
|
|
requ = int(str(evt.content.body))
|
2022-12-20 18:45:57 +00:00
|
|
|
if requ > 0:
|
|
|
|
populars = json.loads(result_json)
|
|
|
|
if str(requ) in populars:
|
|
|
|
await self.init_movie()
|
2024-11-28 09:03:49 +00:00
|
|
|
if self.api:
|
|
|
|
await self.api.search_id(populars[str(requ)])
|
2022-12-20 18:45:57 +00:00
|
|
|
await self.send_movie_info(evt, self.api)
|
|
|
|
else:
|
2022-12-22 21:59:33 +00:00
|
|
|
self.log.info("Not in reply to a known message")
|
2022-12-20 18:45:57 +00:00
|
|
|
else:
|
2022-12-22 21:59:33 +00:00
|
|
|
self.log.info("No reply")
|