Unifed command (see ticket #7)

This commit is contained in:
lomion 2020-07-03 15:06:33 +02:00
parent be86786893
commit fbaf6c6666
3 changed files with 67 additions and 21 deletions

View file

@ -2,18 +2,18 @@
A [maubot](https://github.com/maubot/maubot) to get information about movies from [TheMovieDB.org](https://www.themoviedb.org/). A [maubot](https://github.com/maubot/maubot) to get information about movies from [TheMovieDB.org](https://www.themoviedb.org/).
## Usage ## Usage
Use `!movie-help` to get help about the bot usage. Use `!tmdb help` to get help about the bot usage.
Use `!movie-id {tmdb id}` to get movie detail for tmdb-id. Use `!tmdb id movie {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. Use `!tmdb movie {title} [y:{release year}]` to get movie detail based on the given title.
Use `!movie-language {language}` to set your prefered language. Use `!tmdb language {language}` to set your prefered language.
Use `!tvshow-search {title}` to get detail about a tv show based on the given title. Use `!tmdb tvshow {title}` to get detail about a tv show based on the given title.
## Discussion ## Discussion
Matrix room: [#tmdb-bot:matrix.sarkasti.eu](https://matrix.to/#/#tmdb-bot:matrix.sarkasti.eu) Matrix room: [#tmdb-bot:matrix.sarkasti.eu](https://matrix.to/#/#tmdb-bot:matrix.sarkasti.eu)
## Avator ## Avatar
Avatar icons was made by [inipagistudio](https://www.flaticon.com/authors/inipagistudio) from [www.flaticon.com](https://www.flaticon.com/) Avatar icons was made by [inipagistudio](https://www.flaticon.com/authors/inipagistudio) from [www.flaticon.com](https://www.flaticon.com/)

View file

@ -95,7 +95,7 @@ class TestTmdbMethods(unittest.TestCase):
def test_cast(self): def test_cast(self):
movie = TvShow() movie = TvShow()
movie.search_title('The Flash') movie.search_title('The Flash')
self.assertEqual('Grant Gustin', movie.cast[0]) self.assertEqual('Tom Cavanagh', movie.cast[0])
self.assertEqual('Carlos Valdes', movie.cast[2]) self.assertEqual('Carlos Valdes', movie.cast[2])
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -55,11 +55,17 @@ class TmdbBot(Plugin):
formatted_body=f"{html_message}") formatted_body=f"{html_message}")
await evt.respond(content) await evt.respond(content)
async def send_notice(self, evt: MessageEvent, message: str = "") -> None:
content = TextMessageEventContent(
msgtype=MessageType.NOTICE, format=Format.HTML,
body=message)
await evt.respond(content)
async def send_help(self, evt: MessageEvent) -> None: async def send_help(self, evt: MessageEvent) -> None:
html = """Use <b>!movie-id {tmdb id}</b> to get movie detail for tmdb-id.</br> html = """Use <b>!tmdb id movie {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>!tmdb movie {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>!tmdb 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>""" Use <b>!tmdb tvshow {title}</b> to get detail about a tv show based on the given title.</br>"""
content = TextMessageEventContent( content = TextMessageEventContent(
msgtype=MessageType.TEXT, format=Format.HTML, msgtype=MessageType.TEXT, format=Format.HTML,
body=f"Help for TMDB Bot", body=f"Help for TMDB Bot",
@ -103,8 +109,6 @@ class TmdbBot(Plugin):
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())
@command.new("movie-id", help="Movie lookup by id")
@command.argument("message", pass_raw=True, required=True)
async def movie_id(self, evt: MessageEvent, message: str = "") -> None: async def movie_id(self, evt: MessageEvent, message: str = "") -> None:
movie = Movie() movie = Movie()
language = self.db.get_language(evt.sender) language = self.db.get_language(evt.sender)
@ -113,8 +117,11 @@ class TmdbBot(Plugin):
movie.query_details(message) movie.query_details(message)
await self.send_movie_info(evt, movie) await self.send_movie_info(evt, movie)
@command.new("movie-search", help="Movie lookup by Title") @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)
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: async def movie_search(self, evt: MessageEvent, message: str = "") -> None:
movie = Movie() movie = Movie()
language = self.db.get_language(evt.sender) language = self.db.get_language(evt.sender)
@ -130,8 +137,11 @@ class TmdbBot(Plugin):
body=f"No movie found!") body=f"No movie found!")
await evt.respond(content) await evt.respond(content)
@command.new("movie-language", help="Set language for lookup") @command.new("movie-search", help="Movie lookup by Title")
@command.argument("message", pass_raw=True, required=True) @command.argument("message", pass_raw=True, required=True)
async def command_movie_search(self, evt: MessageEvent, message: str = "") -> None:
await self.movie_search(evt, message)
async def movie_language(self, evt: MessageEvent, message: str = "") -> None: async def movie_language(self, evt: MessageEvent, message: str = "") -> None:
self.db.set_language(evt.sender, message) self.db.set_language(evt.sender, message)
content = TextMessageEventContent( content = TextMessageEventContent(
@ -139,6 +149,11 @@ class TmdbBot(Plugin):
body=f"Language set to {message}!") body=f"Language set to {message}!")
await evt.respond(content) await evt.respond(content)
@command.new("movie-language", help="Set language for lookup")
@command.argument("message", pass_raw=True, required=True)
async def command_movie_language(self, evt: MessageEvent, message: str = "") -> None:
await self.movie_language(evt, message)
@command.new("movie-help", help="Help for TMDB Bot") @command.new("movie-help", help="Help for TMDB Bot")
async def movie_help(self, evt: MessageEvent, message: str = "") -> None: async def movie_help(self, evt: MessageEvent, message: str = "") -> None:
await self.send_help(evt) await self.send_help(evt)
@ -147,8 +162,6 @@ class TmdbBot(Plugin):
async def tvshow_help(self, evt: MessageEvent, message: str = "") -> None: async def tvshow_help(self, evt: MessageEvent, message: str = "") -> None:
await self.send_help(evt) 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: async def tvshow_search(self, evt: MessageEvent, message: str = "") -> None:
movie = TvShow() movie = TvShow()
language = self.db.get_language(evt.sender) language = self.db.get_language(evt.sender)
@ -158,7 +171,40 @@ class TmdbBot(Plugin):
if movie.valid: if movie.valid:
await self.send_movie_info(evt, movie) await self.send_movie_info(evt, movie)
else: else:
content = TextMessageEventContent( await self.send_notice("No tv show found!")
msgtype=MessageType.NOTICE, format=Format.HTML,
body=f"No tv show found!") @command.new("tvshow-search", help="TV Show lookup by Title")
await evt.respond(content) @command.argument("message", pass_raw=True, required=True)
async def command_tvshow_search(self, evt: MessageEvent, message: str = "") -> None:
await self.tvshow_search(evt, message)
@command.new("tmdb", help="TMDB Bot")
@command.argument("message", pass_raw=True, required=True)
async def command_dispatcher(self, evt: MessageEvent, message: str = "") -> None:
m = re.search(r'^([^\s]*)\s*(.*)', message)
if m:
command = m.group(1)
parameters = m.group(2)
if command.lower() == 'help':
await self.send_help(evt)
elif command.lower() == 'movie':
await self.movie_search(evt, parameters)
elif command.lower() == 'language':
await self.movie_language(evt, parameters)
elif command.lower() == 'tvshow':
await self.tvshow_search(evt, parameters)
elif command.lower() == 'id':
m = re.search(r'^(movie|tvshow) (\d*)', parameters)
if m:
type = m.group(1)
id = m.group(2)
if type.lower() == 'movie':
await self.movie_id(evt, id)
elif type.lower() == 'tvshow':
await self.send_notice(evt, 'Not yet implemented. Search TV shows by title for now (!tmdb tvshow {title})')
else:
await self.send_notice(evt, 'Syntax wrong: !tmdb id {movie|tvshow} {tmdb id}')
else:
await self.send_notice(evt, 'Syntax wrong: !tmdb id {movie|tvshow} {tmdb id}')
else:
await self.send_notice(evt, 'Command unknown!')