Skip to content

Commit 576547f

Browse files
committed
feat(updater): downloader
Signed-off-by: Me0wo <[email protected]>
1 parent d7625b1 commit 576547f

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Updater/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from Updater.config.yml_loader import get_config
66
from Updater.updater.assets.name import get_package_name_from_current_machine
7+
from Updater.updater.downloader.temp import download_to_temp
78
from Updater.updater.runner.process import find_processes_by_path, try_terminate
89
from Updater.updater.tag.reader import read
910
from fetch.github_release_api import fetch_latest_release
@@ -40,10 +41,13 @@ def main():
4041
break
4142

4243
if download_url is None:
43-
print(f"Could not find asset for {get_package_name_from_current_machine()}")
44+
print(
45+
f"Could not find asset for {get_package_name_from_current_machine()}\nPlease update manually or send an issue.")
4446
return
4547

46-
print(download_url)
48+
downloaded_path = download_to_temp(download_url)
49+
50+
print(downloaded_path)
4751

4852
print("=" * 80)
4953
time.sleep(INTERVAL)

Updater/updater/downloader/__init__.py

Whitespace-only changes.

Updater/updater/downloader/temp.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import atexit
2+
import tempfile
3+
from datetime import datetime
4+
from pathlib import Path
5+
from urllib.parse import urlparse
6+
7+
import requests
8+
9+
10+
def download_to_temp(url: str) -> Path:
11+
original_name = Path(urlparse(url).path).name
12+
13+
filename = f"{Path(original_name).stem}_{datetime.now().strftime("%Y%m%d_%H%M%S")}{Path(original_name).suffix}"
14+
15+
temp_path = Path(tempfile.gettempdir()) / filename
16+
17+
with requests.get(url, stream=True) as r:
18+
r.raise_for_status()
19+
with open(temp_path, 'wb') as f:
20+
for chunk in r.iter_content(chunk_size=1024):
21+
if chunk:
22+
f.write(chunk)
23+
24+
atexit.register(lambda: temp_path.exists() and temp_path.unlink())
25+
26+
return temp_path

0 commit comments

Comments
 (0)