From a811653ebc0672183ef7d233f266e7f62091e831 Mon Sep 17 00:00:00 2001 From: Lomion Date: Mon, 22 Jun 2020 13:15:28 +0200 Subject: [PATCH] Initial source commit --- .gitignore | 1 + README.md | 5 +++- maubot.yaml | 7 +++++ tmdb/__init__.py | 1 + tmdb/tmdb.py | 58 ++++++++++++++++++++++++++++++++++++++ tmdb/tmdb_api.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 maubot.yaml create mode 100644 tmdb/__init__.py create mode 100644 tmdb/tmdb.py create mode 100644 tmdb/tmdb_api.py diff --git a/.gitignore b/.gitignore index 13d1490..7b8fdf6 100644 --- a/.gitignore +++ b/.gitignore @@ -129,3 +129,4 @@ dmypy.json # Pyre type checker .pyre/ +lomion.tmdb.mbp diff --git a/README.md b/README.md index 6bbc8a5..82fe194 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # tmdb-bot +A [maubot](https://github.com/maubot/maubot) to get information about movies from [TheMovieDB.org](https://www.themoviedb.org/). -A [maubot](https://github.com/maubot/maubot) to get information about movies from [TheMovieDB.org](https://www.themoviedb.org/). \ No newline at end of file +## Usage +Use `!movie-id ` to get movie detail for . +Use `!movie-search ` to get movie detail based on the given <title>. \ No newline at end of file diff --git a/maubot.yaml b/maubot.yaml new file mode 100644 index 0000000..9dda137 --- /dev/null +++ b/maubot.yaml @@ -0,0 +1,7 @@ +maubot: 0.1.0 +id: lomion.tmdb +version: 0.0.1 +license: AGPL 3.0 +modules: +- tmdb +main_class: TmdbBot diff --git a/tmdb/__init__.py b/tmdb/__init__.py new file mode 100644 index 0000000..9047830 --- /dev/null +++ b/tmdb/__init__.py @@ -0,0 +1 @@ +from .tmdb import TmdbBot diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py new file mode 100644 index 0000000..229b4e7 --- /dev/null +++ b/tmdb/tmdb.py @@ -0,0 +1,58 @@ +from html import escape + +from mautrix.types import TextMessageEventContent, MediaMessageEventContent, MessageType, Format, RelatesTo, RelationType + +from maubot import Plugin, MessageEvent +from maubot.handlers import command + +from tmdb.tmdb_api import Movie + +class TmdbBot(Plugin): + + async def send_movie_info(self, evt: MessageEvent, movie) -> None: + mxc_uri = await self.client.upload_media(data=movie.get_image_binary()) + text_message = f'{movie.title}' + if len(movie.overview) > 200: + three_dotts = " [...]" + else: + three_dotts = "" + cast = "Acting: " + for actor in movie.cast[:3]: + cast+= f'{actor}, ' + cast = cast[:-2] + html_message = f"""<p><b>{escape(movie.title)}</b></p> + <p>{escape(movie.overview)[:200]}{three_dotts}</p> + <p>{cast}</p> + <p>taken from www.themoviedb.org</p>""" + content = TextMessageEventContent( + msgtype=MessageType.TEXT, format=Format.HTML, + body=f"{text_message}", + formatted_body=f"{html_message}") + await evt.respond(content) + content = MediaMessageEventContent( + msgtype=MessageType.IMAGE, + body=f"Image {movie.title}", + url=f"{mxc_uri}") + await evt.respond(content) + + + @command.new("movie-id", help="Movie lookup by id") + @command.argument("message", pass_raw=True, required=True) + async def movie_id(self, evt: MessageEvent, message: str = "") -> None: + movie = Movie() + movie.query_details(message) + await self.send_movie_info(evt, movie) + + + @command.new("movie-search", help="Movie lookup by Title") + @command.argument("message", pass_raw=True, required=True) + async def movie_search(self, evt: MessageEvent, message: str = "") -> None: + movie = Movie() + movie.search_title(message) + if movie.valid: + await self.send_movie_info(evt, movie) + else: + content = TextMessageEventContent( + msgtype=MessageType.NOTICE, format=Format.HTML, + body=f"No movie found!") + await evt.respond(content) diff --git a/tmdb/tmdb_api.py b/tmdb/tmdb_api.py new file mode 100644 index 0000000..f819671 --- /dev/null +++ b/tmdb/tmdb_api.py @@ -0,0 +1,72 @@ +import requests + +class Connection(): + def __init__(self): + self.load_parameters() + + def load_parameters(self): + self.api_key = '51d75c00dc1502dc894b7773ec3e7a15' + + self.base_url = "https://api.themoviedb.org/3/" + result = requests.get(self.base_url + 'configuration', params = self.get_apikey()).json() + self.base_url_poster = result['images']['base_url'] + result['images']['poster_sizes'][0] + self.valid = False + + def get_apikey(self): + return { 'api_key' : self.api_key } + + def request(self, request_uri): + url = self.base_url + request_uri + payload = self.get_apikey() + payload['language'] = 'de' + result = requests.get(url, params=payload) + self.valid = True + return result + + def get_image_binary(self): + return requests.get(self.poster_url).content + + +class Movie(Connection): + def __init__(self): + self.load_parameters() + pass + + def search_title(self, title): + url = self.base_url+ 'search/movie' + payload = self.get_apikey() + payload['language'] = 'de' + payload['query'] = title + result = requests.get(url, params=payload) + json = result.json() + if json['total_results'] > 0: + movie_id = json['results'][0]['id'] + self.query_details(movie_id) + return movie_id + + def query_details(self, id): + data = self.request('movie/' + str(id)).json() + self.title = data['title'] + self.id = data['id'] + self.poster_url = self.base_url_poster + data['poster_path'] + self.overview = data['overview'] + self.web_url = 'https://www.themoviedb.org/movie/' + str(self.id) + self.vote_average = str(data['vote_average']) + self.query_cast() + + def query_cast(self): + data = self.request('movie/'+str(self.id)+'/credits').json() + self.cast = [] + for actor in data['cast']: + self.cast.append(actor['name']) + + def get_cast(self, amount): + return self.cast[:amount] + +movie = Movie() +#movie.query_details('550') +movie.search_title('Jack Reacher') +print(movie.title) +print(movie.overview[:150]) +print(movie.web_url) +print(movie.poster_url)