From e528b9600b679c564f22e88fd7f343bcda2b63f3 Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Fri, 23 Apr 2021 11:01:50 +0200 Subject: [PATCH] cr fix --- README.md | 32 ++++++++++++++------------------ modules/giphy.py | 19 +++++++++++++++++-- modules/tautulli.py | 14 ++++++++++++-- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 7278b76..cdd79eb 100644 --- a/README.md +++ b/README.md @@ -470,17 +470,15 @@ Can be used to post a picture from giphy given a query string. API Key: -The module has no API Key set up by defualt. You have to provide an api key by setting the environment variable `GIPHY_API_KEY`. +The module has no API Key set up by default. You have to provide an api key by using the relevant command. Read the documentation to create one at https://developers.giphy.com/docs/api -Environ variables seen by commands: - -* GIPHY_API_KEY: API Key for Giphy access - Commands: -* !giphy "query" - Post the first image result from giphy for the given "query" +* !giphy apikey [apikey] - Set api key (Must be done as bot owner) +* !giphy [query] - Post the first image result from giphy for the given [query] + Example: @@ -492,7 +490,7 @@ Can be used to post a picture from Gfycat given a query string. Commands: -* !gfycat "query" - Post the first image result from gfycat for the given "query" +* !gfycat [query] - Post the first image result from gfycat for the given [query] Example: @@ -504,26 +502,24 @@ Can be used to fetch recently added information from Tautulli or receive Tautull Commands: -* !tautulli movie - Show the last 10 recently added movies to Plex library monitered by Tautulli -* !tautulli show - Show the last 10 recently added tv series epsiodes to Plex library monitered by Tautulli -* !tautulli artist - Show the last 10 recently added music artists to Plex library monitered by Tautulli -* !tautulli add "room_id" encrypted - Add the provided encrypted "room_id" to the list of rooms to post the recently added notifications received by the webhook -* !tautulli add "room_id" plain - Add the provided unencrypted "room_id" to the list of rooms to post the recently added notifications received by the webhook -* !tautulli remove "room_id" encrypted - Remove the provided encrypted "room_id" to the list of rooms to post the recently added notifications received by the webhook -* !tautulli remove "room_id" plain - Remove the provided unencrypted "room_id" to the list of rooms to post the recently added notifications received by the webhook +* !tautulli apikey [apikey] - Set api key (Must be done as bot owner) +* !tautulli movie - Show the last 10 recently added movies to Plex library monitered by Tautulli +* !tautulli show - Show the last 10 recently added tv series epsiodes to Plex library monitered by Tautulli +* !tautulli artist - Show the last 10 recently added music artists to Plex library monitered by Tautulli +* !tautulli add [room_id] encrypted - Add the provided encrypted [room_id] to the list of rooms to post the recently added notifications received by the webhook +* !tautulli add [room_id] plain - Add the provided unencrypted [room_id] to the list of rooms to post the recently added notifications received by the webhook +* !tautulli remove [room_id] encrypted - Remove the provided encrypted [room_id] to the list of rooms to post the recently added notifications received by the webhook +* !tautulli remove [room_id] plain - Remove the provided unencrypted [room_id] to the list of rooms to post the recently added notifications received by the webhook Tautulli instance and API Key: The module work with an existing installed instance of Tautulli accessible on the machine at the path defined by env variable `TAUTULLI_PATH` -You have to provide an api key by setting the environment variable `TAUTULLI_API_KEY`. - -Read the documentation to create one at https://developers.giphy.com/docs/api +You have to provide an api key by using the relevant command. Environ variables seen by commands: * TAUTULLI_PATH: Path accessible from the machine to the installed instance of Tautulli * TAUTULLI_URL: Url accessible from the machine to the installed instance of Tautulli -* TAUTULLI_API_KEY: API Key for Tautulli access * TAUTULLI_NOTIFIER_ADDR: Listening address for the Tautulli webhook handler target * TAUTULLI_NOTIFIER_PORT: Listening port for the Tautulli webhook handler target * BOT_OWNERS: Owner of the rooms in the list for the notification webhook diff --git a/modules/giphy.py b/modules/giphy.py index 511c7dd..a4f7224 100644 --- a/modules/giphy.py +++ b/modules/giphy.py @@ -12,13 +12,21 @@ from collections import namedtuple from modules.common.module import BotModule class MatrixModule(BotModule): + api_key = None + async def matrix_message(self, bot, room, event): args = event.body.split() - if len(args) > 1: + if len(args) == 3 and args[1] == 'apikey': + bot.must_be_owner(event) + + self.api_key = args[2] + bot.save_settings() + await bot.send_text(room, 'Api key set') + elif len(args) > 1: gif_url = "No image found" query = event.body[len(args[0])+1:] try: - g = giphypop.Giphy(api_key=os.getenv("GIPHY_API_KEY")) + g = giphypop.Giphy(api_key=self.api_key) gifs = [] try: for x in g.search(phrase=query, limit=1): @@ -38,8 +46,15 @@ class MatrixModule(BotModule): else: await bot.send_text(room, 'Usage: !giphy ') + def get_settings(self): + data = super().get_settings() + data["api_key"] = self.api_key + return data + def set_settings(self, data): super().set_settings(data) + if data.get("api_key"): + self.api_key = data["api_key"] def help(self): return ('Giphy bot') diff --git a/modules/tautulli.py b/modules/tautulli.py index a570b50..a9f3c49 100644 --- a/modules/tautulli.py +++ b/modules/tautulli.py @@ -123,6 +123,7 @@ class WebServer: class MatrixModule(BotModule): httpd = None rooms = dict() + api_key = None def __init__(self, name): super().__init__(name) @@ -151,14 +152,20 @@ class MatrixModule(BotModule): async def matrix_message(self, bot, room, event): args = event.body.split() - if len(args) == 2: + if len(args) == 3 and args[1] == 'apikey': + bot.must_be_owner(event) + + self.api_key = args[2] + bot.save_settings() + await bot.send_text(room, 'Api key set') + elif len(args) == 2: media_type = args[1] if media_type != "movie" and media_type != "show" and media_type != "artist": await bot.send_text(room, "media type '%s' provided not valid" % media_type) return try: - url = "{}/api/v2?apikey={}&cmd=get_recently_added&count=10".format(os.getenv("TAUTULLI_URL"), os.getenv("TAUTULLI_API_KEY")) + url = "{}/api/v2?apikey={}&cmd=get_recently_added&count=10".format(os.getenv("TAUTULLI_URL"), self.api_key) req = urllib.request.Request(url+"&media_type="+media_type) connection = urllib.request.urlopen(req).read() entries = json.loads(connection) @@ -194,6 +201,7 @@ class MatrixModule(BotModule): def get_settings(self): data = super().get_settings() + data["api_key"] = self.api_key data["rooms"] = self.rooms self.httpd.rooms = self.rooms return data @@ -203,6 +211,8 @@ class MatrixModule(BotModule): if data.get("rooms"): self.rooms = data["rooms"] self.httpd.rooms = self.rooms + if data.get("api_key"): + self.api_key = data["api_key"] def help(self): return ('Tautulli recently added bot')