Added better error handling to download function

This commit is contained in:
Jan-Luca Bogdan | BEL NET GmbH 2022-06-21 09:57:52 +02:00
parent 8620dfbe74
commit 96b4411020
4 changed files with 24 additions and 8 deletions

View File

@ -40,7 +40,7 @@ if __name__ == "__main__":
handle_input(args["mode"], args["object"], args["version"], arguments_from_console=True) handle_input(args["mode"], args["object"], args["version"], arguments_from_console=True)
else: else:
# no arguments were used so start pluGET console # no arguments were used so start pluGET console
#clear_console() clear_console()
print_logo() print_logo()
check_for_pluGET_update() check_for_pluGET_update()
handle_input() handle_input()

View File

@ -101,11 +101,13 @@ def download_specific_plugin_version_spiget(plugin_id, download_path, version_id
rich_print_error("Sorry but specific version downloads aren't supported because of cloudflare protection. :(") rich_print_error("Sorry but specific version downloads aren't supported because of cloudflare protection. :(")
rich_print_error("Reverting to latest version.") rich_print_error("Reverting to latest version.")
#url = f"https://api.spiget.org/v2/resources/{plugin_id}/versions/latest/download" #throws 403 forbidden error...cloudflare :( #throws 403 forbidden error...cloudflare :(
#url = f"https://api.spiget.org/v2/resources/{plugin_id}/versions/latest/download"
url = f"https://api.spiget.org/v2/resources/{plugin_id}/download" url = f"https://api.spiget.org/v2/resources/{plugin_id}/download"
# use rich Progress() to create progress bar # use rich Progress() to create progress bar
with Progress() as progress: with Progress(transient=True) as progress:
header = {'user-agent': 'pluGET/1.0'} header = {'user-agent': 'pluGET/1.0'}
r = requests.get(url, headers=header, stream=True) r = requests.get(url, headers=header, stream=True)
try: try:
@ -175,6 +177,10 @@ def get_specific_plugin(plugin_id, plugin_version="latest") -> None:
plugin_version_name = get_version_name(plugin_id, plugin_version_id) plugin_version_name = get_version_name(plugin_id, plugin_version_id)
plugin_download_name = f"{plugin_name}-{plugin_version_name}.jar" plugin_download_name = f"{plugin_name}-{plugin_version_name}.jar"
download_plugin_path = Path(f"{download_path}/{plugin_download_name}") download_plugin_path = Path(f"{download_path}/{plugin_download_name}")
# if api requests weren't successfull stop function
if plugin_version_id == None or plugin_version_name == None:
rich_print_error("Error: Webrequest timed out")
return None
# set the plugin_version_id to None if a specific version wasn't given as parameter # set the plugin_version_id to None if a specific version wasn't given as parameter
if plugin_version == "latest" or plugin_version is None: if plugin_version == "latest" or plugin_version is None:
plugin_version_id = None plugin_version_id = None
@ -193,6 +199,7 @@ def search_specific_plugin(plugin_name) -> None:
url= f"https://api.spiget.org/v2/search/resources/{plugin_name}?field=name&sort=-downloads" url= f"https://api.spiget.org/v2/search/resources/{plugin_name}?field=name&sort=-downloads"
plugin_search_results = api_do_request(url) plugin_search_results = api_do_request(url)
if plugin_search_results == None: if plugin_search_results == None:
rich_print_error("Error: Webrequest wasn't successfull!")
return None return None
print(f"Searching for {plugin_name}...") print(f"Searching for {plugin_name}...")

View File

@ -155,6 +155,11 @@ def compare_plugin_version(plugin_latest_version, plugin_file_version) -> bool:
def check_installed_plugins(input_selected_object="all", input_parameter=None) -> None: def check_installed_plugins(input_selected_object="all", input_parameter=None) -> None:
""" """
Gets installed plugins and checks it against the apis if there are updates for the plugins available Gets installed plugins and checks it against the apis if there are updates for the plugins available
:param input_selected_object: Which plugin should be checked
:param input_parameter: Optional parameters
:returns: None
""" """
config_values = config_value() config_values = config_value()
plugin.create_plugin_list() plugin.create_plugin_list()
@ -171,7 +176,7 @@ def check_installed_plugins(input_selected_object="all", input_parameter=None) -
plugin_list = os.listdir(plugin_folder_path) plugin_list = os.listdir(plugin_folder_path)
# create simple progress bar from rich # create simple progress bar from rich
for plugin_file in track(plugin_list, description="Checking...", transient=True, style="bright_yellow"): for plugin_file in track(plugin_list, description="[cyan]Checking...", transient=True, style="bright_yellow"):
plugin_no_attributes = False plugin_no_attributes = False
match config_values.connection: match config_values.connection:
case "sftp": case "sftp":
@ -220,8 +225,8 @@ def search_plugin_spigot(plugin_file, plugin_file_name, plugin_file_version) ->
plugin_file_version2 = re.sub(r'(\-\w*)', '', plugin_file_version) plugin_file_version2 = re.sub(r'(\-\w*)', '', plugin_file_version)
if i == 2: if i == 2:
""" """
pluginNameinYML = eggCrackingJar(plugin_file, 'name') plugin_name_in_yml = eggCrackingJar(plugin_file, 'name')
url = "https://api.spiget.org/v2/search/resources/" + pluginNameinYML + "?field=name&sort=-downloads" url = f"https://api.spiget.org/v2/search/resources/{plugin_name_in_yml}?field=name&sort=-downloads"
try: try:
plugin_list = api_do_request(url) plugin_list = api_do_request(url)
except ValueError: except ValueError:

View File

@ -50,7 +50,11 @@ def api_do_request(url) -> list:
rich_print_error("Error: 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
try:
api_json_data = response.json() api_json_data = response.json()
except:
rich_print_error("Error: Couldn't parse json of webrequest")
return None
return api_json_data return api_json_data