diff --git a/test_tmdb.py b/test_tmdb.py index e55d716..ccfc2da 100644 --- a/test_tmdb.py +++ b/test_tmdb.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import unittest from tmdb.tmdb_api import Movie, TvShow -from tmdb.tmdb import TmdbBot +from tmdb.tmdb import TmdbBot, MessageConstructor from tmdb.database import Database from sqlalchemy import create_engine @@ -40,7 +40,9 @@ class TestTmdbMethods(unittest.TestCase): movie = Movie() tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ) movie.query_details('550') - message = tmdb.construct_html_message(movie, overview_length = 10) + constructor = MessageConstructor(movie) + constructor.overview_length = 10 + message = constructor.construct_html_message() self.assertEqual(message, """
A ticking- [...]
Acting: Edward Norton, Brad Pitt, Helena Bonham Carter
@@ -68,9 +70,9 @@ class TestTmdbMethods(unittest.TestCase): def test_search_year(self): movie = Movie() id = movie.search_title('Dune') - self.assertEqual(id, 841) - id = movie.search_title('Dune', 2020) self.assertEqual(id, 438631) + id = movie.search_title('Dune', 1984) + self.assertEqual(id, 841) def test_split_year(self): tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ) diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index c46a2db..799d9ba 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.py @@ -24,6 +24,32 @@ from maubot.handlers import command from tmdb.tmdb_api import TmdbApi, Movie, TvShow from tmdb.database import Database +class MessageConstructor(): + def __init__(self, movie : TmdbApi): + self.movie = movie + self.overview_length = 200 + self.cast_length = 3 + + def three_dotts(self): + if len(self.movie.overview) > self.overview_length: + return " [...]" + else: + return "" + + def cast(self): + cast = "Acting: " + for actor in self.movie.cast[:self.cast_length]: + cast+= f'{actor}, ' + return cast[:-2] + + + def construct_html_message(self) -> str: + html_message = f""" +{escape(self.movie.overview)[:self.overview_length]}{self.three_dotts()}
+{self.cast()}
+Taken from www.themoviedb.org
""" + return html_message + class TmdbBot(Plugin): db: Database @@ -81,22 +107,6 @@ class TmdbBot(Plugin): 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.overview)[:overview_length]}{three_dotts}
-{cast}
-Taken from www.themoviedb.org
""" - return html_message - def split_title_year(self, message : str) -> (str, int): m = re.search(r'^(.*) (y:\d\d\d\d)', message) if m: @@ -116,7 +126,8 @@ class TmdbBot(Plugin): movie.set_poster_size(size) async def send_movie_info(self, evt: MessageEvent, movie) -> None: - html_message = self.construct_html_message(movie) + constructor = MessageConstructor(movie) + html_message = constructor.construct_html_message() await self.send_html_message(evt, f'{movie.title}', html_message) await self.send_image(evt, movie.title, movie.get_image_binary()) @@ -240,4 +251,4 @@ class TmdbBot(Plugin): else: await self.send_help(evt) else: - await self.send_help(evt) \ No newline at end of file + await self.send_help(evt)