From c924fc84c79aff86c5a432f3d8a3f3ba31e14fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9A=AE=E6=98=8C=E9=92=B1?= <2907678297@qq.com> Date: Wed, 7 Feb 2024 00:00:00 +0800 Subject: [PATCH] fix: obsidian_setup.py can not download files when there are no corresponding resources the latest release --- obstoanki_setup.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/obstoanki_setup.py b/obstoanki_setup.py index 522a0e88..29785aa3 100644 --- a/obstoanki_setup.py +++ b/obstoanki_setup.py @@ -3,28 +3,35 @@ import subprocess import os -SCRIPT_URL = "".join( - [ - "https://github.com/Pseudonium/Obsidian_to_Anki/releases/latest", - "/download/obsidian_to_anki.py" - ] -) +SCRIPT_URL = [ + "https://github.com/ObsidianToAnki/Obsidian_to_Anki/releases/latest/download/obsidian_to_anki.py", + "https://raw.githubusercontent.com/ObsidianToAnki/Obsidian_to_Anki/master/obsidian_to_anki.py", +] -REQUIRE_URL = "".join( - [ - "https://github.com/Pseudonium/Obsidian_to_Anki/releases/latest", - "/download/requirements.txt" - ] -) -with urllib.request.urlopen(SCRIPT_URL) as script: - with open("obsidian_to_anki.py", "wb") as f: - f.write(script.read()) +REQUIRE_URL = [ + "https://github.com/ObsidianToAnki/Obsidian_to_Anki/releases/latest/download/requirements.txt", + "https://raw.githubusercontent.com/ObsidianToAnki/Obsidian_to_Anki/master/requirements.txt", +] -with urllib.request.urlopen(REQUIRE_URL) as require: - with open("obstoankirequire.txt", "wb") as f: - f.write(require.read()) +def download_file(urls, filename) : + for url in urls: + try: + with urllib.request.urlopen(url) as response: + with open(filename, "wb") as f: + f.write(response.read()) + print(f"Successfully downloaded {filename}.") + return True + except Exception as e: + print(f"An error occurred while downloading {filename} from {url}: {e}") + + print(f"Failed to download {filename} using all provided URLs.") + return False + +download_file(SCRIPT_URL, "obsidian_to_anki.py") + +if download_file(REQUIRE_URL, "obstoankirequire.txt"): subprocess.check_call( [sys.executable, "-m", "pip", "install", "-r", "obstoankirequire.txt"] ) - os.remove("obstoankirequire.txt") + os.remove("obstoankirequire.txt") \ No newline at end of file