diff --git a/.gitignore b/.gitignore index 892e881..c85af22 100644 --- a/.gitignore +++ b/.gitignore @@ -129,5 +129,5 @@ dmypy.json # Pyre type checker .pyre/ -lomion.tmdb.mbp +*.mbp test.db diff --git a/test_tmdb.py b/test_tmdb.py index a72fb89..2b48fb0 100644 --- a/test_tmdb.py +++ b/test_tmdb.py @@ -1,16 +1,18 @@ #!/usr/bin/env python3 import unittest import requests +from html import escape from tmdb.tmdb_api import Movie, TvShow, MoviePopular from tmdb.tmdb import TmdbBot, MessageConstructor from tmdb.database import Database from sqlalchemy import create_engine + def apiRequests(command): api_key = '51d75c00dc1502dc894b7773ec3e7a15' base_url = "https://api.themoviedb.org/3/" url = base_url + command.lstrip('/') - params={'api_key': api_key} + params = {'api_key': api_key} params.update({'language': 'en'}) return requests.get(url, params=params).json() @@ -125,5 +127,28 @@ class TestTmdbMethods(unittest.TestCase): list = MoviePopular() self.assertEqual(list.query(), results['total_results']) + def test_movie_popular_id(self): + results = apiRequests('/movie/popular') + list = MoviePopular() + list.query() + self.assertEqual(list.list[2]['id'], results['results'][2]['id']) + + def test_movie_popular_html(self): + results = apiRequests('/movie/popular') + list = MoviePopular() + list.query() + test_result = escape(results['results'][-1]['title']) + tested = list.getListHtml()[(len(results['results'][-1]['title']) + 8) * -1:] + self.assertEqual(tested, f"""{test_result}
""") + + def test_movie_popular_text(self): + results = apiRequests('/movie/popular') + list = MoviePopular() + list.query() + test_result = results['results'][-1]['title'] + tested = list.getListText()[(len(results['results'][-1]['title'])) * -1:] + self.assertEqual(tested, test_result) + + if __name__ == '__main__': unittest.main() diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index 56d5a77..a5b83b2 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.py @@ -21,7 +21,7 @@ from mautrix.types import TextMessageEventContent, MediaMessageEventContent, Mes from maubot import Plugin, MessageEvent from maubot.handlers import command -from tmdb.tmdb_api import TmdbApi, Movie, TvShow +from tmdb.tmdb_api import TmdbApi, Movie, TvShow, MoviePopular from tmdb.database import Database @@ -74,7 +74,7 @@ class TmdbBot(Plugin): #{cast}
#taken from www.themoviedb.org
""" - async def send_html_message(self, evt: MessageEvent, text_message, html_message) -> None: + async def send_html_message(self, evt: MessageEvent, text_message: str, html_message: str) -> None: content = TextMessageEventContent( msgtype=MessageType.TEXT, format=Format.HTML, body=f"{text_message}", @@ -90,6 +90,7 @@ class TmdbBot(Plugin): async def send_help(self, evt: MessageEvent) -> None: html = """Use !tmdb id movie {tmdb id} to get movie detail for tmdb-id.
Use !tmdb movie {title} [y:{release year}] to get movie detail based on the given title.
+Use !tmdb popular to get most popular movies.
Use !tmdb language {language} to set your prefered language.
Use !tmdb poster_size [{size}] to set your prefered poster size. With empty {size} all available sizes are listed.
Use !tmdb tvshow {title} to get detail about a tv show based on the given title.
""" @@ -147,21 +148,13 @@ class TmdbBot(Plugin): async def command_movie_id(self, evt: MessageEvent, message: str = "") -> None: await self.movie_id(evt, message) - async def movie_search(self, evt: MessageEvent, message: str = "") -> None: - movie = Movie() - self.poster_size(evt, movie) + async def movie_popular(self, evt: MessageEvent, message: str = "") -> None: + popular = MoviePopular() language = self.db.get_language(evt.sender) if language: - movie.set_language(language) - title, year = self.split_title_year(message) - movie.search_title(title, year) - if movie.valid: - await self.send_movie_info(evt, movie) - else: - content = TextMessageEventContent( - msgtype=MessageType.NOTICE, format=Format.HTML, - body="No movie found!") - await evt.respond(content) + popular.set_language(language) + length = 5 + await self.send_html_message(evt, popular.getListText(length), popular.getListHtml(length)) @command.new("movie-search", help="Movie lookup by Title") @command.argument("message", pass_raw=True, required=True) @@ -220,6 +213,22 @@ class TmdbBot(Plugin): async def command_tvshow_search(self, evt: MessageEvent, message: str = "") -> None: await self.tvshow_search(evt, message) + async def movie_search(self, evt: MessageEvent, message: str = "") -> None: + movie = Movie() + self.poster_size(evt, movie) + language = self.db.get_language(evt.sender) + if language: + movie.set_language(language) + title, year = self.split_title_year(message) + movie.search_title(title, year) + if movie.valid: + await self.send_movie_info(evt, movie) + else: + content = TextMessageEventContent( + msgtype=MessageType.NOTICE, format=Format.HTML, + body="No movie found!") + await evt.respond(content) + @command.new("tmdb", help="TMDB Bot") @command.argument("message", pass_raw=True, required=True) async def command_dispatcher(self, evt: MessageEvent, message: str = "") -> None: @@ -231,6 +240,8 @@ class TmdbBot(Plugin): await self.send_help(evt) elif command.lower() == 'movie': await self.movie_search(evt, parameters) + elif command.lower() == 'popular': + await self.movie_popular(evt, parameters) elif command.lower() == 'language': await self.movie_language(evt, parameters) elif command.lower() == 'poster_size': diff --git a/tmdb/tmdb_api.py b/tmdb/tmdb_api.py index 6197ac5..f57ccac 100644 --- a/tmdb/tmdb_api.py +++ b/tmdb/tmdb_api.py @@ -14,6 +14,7 @@ You should have received a copy of the GNU Affero General Public License along with tmdb-bot. If not, see