Created unittest for HTML creation and refactored code to use it

This commit is contained in:
lomion 2020-06-23 15:47:40 +02:00
parent 985bb0ce91
commit 053f7a3785
2 changed files with 39 additions and 18 deletions

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import unittest import unittest
from tmdb_api import Movie from tmdb.tmdb_api import Movie
from tmdb.tmdb import TmdbBot
class TestTmdbMethods(unittest.TestCase): class TestTmdbMethods(unittest.TestCase):
### TMDB API ### TMDB API
@ -32,5 +33,15 @@ class TestTmdbMethods(unittest.TestCase):
description = 'Five high school students from different walks of life endure a Saturday detention' description = 'Five high school students from different walks of life endure a Saturday detention'
self.assertEqual(description, movie.overview[:len(description)]) 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, """<p><b>Fight Club</b></p>
<p>Ein Yuppie [...]</p>
<p>Acting: Edward Norton, Brad Pitt, Helena Bonham Carter</p>
<p>taken from www.themoviedb.org</p>""")
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -57,33 +57,43 @@ class TmdbBot(Plugin):
async def start(self) -> None: async def start(self) -> None:
await super().start() await super().start()
self.db = Database(self.database) self.db = Database(self.database)
async def send_movie_info(self, evt: MessageEvent, movie) -> None: async def send_html_message(self, text_message, html_message) -> 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( content = TextMessageEventContent(
msgtype=MessageType.TEXT, format=Format.HTML, msgtype=MessageType.TEXT, format=Format.HTML,
body=f"{text_message}", body=f"{text_message}",
formatted_body=f"{html_message}") formatted_body=f"{html_message}")
await evt.respond(content) await evt.respond(content)
async def send_image(self, title, image) -> None:
mxc_uri = await self.client.upload_media(image)
content = MediaMessageEventContent( content = MediaMessageEventContent(
msgtype=MessageType.IMAGE, msgtype=MessageType.IMAGE,
body=f"Image {movie.title}", body=f"Image {title}",
url=f"{mxc_uri}") url=f"{mxc_uri}")
await evt.respond(content) 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"""<p><b>{escape(movie.title)}</b></p>
<p>{escape(movie.overview)[:overview_length]}{three_dotts}</p>
<p>{cast}</p>
<p>taken from www.themoviedb.org</p>"""
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.new("movie-id", help="Movie lookup by id")
@command.argument("message", pass_raw=True, required=True) @command.argument("message", pass_raw=True, required=True)