Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: obsidian_setup.py can not download files when there are no corresponding resources the latest release #529

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 26 additions & 19 deletions obstoanki_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")