From aba4a0e5daf845aca94efef377ca1f203bbf2f14 Mon Sep 17 00:00:00 2001 From: lomion Date: Sat, 16 Jan 2021 09:23:51 +0100 Subject: [PATCH] Moved construct_html_message into own class. Next step: additional method to create body. --- test_tmdb.py | 10 ++++++---- tmdb/tmdb.py | 47 +++++++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 22 deletions(-) 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, """

Fight Club

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.title)}

+

{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.title)}

-

{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)