Added search function

This commit is contained in:
Neocky 2022-06-16 22:35:54 +02:00
parent e229324117
commit d427886c49
5 changed files with 67 additions and 17 deletions

View File

@ -12,7 +12,7 @@ try:
from src.utils.utilities import check_requirements, api_test_spiget from src.utils.utilities import check_requirements, api_test_spiget
from src.handlers.handle_input import handle_input from src.handlers.handle_input import handle_input
except: except:
print("Folder 'src' not found in the directory or missing files detected! \ print("Folder 'src' not found in the directory or missing files or broken functions detected! \
\nPlease redownload the files from here: https://www.github.com/Neocky/pluGET") \nPlease redownload the files from here: https://www.github.com/Neocky/pluGET")
sys.exit() sys.exit()
@ -27,7 +27,7 @@ if __name__ == "__main__":
formatter_class=argparse.ArgumentDefaultsHelpFormatter) formatter_class=argparse.ArgumentDefaultsHelpFormatter)
#parser.add_argument("-a", "--archive", action="store_true", help="archive mode") #parser.add_argument("-a", "--archive", action="store_true", help="archive mode")
#parser.add_argument("--exclude", help="files to exclude") #parser.add_argument("--exclude", help="files to exclude")
parser.add_argument("mode", help="Mode (install/update)", nargs='?', default=None) parser.add_argument("mode", help="Mode (install/update/etc.)", nargs='?', default=None)
parser.add_argument("object", help="Object/Plugin Name", nargs='?', default=None) parser.add_argument("object", help="Object/Plugin Name", nargs='?', default=None)
parser.add_argument("version", help="Version", nargs='?', default=None) parser.add_argument("version", help="Version", nargs='?', default=None)
args = vars(parser.parse_args()) args = vars(parser.parse_args())

View File

@ -3,7 +3,7 @@ Handles the input through the pluGET command line
""" """
from src.utils.console_output import rich_print_error from src.utils.console_output import rich_print_error
from src.plugin.plugin_downloader import get_specific_plugin from src.plugin.plugin_downloader import get_specific_plugin, search_specific_plugin
# check # check
@ -60,8 +60,7 @@ def handle_input(input_command=None, input_selected_object=None, input_parameter
#checkInstalledPackage(inputSelectedObject, inputParams) #checkInstalledPackage(inputSelectedObject, inputParams)
case "search": case "search":
print("search package") search_specific_plugin(input_selected_object)
#searchPackage(inputSelectedObject)
case "remove": case "remove":
print("remove package") print("remove package")
#removePlugin(inputSelectedObject) #removePlugin(inputSelectedObject)
@ -85,7 +84,7 @@ def get_input() -> None:
Gets command line input and calls the handle input function Gets command line input and calls the handle input function
""" """
input_command = None input_command = None
print("'STRG + C' to exit") print("\n'STRG + C' to exit")
while True: while True:
try: try:
input_command, input_selected_object, *input_parameter = input("pluGET >> ").split() input_command, input_selected_object, *input_parameter = input("pluGET >> ").split()

View File

@ -4,10 +4,10 @@ File and functions which handle the download of the specific plugins
import re import re
import urllib.request import urllib.request
from urllib.error import HTTPError
from pathlib import Path from pathlib import Path
import rich from rich.table import Table
from rich.console import Console
from src.utils.utilities import convert_file_size_down, remove_temp_plugin_folder, create_temp_plugin_folder from src.utils.utilities import convert_file_size_down, remove_temp_plugin_folder, create_temp_plugin_folder
from src.utils.utilities import api_do_request from src.utils.utilities import api_do_request
@ -167,3 +167,53 @@ def get_specific_plugin(plugin_id, plugin_version="latest") -> None:
if config_values.connection != "local": if config_values.connection != "local":
remove_temp_plugin_folder() remove_temp_plugin_folder()
return None return None
def search_specific_plugin(plugin_name) -> None:
"""
Search for a name and return the top 10 results sorted for their download count
Then ask for input and download that plugin
"""
url= f"https://api.spiget.org/v2/search/resources/{plugin_name}?field=name&sort=-downloads"
plugin_search_results = api_do_request(url)
if plugin_search_results == None:
return None
print(f"Searching for {plugin_name}...")
print(f"Found plugins:")
# create table with rich
rich_table = Table(box=None)
rich_table.add_column("No.", justify="right", style="cyan", no_wrap=True)
rich_table.add_column("Name", style="bright_magenta")
rich_table.add_column("Downloads", justify="right", style="bright_green")
rich_table.add_column("Description", justify="left", style="white")
# start counting at 1 for all my non-programming friends :)
i = 1
for found_plugin in plugin_search_results:
plugin_name = handle_regex_package_name(found_plugin["name"])
plugin_downloads = found_plugin["downloads"]
plugin_description = found_plugin["tag"]
rich_table.add_row(str(i), plugin_name, str(plugin_downloads), plugin_description)
i += 1
# print table from rich
rich_console = Console()
rich_console.print(rich_table)
try:
plugin_selected = input("Select your wanted resource (No.)(0 to exit): ")
except KeyboardInterrupt:
return None
if plugin_selected == "0":
return None
try:
plugin_selected = int(plugin_selected) - 1
plugin_selected_id = plugin_search_results[plugin_selected]["id"]
except ValueError:
rich_print_error("Error: Input wasn't a number! Please try again!")
return None
except IndexError:
rich_print_error("Error: Number was out of range! Please try again!")
return None
get_specific_plugin(plugin_selected_id)

View File

@ -20,7 +20,7 @@ def api_do_request(url) -> list:
try: try:
response = requests.get(url, headers=webrequest_header) response = requests.get(url, headers=webrequest_header)
except: except:
print("Couldn't create webrequest") rich_print_error("Error: Couldn't create webrequest")
# return None to make functions quit # return None to make functions quit
return None return None
api_json_data = response.json() api_json_data = response.json()
@ -39,7 +39,7 @@ def api_test_spiget() -> None:
if r.status_code != 200: if r.status_code != 200:
rich_print_error("Error: Problems with the API detected. Plese try it again later!") rich_print_error("Error: Problems with the API detected. Plese try it again later!")
sys.exit() sys.exit()
return return None
def create_temp_plugin_folder() -> Path: def create_temp_plugin_folder() -> Path:
@ -107,3 +107,4 @@ def check_requirements() -> None:
print("Check sftp folder") print("Check sftp folder")
case "ftp": case "ftp":
print("check ftp folder") print("check ftp folder")
return None