Moved construct_html_message into own class. Next step: additional method to create body.
This commit is contained in:
parent
5069551064
commit
aba4a0e5da
10
test_tmdb.py
10
test_tmdb.py
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import unittest
|
import unittest
|
||||||
from tmdb.tmdb_api import Movie, TvShow
|
from tmdb.tmdb_api import Movie, TvShow
|
||||||
from tmdb.tmdb import TmdbBot
|
from tmdb.tmdb import TmdbBot, MessageConstructor
|
||||||
from tmdb.database import Database
|
from tmdb.database import Database
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
|
|
||||||
|
@ -40,7 +40,9 @@ class TestTmdbMethods(unittest.TestCase):
|
||||||
movie = Movie()
|
movie = Movie()
|
||||||
tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" )
|
tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" )
|
||||||
movie.query_details('550')
|
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, """<p><a href="https://www.themoviedb.org/movie/550"><b>Fight Club</b></a></p>
|
self.assertEqual(message, """<p><a href="https://www.themoviedb.org/movie/550"><b>Fight Club</b></a></p>
|
||||||
<p>A ticking- [...]</p>
|
<p>A ticking- [...]</p>
|
||||||
<p>Acting: Edward Norton, Brad Pitt, Helena Bonham Carter</p>
|
<p>Acting: Edward Norton, Brad Pitt, Helena Bonham Carter</p>
|
||||||
|
@ -68,9 +70,9 @@ class TestTmdbMethods(unittest.TestCase):
|
||||||
def test_search_year(self):
|
def test_search_year(self):
|
||||||
movie = Movie()
|
movie = Movie()
|
||||||
id = movie.search_title('Dune')
|
id = movie.search_title('Dune')
|
||||||
self.assertEqual(id, 841)
|
|
||||||
id = movie.search_title('Dune', 2020)
|
|
||||||
self.assertEqual(id, 438631)
|
self.assertEqual(id, 438631)
|
||||||
|
id = movie.search_title('Dune', 1984)
|
||||||
|
self.assertEqual(id, 841)
|
||||||
|
|
||||||
def test_split_year(self):
|
def test_split_year(self):
|
||||||
tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" )
|
tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" )
|
||||||
|
|
45
tmdb/tmdb.py
45
tmdb/tmdb.py
|
@ -24,6 +24,32 @@ from maubot.handlers import command
|
||||||
from tmdb.tmdb_api import TmdbApi, Movie, TvShow
|
from tmdb.tmdb_api import TmdbApi, Movie, TvShow
|
||||||
from tmdb.database import Database
|
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"""<p><a href="{self.movie.web_url}"><b>{escape(self.movie.title)}</b></a></p>
|
||||||
|
<p>{escape(self.movie.overview)[:self.overview_length]}{self.three_dotts()}</p>
|
||||||
|
<p>{self.cast()}</p>
|
||||||
|
<p>Taken from www.themoviedb.org</p>"""
|
||||||
|
return html_message
|
||||||
|
|
||||||
|
|
||||||
class TmdbBot(Plugin):
|
class TmdbBot(Plugin):
|
||||||
db: Database
|
db: Database
|
||||||
|
@ -81,22 +107,6 @@ class TmdbBot(Plugin):
|
||||||
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><a href="{movie.web_url}"><b>{escape(movie.title)}</b></a></p>
|
|
||||||
<p>{escape(movie.overview)[:overview_length]}{three_dotts}</p>
|
|
||||||
<p>{cast}</p>
|
|
||||||
<p>Taken from www.themoviedb.org</p>"""
|
|
||||||
return html_message
|
|
||||||
|
|
||||||
def split_title_year(self, message : str) -> (str, int):
|
def split_title_year(self, message : str) -> (str, int):
|
||||||
m = re.search(r'^(.*) (y:\d\d\d\d)', message)
|
m = re.search(r'^(.*) (y:\d\d\d\d)', message)
|
||||||
if m:
|
if m:
|
||||||
|
@ -116,7 +126,8 @@ class TmdbBot(Plugin):
|
||||||
movie.set_poster_size(size)
|
movie.set_poster_size(size)
|
||||||
|
|
||||||
async def send_movie_info(self, evt: MessageEvent, movie) -> None:
|
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_html_message(evt, f'{movie.title}', html_message)
|
||||||
await self.send_image(evt, movie.title, movie.get_image_binary())
|
await self.send_image(evt, movie.title, movie.get_image_binary())
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue