Created unittest for HTML creation and refactored code to use it
This commit is contained in:
parent
985bb0ce91
commit
053f7a3785
|
@ -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, """<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__':
|
||||
unittest.main()
|
42
tmdb/tmdb.py
42
tmdb/tmdb.py
|
@ -58,32 +58,42 @@ class TmdbBot(Plugin):
|
|||
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"""<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>"""
|
||||
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"""<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.argument("message", pass_raw=True, required=True)
|
||||
|
|
Loading…
Reference in a new issue