From 053f7a37857a854b9dedb385a012b967bdd66fff Mon Sep 17 00:00:00 2001 From: lomion Date: Tue, 23 Jun 2020 15:47:40 +0200 Subject: [PATCH 1/9] Created unittest for HTML creation and refactored code to use it --- tmdb/test_tmdb.py => test_tmdb.py | 13 ++++++++- tmdb/tmdb.py | 44 +++++++++++++++++++------------ 2 files changed, 39 insertions(+), 18 deletions(-) rename tmdb/test_tmdb.py => test_tmdb.py (67%) diff --git a/tmdb/test_tmdb.py b/test_tmdb.py similarity index 67% rename from tmdb/test_tmdb.py rename to test_tmdb.py index 4d0cdc2..9740d04 100644 --- a/tmdb/test_tmdb.py +++ b/test_tmdb.py @@ -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, """

Fight Club

+

Ein Yuppie [...]

+

Acting: Edward Norton, Brad Pitt, Helena Bonham Carter

+

taken from www.themoviedb.org

""") + if __name__ == '__main__': unittest.main() diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index 59613ba..e798644 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.py @@ -57,33 +57,43 @@ class TmdbBot(Plugin): async def start(self) -> None: 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"""

{escape(movie.title)}

-

{escape(movie.overview)[:200]}{three_dotts}

-

{cast}

-

taken from www.themoviedb.org

""" + + 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"""

{escape(movie.title)}

+

{escape(movie.overview)[:overview_length]}{three_dotts}

+

{cast}

+

taken from www.themoviedb.org

""" + 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) From 75d2682458287e039efce1c1f9de5b43252f299c Mon Sep 17 00:00:00 2001 From: lomion Date: Tue, 23 Jun 2020 17:34:34 +0200 Subject: [PATCH 2/9] Moved database to own file --- test_tmdb.py | 11 +++++++++++ tmdb/database.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ tmdb/tmdb.py | 29 +---------------------------- 3 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 tmdb/database.py diff --git a/test_tmdb.py b/test_tmdb.py index 9740d04..e3b4ac3 100644 --- a/test_tmdb.py +++ b/test_tmdb.py @@ -2,6 +2,9 @@ import unittest from tmdb.tmdb_api import Movie from tmdb.tmdb import TmdbBot +from tmdb.database import Database +from sqlalchemy import create_engine + class TestTmdbMethods(unittest.TestCase): ### TMDB API @@ -43,5 +46,13 @@ class TestTmdbMethods(unittest.TestCase):

Acting: Edward Norton, Brad Pitt, Helena Bonham Carter

taken from www.themoviedb.org

""") + def test_database_language(self): + engine = create_engine('sqlite:///test.db', echo = True) + db = Database(engine) + db.set_language('@testuser:example.com', 'de') + self.assertEqual(str(db.get_language('@testuser:example.com')), 'de') + db.set_language('@testuser:example.com', 'en') + self.assertEqual(str(db.get_language('@testuser:example.com')), 'en') + if __name__ == '__main__': unittest.main() diff --git a/tmdb/database.py b/tmdb/database.py new file mode 100644 index 0000000..fdba0dd --- /dev/null +++ b/tmdb/database.py @@ -0,0 +1,44 @@ +''' +This file is part of tmdb-bot. + +tmdb-bot is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License version 3 as published by +the Free Software Foundation. + +tmdb-bot is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with tmdb-bot. If not, see . +''' + +from sqlalchemy import (Column, String, Integer, ForeignKey, Table, MetaData, + select, and_) +from sqlalchemy.engine.base import Engine + +class Database: + db: Engine + + def __init__(self, db: Engine) -> None: + self.db = db + + meta = MetaData() + meta.bind = db + + self.language = Table("tmdb_language", meta, + Column("id", Integer, primary_key=True, autoincrement=True), + Column("user_id", String(255), nullable=False), + Column("language", String(255), nullable=False),) + meta.create_all(db) + + def set_language(self, user_id, language): + with self.db.begin() as tx: + tx.execute(self.language.delete().where(self.language.c.user_id == user_id)) + tx.execute(self.language.insert().values(user_id=user_id, language=language)) + + def get_language(self, user_id): + rows = self.db.execute(select([self.language.c.language]) + .where(self.language.c.user_id == user_id)) + return rows.fetchone()['language'] \ No newline at end of file diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index e798644..2aefa0f 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.py @@ -21,35 +21,8 @@ from maubot import Plugin, MessageEvent from maubot.handlers import command from tmdb.tmdb_api import Movie +from tmdb.database import Database -from sqlalchemy import (Column, String, Integer, ForeignKey, Table, MetaData, - select, and_) -from sqlalchemy.engine.base import Engine - -class Database: - db: Engine - - def __init__(self, db: Engine) -> None: - self.db = db - - meta = MetaData() - meta.bind = db - - self.language = Table("tmdb_language", meta, - Column("id", Integer, primary_key=True, autoincrement=True), - Column("user_id", String(255), nullable=False), - Column("language", String(255), nullable=False),) - meta.create_all(db) - - def set_language(self, user_id, language): - with self.db.begin() as tx: - tx.execute(self.language.delete().where(self.language.c.user_id == user_id)) - tx.execute(self.language.insert().values(user_id=user_id, language=language)) - - def get_language(self, user_id): - rows = self.db.execute(select([self.language.c.language]) - .where(self.language.c.user_id == user_id)) - return rows.fetchone() class TmdbBot(Plugin): db: Database From 4d2f386b4f934e6211bee900944deb18e48c9169 Mon Sep 17 00:00:00 2001 From: lomion Date: Tue, 23 Jun 2020 17:47:37 +0200 Subject: [PATCH 3/9] Started refactoring tmdb_api --- .gitignore | 1 + tmdb/tmdb_api.py | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 7b8fdf6..892e881 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,4 @@ dmypy.json .pyre/ lomion.tmdb.mbp +test.db diff --git a/tmdb/tmdb_api.py b/tmdb/tmdb_api.py index c6a82a4..e9529b0 100644 --- a/tmdb/tmdb_api.py +++ b/tmdb/tmdb_api.py @@ -15,9 +15,18 @@ along with tmdb-bot. If not, see . ''' import requests -class Connection(): +class TmdbApi(): def __init__(self): self.load_parameters() + self.title = None + self.id = None + self.poster_url = None + self.overview = None + self.web_url = None + self.vote_average = None + self.language = 'de' + + self.valid = False def load_parameters(self): self.api_key = '51d75c00dc1502dc894b7773ec3e7a15' @@ -25,18 +34,16 @@ class Connection(): self.base_url = "https://api.themoviedb.org/3/" result = requests.get(self.base_url + 'configuration', params = self.get_apikey()).json() self.base_url_poster = result['images']['base_url'] + result['images']['poster_sizes'][0] - self.valid = False - - self.language = 'de' def get_apikey(self): return { 'api_key' : self.api_key } - def request(self, request_uri): + def request(self, request_uri, payload : dict = {}): url = self.base_url + request_uri - payload = self.get_apikey() - payload['language'] = self.language - result = requests.get(url, params=payload) + _payload = self.get_apikey() + _payload['language'] = self.language + _payload.update(payload) + result = requests.get(url, params=_payload) self.valid = True return result @@ -44,11 +51,15 @@ class Connection(): self.language = language def get_image_binary(self): - return requests.get(self.poster_url).content + if self.poster_url: + return requests.get(self.poster_url).content + else: + return None -class Movie(Connection): +class Movie(TmdbApi): def __init__(self): + super().__init__() self.load_parameters() pass From 10955cc96fa589c3afa9ff4958948c90b723ca6d Mon Sep 17 00:00:00 2001 From: lomion Date: Wed, 24 Jun 2020 10:45:18 +0200 Subject: [PATCH 4/9] Handling for Tv Shows (tmdb_api part) --- test_tmdb.py | 30 +++++++++++++++++++++++++++- tmdb/tmdb_api.py | 52 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/test_tmdb.py b/test_tmdb.py index e3b4ac3..2b9f5d5 100644 --- a/test_tmdb.py +++ b/test_tmdb.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 import unittest -from tmdb.tmdb_api import Movie +from tmdb.tmdb_api import Movie, TvShow from tmdb.tmdb import TmdbBot from tmdb.database import Database from sqlalchemy import create_engine @@ -54,5 +54,33 @@ class TestTmdbMethods(unittest.TestCase): db.set_language('@testuser:example.com', 'en') self.assertEqual(str(db.get_language('@testuser:example.com')), 'en') + def test_id_lookup(self): + movie = Movie() + movie.query_details('2108') + self.assertEqual('The Breakfast Club', movie.title) + + def test_search_fails(self): + movie = Movie() + id = movie.search_title('Breakfast Club 2019') + self.assertEqual(id, None) + self.assertEqual(None, movie.title) + + # TV Shows + def test_search_tvshow(self): + movie = TvShow() + id = movie.search_title('The Flash') + self.assertEqual(id, 60735) + + def test_tv_title(self): + movie = TvShow() + movie.search_title('The Flash') + self.assertEqual('The Flash', movie.title) + + def test_cast(self): + movie = TvShow() + movie.search_title('The Flash') + self.assertEqual('Grant Gustin', movie.cast[0]) + self.assertEqual('Carlos Valdes', movie.cast[2]) + if __name__ == '__main__': unittest.main() diff --git a/tmdb/tmdb_api.py b/tmdb/tmdb_api.py index e9529b0..b2013b7 100644 --- a/tmdb/tmdb_api.py +++ b/tmdb/tmdb_api.py @@ -38,12 +38,11 @@ class TmdbApi(): def get_apikey(self): return { 'api_key' : self.api_key } - def request(self, request_uri, payload : dict = {}): - url = self.base_url + request_uri - _payload = self.get_apikey() - _payload['language'] = self.language - _payload.update(payload) - result = requests.get(url, params=_payload) + def request(self, request_uri, params : dict = {}): + url = self.base_url + request_uri.lstrip('/') + params.update(self.get_apikey()) + params.update({ 'language' : self.language }) + result = requests.get(url, params=params) self.valid = True return result @@ -60,15 +59,11 @@ class TmdbApi(): class Movie(TmdbApi): def __init__(self): super().__init__() - self.load_parameters() - pass def search_title(self, title): - url = self.base_url+ 'search/movie' - payload = self.get_apikey() - payload['language'] = self.language + payload = {} payload['query'] = title - result = requests.get(url, params=payload) + result = self.request('search/movie', params=payload) json = result.json() if json['total_results'] > 0: movie_id = json['results'][0]['id'] @@ -93,4 +88,37 @@ class Movie(TmdbApi): def get_cast(self, amount): return self.cast[:amount] + + +class TvShow(TmdbApi): + def __init__(self): + super().__init__() + def search_title(self, title): + payload = {} + payload['query'] = title + result = self.request('/search/tv', params=payload) + json = result.json() + if json['total_results'] > 0: + movie_id = json['results'][0]['id'] + self.query_details(movie_id) + return movie_id + + def query_details(self, id): + data = self.request('tv/' + str(id)).json() + self.title = data['name'] + self.id = data['id'] + self.poster_url = self.base_url_poster + data['poster_path'] + self.overview = data['overview'] + self.web_url = 'https://www.themoviedb.org/tv/' + str(self.id) + self.vote_average = str(data['vote_average']) + self.query_cast() + + def query_cast(self): + data = self.request('tv/'+str(self.id)+'/credits').json() + self.cast = [] + for actor in data['cast']: + self.cast.append(actor['name']) + + def get_cast(self, amount): + return self.cast[:amount] \ No newline at end of file From 984ce533e58aa91ac98c69ba4cc239c4a2a8d2cd Mon Sep 17 00:00:00 2001 From: lomion Date: Wed, 24 Jun 2020 10:51:42 +0200 Subject: [PATCH 5/9] Turned movie title into link --- test_tmdb.py | 2 +- tmdb/tmdb.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test_tmdb.py b/test_tmdb.py index 2b9f5d5..f037b8e 100644 --- a/test_tmdb.py +++ b/test_tmdb.py @@ -41,7 +41,7 @@ class TestTmdbMethods(unittest.TestCase): tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ) movie.query_details('550') message = tmdb.construct_html_message(movie, overview_length = 10) - self.assertEqual(message, """

Fight Club

+ self.assertEqual(message, """

Fight Club

Ein Yuppie [...]

Acting: Edward Norton, Brad Pitt, Helena Bonham Carter

taken from www.themoviedb.org

""") diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index 2aefa0f..b07698c 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.py @@ -56,7 +56,7 @@ class TmdbBot(Plugin): for actor in movie.cast[:cast_length]: cast+= f'{actor}, ' cast = cast[:-2] - html_message = f"""

{escape(movie.title)}

+ html_message = f"""

{escape(movie.title)}

{escape(movie.overview)[:overview_length]}{three_dotts}

{cast}

taken from www.themoviedb.org

""" From 59a6ce50bfee93c8123fcf3c31ca974cc74ad06f Mon Sep 17 00:00:00 2001 From: lomion Date: Wed, 24 Jun 2020 11:17:19 +0200 Subject: [PATCH 6/9] Handling for Tv Shows (tmdb part) --- maubot.yaml | 2 +- test_tmdb.py | 2 +- tmdb/tmdb.py | 28 ++++++++++++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/maubot.yaml b/maubot.yaml index 9784f72..92fe944 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,6 +1,6 @@ maubot: 0.1.0 id: lomion.tmdb -version: 0.0.3 +version: 0.2.0 license: AGPL 3.0 modules: - tmdb diff --git a/test_tmdb.py b/test_tmdb.py index f037b8e..123c241 100644 --- a/test_tmdb.py +++ b/test_tmdb.py @@ -44,7 +44,7 @@ class TestTmdbMethods(unittest.TestCase): self.assertEqual(message, """

