Added testing config, add search functionality, update help

This commit is contained in:
j4nk 2024-08-01 23:10:11 -04:00
parent 379e4af2f8
commit 33812eac65
3 changed files with 133 additions and 2 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
config.conf
config_testing.conf
*~
#*

125
bot.py
View File

@ -150,6 +150,31 @@ class QBittorrentAPICaller():
return "Successfully added " + p["name"]
return "Could not add torrent, please double check the magnet link (hash=" + magnet_hash + ")"
def get_search_plugins(self):
the_url = self.url + "/" + "api/v2/search/plugins"
return self.session.post(the_url)
def search_start(self, searchstring, category="all"):
the_url = self.url + "/" + "api/v2/search/start"
return self.session.post(the_url, data={"pattern":searchstring, "plugins":"enabled", "category":category})
def search_status(self, search_id):
the_url = self.url + "/" + "api/v2/search/status"
return self.session.post(the_url, data={"id":search_id})
def search_stop(self, search_id):
the_url = self.url + "/" + "api/v2/search/stop"
return self.session.post(the_url, data={"id":search_id})
def search_results(self, search_id):
the_url = self.url + "/" + "api/v2/search/results"
return self.session.post(the_url, data={"id":search_id})
def search_delete(self, search_id):
the_url = self.url + "/" + "api/v2/search/delete"
return self.session.post(the_url, data={"id":search_id})
class QBBot(slixmpp.ClientXMPP):
@ -161,6 +186,7 @@ class QBBot(slixmpp.ClientXMPP):
self.nick = nick
self.api_caller = QBittorrentAPICaller(api_url, api_username, api_password)
self.searchselects = {}
self.add_event_handler("session_start", self.session_start)
self.add_event_handler("message", self.message)
@ -275,14 +301,109 @@ class QBBot(slixmpp.ClientXMPP):
case "add":
message += self.api_caller.add(tokens[2], msg['mucnick'])
case "searchplugins":
resp = self.api_caller.get_search_plugins()
if (resp.status_code != 200):
message += "get_search_plugins() returned " + str(resp.status_code)
logging.warning("get_search_plugins() returned" + str(resp.status_code))
else:
parsed = json.loads(resp.text)
message += "```\n"
message += "\n".join([p["fullName"] + " (" + p["url"] + ")" + " v" + p["version"] + " | " +
("enabled" if p["enabled"] else "disabled") + " | " +
", ".join([c["id"] for c in p["supportedCategories"]])
for p in parsed])
message += "\n```"
case "search":
category = "all"
search_token_start=2
if tokens[2].startswith("c="):
category = tokens[2].removeprefix("c=")
search_token_start=3
search_string = " ".join(tokens[search_token_start:])
resp = self.api_caller.search_start(search_string, category)
if resp.status_code == 409:
message += "Server reported too many searches!"
else:
search_id = json.loads(resp.text)["id"]
# 30 second timeout
count = 6
while (count >= 0 and json.loads(self.api_caller.search_status(search_id).text)[0]["status"] != "Stopped"):
time.sleep(5)
count -= 1
if count == 0:
self.api_caller.search_stop(search_id)
message += "Search took longer than 30 seconds!"
res = self.api_caller.search_results(search_id)
# Delete the search, we already have the data
self.api_caller.search_delete(search_id)
parsed_res = json.loads(res.text)
message += "```\n"
message += "Total results for \"" + search_string + "\": " + str(parsed_res["total"]) + "\n"
the_list = [[r["fileName"], r["fileUrl"], str(r["nbSeeders"]), r["fileSize"]] for r in parsed_res["results"]]
# Remove torrents with no seeds from the search results
the_list = [i for i in the_list if int(i[2]) != 0]
message += "\n".join([str(i) + ". " + l[0] + ", "
"seeds: " + str(l[2]) + ", "
"size: " + '{0:.2f}'.format(int(l[3])/1024/1024/1024) + " GB"
for i, l in enumerate(the_list)])
message += "\n```"
# Register the users's latest search in the searchselects structure
user = msg['mucnick']
self.searchselects[user] = the_list
case "searchselect":
user = msg['mucnick']
if self.searchselects[user] is None:
message += "Please initiate a search first."
else:
selection = self.searchselects[user]
index = int(tokens[2])
if index < len(selection):
link = selection[index][1]
self.api_caller.add(link, user)
message += "Successfully added " + selection[index][0]
else:
message += "Error: index out of range"
case "searchhelp":
message += "Conducting a search\n"
message += "-------------------\n"
message += "The whole search process is done with 2 commands.\n First, `search` is used to obtain a list of results.\n The optional `c=CATEGORY` selects a category, by default it is \"all\".\n The full list of categories can be obtained from `searchplugins`, 3rd column.\n Note that only enabled plugins are utilized by this feature.\n I highly recommend using a category, or the search can take a really long time.\n Once the search process concludes (it takes at most 30 seconds), you will receive a list of indices, along with names, number of seeders, and file size.\n Once you choose the torrent you want, note the index and invoke `searchselect INDEX`.\n This will add the torrent to the queue.\n The following is an example usage of the search functionality.\n"
message += "```\n"
message += self.nick + " search c=software ubuntu 16.04\n\n"
message += "Total results for \"ubuntu 16.04\": 18\n"
message += "0. Ubuntu MATE 16.04.2 [MATE][armhf][img.xz][Uzerus], seeds: 260, size: 1.10 GB\n"
message += "1. Ubuntu 16.04.1 LTS Desktop 64-bit, seeds: 55, size: 1.40 GB\n"
message += "2. Ubuntu 16.04.5 LTS [Xenial Xerus][Unity][x64 x86_64 amd64][Server][ISO][Uzerus], seeds: 8, size: 0.60 GB\n"
message += "...\n\n"
message += self.nick + " searchselect 0\n\n"
message += "Successfully added Ubuntu MATE 16.04.2 [MATE][armhf][img.xz][Uzerus]\n"
message += "```\n"
message += "Note: .torrent files are not supported right now, some results may return .torrent file links rather than magnet links\n"
case "help"|_:
message += "```\n"
message += "Commands\n"
message += "info: Displays information about QBittorrent server" + "\n"
message += "help: Displays this help" + "\n"
message += "list: Lists torrents, downloading torrents' names truncated to 25 characters\n"
message += "list: Lists torrents, names truncated to 25 characters\n"
message += "fulllist: Lists torrents, no name truncation\n"
message += "add [MAGNET_URL]: Adds torrent corresponding to MAGNET_URL to the download list. Note that this will take about 5 seconds, as there's a check for 0 seeds after 5 seconds as a warning"
message += "add MAGNET_URL: Adds torrent corresponding to MAGNET_URL to the download list. Note that this will take about 5 seconds, as there's a check for 0 seeds after 5 seconds as a warning\n"
message += "searchplugins: List the installed search plugins\n"
message += "search [c=CATEGORY] search_string: Search from all enabled plugins for search_string, with optional category CATEGORY. Valid categories can be found from the searchplugins command.\n"
message += "searchselect: Selects a torrent from a previous search to download\n"
message += "searchhelp: Shows the in-depth process of utilizing search"
message += "\n```"
self.send_message(mto=msg['from'].bare,
mbody=message,

View File

@ -9,3 +9,12 @@ services:
- ./config.conf:/config.conf
- ./qbittorrent_logo.png:/qbittorrent_logo.png
command: /init.sh
qb_testing:
image: docker.io/alpine:latest # Use the latest Nginx image from Docker Hub
volumes:
- ./bot.py:/bot.py # Mount a local directory to the container
- ./init.sh:/init.sh
- ./config_testing.conf:/config.conf
- ./qbittorrent_logo.png:/qbittorrent_logo.png
command: /init.sh