From 034547189ba7aea13c6596f490f58bc7c22c7308 Mon Sep 17 00:00:00 2001 From: j4nk Date: Fri, 21 Feb 2025 23:59:49 -0500 Subject: [PATCH] Added tv series and music search --- bot.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/bot.py b/bot.py index 436f9df..60c8b77 100644 --- a/bot.py +++ b/bot.py @@ -35,6 +35,8 @@ def json_to_key_value_string(json_data): return "\n".join(result_lines) class QBBot(slixmpp.ClientXMPP): + + def __init__(self, jid, password, room, nick, api_url, api_username, api_password): slixmpp.ClientXMPP.__init__(self, jid, password) @@ -50,7 +52,9 @@ class QBBot(slixmpp.ClientXMPP): self.jf_api_credentials = None self.jf_api_init() self.jf_api_login() - + + self.media_dictionary = {} + self.init_dictionary() self.add_event_handler("session_start", self.session_start) @@ -68,6 +72,14 @@ class QBBot(slixmpp.ClientXMPP): # Here's how to access plugins once you've registered them: # self['xep_0030'].add_feature('echo_demo') + def init_dictionary(self): + # put media type conversions here (don't know the pythonic way to do it) + self.media_dictionary['tv'] = "Series" + self.media_dictionary['movie'] = "Movie" + self.media_dictionary['movies'] = "Movie" + self.media_dictionary['music'] = "MusicAlbum" + + def jf_api_init(self): if self.jf_api is not None: print("Warning: API already initialized, doing nothing") @@ -142,6 +154,8 @@ class QBBot(slixmpp.ClientXMPP): # callback that will be executed when the reply is received. def message(self, msg): + #query = self.jf_api.jellyfin.search_media_items(term="dream theater distance over time") + if msg['type'] in ('chat', 'normal'): msg.reply("I was not designed to take direct messages, please use me in a MUC").send() @@ -160,19 +174,26 @@ class QBBot(slixmpp.ClientXMPP): case "search": category = tokens[2] category = category.lower() - if category not in ["movie", "tv", "music"]: + category = self.media_dictionary[category] if category in self.media_dictionary.keys() else category + if category not in ["Movie", "Series", "MusicAlbum"]: message += "Error: invalid category " + tokens[2] else: search_query = " ".join(tokens[3:]) query = self.jf_api.jellyfin.search_media_items(term=search_query, media=category) items = query['Items'] - items = [i for i in items if i['MediaType'] != "Unknown"] + #items = [i for i in items if i['MediaType'] != "Unknown"] if len(items) == 0: - message += f"{search_query} not found in category {category}" + message += f"{search_query} not found in category {category}\n" match category: - case "movie": + case "Movie": for i in items: message += f"{i['Name']}, {i['ProductionYear']}, {i['OfficialRating'] if 'OfficialRating' in i else "unknown rating"}, {i['Container']}\n" + case "Series": + for i in items: + message += f"{i['Name']}, {i['ProductionYear']}, {i['OfficialRating'] if 'OfficialRating' in i else "unknown rating"}, No. Episodes: {i['UserData']['UnplayedItemCount']}\n" + case "MusicAlbum": + for i in items: + message += f"{i['AlbumArtist'] if 'AlbumArtist' in i.keys() else "Unknown"} - {i['Name']}, {i['ProductionYear'] if 'ProductionYear' in i.keys() else "Unknown"}\n" message += "```"