Made categories mandatory for add and search, updated help

This commit is contained in:
j4nk 2024-08-28 22:21:28 -04:00
parent 71b970e68a
commit 46d096eeeb
1 changed files with 51 additions and 49 deletions

100
bot.py
View File

@ -300,13 +300,12 @@ class QBBot(slixmpp.ClientXMPP):
message += self.api_caller.torrentList("full")
case "add":
category = "unknown"
string_start = 2
if tokens[2].startswith("c="):
category = tokens[2].removeprefix("c=")
category = tokens[2]
if category not in ["all", "anime", "games", "movies", "music", "software", "tv"]:
message = "Please specify a category"
else:
string_start = 3
message += self.api_caller.add(tokens[string_start], msg['mucnick'], category)
message += self.api_caller.add(tokens[string_start], msg['mucnick'], category)
case "searchplugins":
resp = self.api_caller.get_search_plugins()
@ -323,48 +322,51 @@ class QBBot(slixmpp.ClientXMPP):
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!"
if len(tokens) < 4:
message = "Please specify a category and search term"
else:
search_id = json.loads(resp.text)["id"]
category = tokens[2]
if category not in ["all", "anime", "games", "movies", "music", "software", "tv"]:
message = "Invalid category: " + category
else:
search_token_start = 3 # qb search CATEGORY ...
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
# 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!"
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"
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]
# Report the number of torrents in the search results
message += "Total results for \"" + search_string + "\": " + str(len(the_list)) + "\n"
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] = [category if category != "all" else "unknown", the_list]
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"
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]
# Report the number of torrents in the search results
message += "Total results for \"" + search_string + "\" under category " + category + ": " + str(len(the_list)) + "\n"
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] = [category if category != "all" else "unknown", the_list]
case "searchselect":
user = msg['mucnick']
@ -384,10 +386,10 @@ class QBBot(slixmpp.ClientXMPP):
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 += "The whole search process is done with 2 commands.\n First, `search` is used to obtain a list of results.\n The required CATEGORY selects a category.\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 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 += self.nick + " search software ubuntu 16.04\n\n"
message += "Total results for \"ubuntu 16.04\" in category software: 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"
@ -406,9 +408,9 @@ class QBBot(slixmpp.ClientXMPP):
message += "help: Displays this help" + "\n"
message += "list: Lists torrents, names truncated to 25 characters\n"
message += "fulllist: Lists torrents, no name truncation\n"
message += "add [c=CATEGORY] MAGNET_URL: Adds torrent corresponding to MAGNET_URL to the download list.\n\tNote that this will take about 5 seconds, as there's a check for 0 seeds after 5 seconds as a warning.\n\tOptionally include CATEGORY to assist in automated copy.\n\tValid CATEGORY values can be found with the categories in searchplugins\n"
message += "add CATEGORY MAGNET_URL: Adds torrent corresponding to MAGNET_URL to the download list with category CATEGORY.\n\tNote that this will take about 5 seconds, as there's a check for 0 seeds after 5 seconds as a warning.\n\tValid CATEGORY values can be found with the searchplugins command.\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 += "search CATEGORY SEARCH_STRING: Search from all enabled plugins for SEARCH_STRING in CATEGORY. Valid CATEGORY values 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```"