diff --git a/tmdb/test_tmdb.py b/test_tmdb.py similarity index 67% rename from tmdb/test_tmdb.py rename to test_tmdb.py index 4d0cdc2..9740d04 100644 --- a/tmdb/test_tmdb.py +++ b/test_tmdb.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import unittest -from tmdb_api import Movie +from tmdb.tmdb_api import Movie +from tmdb.tmdb import TmdbBot class TestTmdbMethods(unittest.TestCase): ### TMDB API @@ -32,5 +33,15 @@ class TestTmdbMethods(unittest.TestCase): description = 'Five high school students from different walks of life endure a Saturday detention' self.assertEqual(description, movie.overview[:len(description)]) + def test_html_construction(self): + movie = Movie() + tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ) + movie.query_details('550') + message = tmdb.construct_html_message(movie, overview_length = 10) + self.assertEqual(message, """
Fight Club
+Ein Yuppie [...]
+Acting: Edward Norton, Brad Pitt, Helena Bonham Carter
+taken from www.themoviedb.org
""") + if __name__ == '__main__': unittest.main() diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index 59613ba..e798644 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.py @@ -57,33 +57,43 @@ class TmdbBot(Plugin): async def start(self) -> None: await super().start() self.db = Database(self.database) - - 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"""{escape(movie.title)}
-{escape(movie.overview)[:200]}{three_dotts}
-{cast}
-taken from www.themoviedb.org
""" + + async def send_html_message(self, text_message, html_message) -> None: content = TextMessageEventContent( msgtype=MessageType.TEXT, format=Format.HTML, body=f"{text_message}", formatted_body=f"{html_message}") await evt.respond(content) + + async def send_image(self, title, image) -> None: + mxc_uri = await self.client.upload_media(image) content = MediaMessageEventContent( msgtype=MessageType.IMAGE, - body=f"Image {movie.title}", + body=f"Image {title}", url=f"{mxc_uri}") await evt.respond(content) + def construct_html_message(self, movie, overview_length = 200, cast_length = 3) -> str: + if len(movie.overview) > overview_length: + three_dotts = " [...]" + else: + three_dotts = "" + + cast = "Acting: " + for actor in movie.cast[:cast_length]: + cast+= f'{actor}, ' + cast = cast[:-2] + html_message = f"""{escape(movie.title)}
+{escape(movie.overview)[:overview_length]}{three_dotts}
+{cast}
+taken from www.themoviedb.org
""" + return html_message + + async def send_movie_info(self, evt: MessageEvent, movie) -> None: + html_message = self.construct_html_message(movie) + await self.send_html_message(f'{movie.title}', html_message) + await self.send_image(movie.title, movie.get_image_binary()) + @command.new("movie-id", help="Movie lookup by id") @command.argument("message", pass_raw=True, required=True)