Fight Club

Ein Yuppie [...]

Acting: Edward Norton, Brad Pitt, Helena Bonham Carter

-

taken from www.themoviedb.org

""") +

Taken from www.themoviedb.org

""") def test_database_language(self): engine = create_engine('sqlite:///test.db', echo = True) diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index b07698c..4d9a8d1 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.py @@ -20,7 +20,7 @@ from mautrix.types import TextMessageEventContent, MediaMessageEventContent, Mes from maubot import Plugin, MessageEvent from maubot.handlers import command -from tmdb.tmdb_api import Movie +from tmdb.tmdb_api import Movie, TvShow from tmdb.database import Database @@ -31,14 +31,14 @@ class TmdbBot(Plugin): await super().start() self.db = Database(self.database) - async def send_html_message(self, text_message, html_message) -> None: + async def send_html_message(self, evt: MessageEvent, 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: + async def send_image(self, evt: MessageEvent, title, image) -> None: mxc_uri = await self.client.upload_media(image) content = MediaMessageEventContent( msgtype=MessageType.IMAGE, @@ -59,13 +59,13 @@ class TmdbBot(Plugin): html_message = f"""

{escape(movie.title)}

{escape(movie.overview)[:overview_length]}{three_dotts}

{cast}

-

taken from www.themoviedb.org

""" +

Taken from www.themoviedb.org

""" 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()) + await self.send_html_message(evt, f'{movie.title}', html_message) + await self.send_image(evt, movie.title, movie.get_image_binary()) @command.new("movie-id", help="Movie lookup by id") @@ -103,3 +103,19 @@ class TmdbBot(Plugin): msgtype=MessageType.NOTICE, format=Format.HTML, body=f"Language set to {message}!") await evt.respond(content) + + @command.new("tvshow-search", help="TV Show lookup by Title") + @command.argument("message", pass_raw=True, required=True) + async def tvshow_search(self, evt: MessageEvent, message: str = "") -> None: + movie = TvShow() + language = self.db.get_language(evt.sender) + if language: + movie.set_language(language) + movie.search_title(message) + if movie.valid: + await self.send_movie_info(evt, movie) + else: + content = TextMessageEventContent( + msgtype=MessageType.NOTICE, format=Format.HTML, + body=f"No tv show found!") + await evt.respond(content) \ No newline at end of file From 05fd4b8f0b6ed6d460bb33546418025e7baafd90 Mon Sep 17 00:00:00 2001 From: lomion Date: Wed, 24 Jun 2020 11:19:58 +0200 Subject: [PATCH 7/9] README.md: Added command for tv show search --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bff2726..80526e7 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,13 @@ A [maubot](https://github.com/maubot/maubot) to get information about movies from [TheMovieDB.org](https://www.themoviedb.org/). ## Usage -Use `!movie-id ` to get movie detail for tmdb-id. +Use `!movie-id {tmdb id}` to get movie detail for tmdb-id. -Use `!movie-search ` to get movie detail based on the given title. +Use `!movie-search {title}` to get movie detail based on the given title. -Use `!movie-language <language>` to set your prefered language. +Use `!movie-language {language}` to set your prefered language. + +Use `!tvshow-search {title}` to get detail about a tv show based on the given title. ## Discussion Matrix room: [#tmdb-bot:matrix.sarkasti.eu](https://matrix.to/#/#tmdb-bot:matrix.sarkasti.eu) \ No newline at end of file From 0eafc0fc3c69b08d2158dc6312cdc6bc40ff827b Mon Sep 17 00:00:00 2001 From: lomion <lomion@aon.at> Date: Fri, 26 Jun 2020 16:41:44 +0200 Subject: [PATCH 8/9] Enhanced movie search by release year --- README.md | 2 +- test_tmdb.py | 18 +++++++++++++++++- tmdb/tmdb.py | 12 +++++++++++- tmdb/tmdb_api.py | 4 +++- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 80526e7..4ada9d9 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A [maubot](https://github.com/maubot/maubot) to get information about movies fro ## Usage Use `!movie-id {tmdb id}` to get movie detail for tmdb-id. -Use `!movie-search {title}` to get movie detail based on the given title. +Use `!movie-search {title} [y:{release year}]` to get movie detail based on the given title. Use `!movie-language {language}` to set your prefered language. diff --git a/test_tmdb.py b/test_tmdb.py index 123c241..ac8f3af 100644 --- a/test_tmdb.py +++ b/test_tmdb.py @@ -64,7 +64,23 @@ class TestTmdbMethods(unittest.TestCase): id = movie.search_title('Breakfast Club 2019') self.assertEqual(id, None) self.assertEqual(None, movie.title) - + + 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) + + def test_split_year(self): + tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" ) + title, year = tmdb.split_title_year('Dune') + self.assertEqual('Dune', title) + self.assertEqual(None, year) + title, year = tmdb.split_title_year('Dune y:2020 ') + self.assertEqual('Dune', title) + self.assertEqual(2020, year) + # TV Shows def test_search_tvshow(self): movie = TvShow() diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index 4d9a8d1..2afd256 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.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 <https://www.gnu.org/licenses/>. ''' from html import escape +import re from mautrix.types import TextMessageEventContent, MediaMessageEventContent, MessageType, Format, RelatesTo, RelationType @@ -62,6 +63,14 @@ class TmdbBot(Plugin): <p>Taken from www.themoviedb.org</p>""" return html_message + def split_title_year(self, message : str) -> (str, int): + m = re.search(r'^(.*) (y:\d\d\d\d)', message) + if m: + title = m.group(1) + year = int(m.group(2)[2:]) + return (title, year) + return (message, None) + async def send_movie_info(self, evt: MessageEvent, movie) -> None: html_message = self.construct_html_message(movie) await self.send_html_message(evt, f'{movie.title}', html_message) @@ -86,7 +95,8 @@ class TmdbBot(Plugin): language = self.db.get_language(evt.sender) if language: movie.set_language(language) - movie.search_title(message) + title, year = self.split_title_year(message) + movie.search_title(title, year) if movie.valid: await self.send_movie_info(evt, movie) else: diff --git a/tmdb/tmdb_api.py b/tmdb/tmdb_api.py index b2013b7..cb2ee1f 100644 --- a/tmdb/tmdb_api.py +++ b/tmdb/tmdb_api.py @@ -60,9 +60,11 @@ class Movie(TmdbApi): def __init__(self): super().__init__() - def search_title(self, title): + def search_title(self, title : str, year: int = None) -> int: payload = {} payload['query'] = title + if year: + payload['year'] = year result = self.request('search/movie', params=payload) json = result.json() if json['total_results'] > 0: From 8408c63191dee46f34334b72c05f1e45fcf07ee0 Mon Sep 17 00:00:00 2001 From: lomion <lomion@aon.at> Date: Fri, 26 Jun 2020 17:43:13 +0200 Subject: [PATCH 9/9] Added help command !movie-help or !tvshow-help --- README.md | 2 ++ tmdb/tmdb.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4ada9d9..c0164cd 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ A [maubot](https://github.com/maubot/maubot) to get information about movies from [TheMovieDB.org](https://www.themoviedb.org/). ## Usage +Use `!movie-help` to get help about the bot usage. + Use `!movie-id {tmdb id}` to get movie detail for tmdb-id. Use `!movie-search {title} [y:{release year}]` to get movie detail based on the given title. diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index 2afd256..f298746 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.py @@ -39,6 +39,17 @@ class TmdbBot(Plugin): formatted_body=f"{html_message}") await evt.respond(content) + async def send_help(self, evt: MessageEvent) -> None: + html = """Use <b>!movie-id {tmdb id}</b> to get movie detail for tmdb-id.</br> + Use <b>!movie-search {title} [y:{release year}]</b> to get movie detail based on the given title.</br> + Use <b>!movie-language {language}</b> to set your prefered language.</br> + Use <b>!tvshow-search {title}</b> to get detail about a tv show based on the given title.</br>""" + content = TextMessageEventContent( + msgtype=MessageType.TEXT, format=Format.HTML, + body=f"Help for TMDB Bot", + formatted_body=f"{html}") + await evt.respond(content) + async def send_image(self, evt: MessageEvent, title, image) -> None: mxc_uri = await self.client.upload_media(image) content = MediaMessageEventContent( @@ -114,6 +125,14 @@ class TmdbBot(Plugin): body=f"Language set to {message}!") await evt.respond(content) + @command.new("movie-help", help="Help for TMDB Bot") + async def movie_help(self, evt: MessageEvent, message: str = "") -> None: + await self.send_help(evt) + + @command.new("tvshow-help", help="Help for TMDB Bot") + async def tvshow_help(self, evt: MessageEvent, message: str = "") -> None: + await self.send_help(evt) + @command.new("tvshow-search", help="TV Show lookup by Title") @command.argument("message", pass_raw=True, required=True) async def tvshow_search(self, evt: MessageEvent, message: str = "") -> None: @@ -128,4 +147,18 @@ class TmdbBot(Plugin): content = TextMessageEventContent( msgtype=MessageType.NOTICE, format=Format.HTML, body=f"No tv show found!") - await evt.respond(content) \ No newline at end of file + await evt.respond(content) + +# @command.new("movie-help", help="Help text") +''' @command.new("tvshow-help", help="Help text") + @command.argument("message", pass_raw=True, required=True) + async def movie_help(self, evt: MessageEvent, message: str = "") -> None: + content = TextMessageEventContent( + msgtype=MessageType.NOTICE, format=Format.HTML, + body=f"Help for TMDB Bot") + , + formatted_body = f"""<p>Use !movie-id {tmdb id} to get movie detail for tmdb-id.</p> + <p>Use !movie-search {title} [y:{release year}] to get movie detail based on the given title.</p> + <p>Use !movie-language {language} to set your prefered language.</p> + <p>Use !tvshow-search {title} to get detail about a tv show based on the given title.</p>""") + await evt.respond(content)''' \ No newline at end of file