summaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
author苏向夜 <fu050409@163.com>2024-01-16 21:45:33 +0800
committer苏向夜 <fu050409@163.com>2024-01-16 21:45:33 +0800
commit8a95ec7348dee1ba5d29ba539c56098a847a8193 (patch)
treecf67c5abfab4f867123dc55c31e732104d223e49 /src
parent4aeac0408f5ea839ad6b3c149ea462eacde5c7b2 (diff)
downloadipm-8a95ec7348dee1ba5d29ba539c56098a847a8193.tar.gz
ipm-8a95ec7348dee1ba5d29ba539c56098a847a8193.zip
:sparkles: feat(api): extract download method
Diffstat (limited to 'src')
-rw-r--r--src/ipm/api.py35
-rw-r--r--src/ipm/const.py4
-rw-r--r--src/ipm/utils/loader.py25
3 files changed, 40 insertions, 24 deletions
diff --git a/src/ipm/api.py b/src/ipm/api.py
index 323ce5b..784f9d6 100644
--- a/src/ipm/api.py
+++ b/src/ipm/api.py
@@ -1,13 +1,11 @@
from pathlib import Path
from .typing import StrPath
-from .utils import freeze, urlparser
+from .utils import freeze, urlparser, loader
from .models.ipk import InfiniPackage
from .exceptions import FileTypeMismatch
-from .const import INDEX
+from .const import INDEX, HOME
import os
-import requests
-import tempfile
def build(source_path: StrPath):
@@ -21,33 +19,22 @@ def extract(source_path: StrPath, dist_path: StrPath | None = None):
freeze.extract_ipk(source_path, dist_path)
-def install(uri: str, index: str | None = ""):
- home = Path.home() / ".ipm" / "src"
- home.mkdir(parents=True, exist_ok=True)
+def install(uri: str, index: str = ""):
+ HOME.mkdir(parents=True, exist_ok=True)
index = index if index else INDEX
if os.path.isabs(uri):
if uri.endswith(".ipk"):
- extract(Path(uri).resolve(), home)
+ extract(Path(uri).resolve(), HOME)
else:
raise FileTypeMismatch("文件类型与预期[.ipk]不匹配.")
elif urlparser.is_valid_url(uri):
- ipk_bytes = requests.get(uri).content
- hash_bytes = requests.get(uri.rstrip("/") + ".hash").content
-
- temp_dir = tempfile.TemporaryDirectory()
- temp_path = Path(temp_dir.name).resolve()
-
- ipk_file = (temp_path / "temp.ipk").open("w+b")
- ipk_file.write(ipk_bytes)
- ipk_file.close()
-
- hash_file = (temp_path / "temp.ipk.hash").open("w+b")
- hash_file.write(hash_bytes)
- hash_file.close()
-
- extract(ipk_file, home)
- temp_dir.cleanup()
+ filename = uri.rstrip("/").split("/")[-1]
+ loader.load(
+ "temp",
+ uri.rstrip("/").rsplit("/")[0],
+ filename,
+ )
elif uri.isalpha():
...
else:
diff --git a/src/ipm/const.py b/src/ipm/const.py
index c9dac26..fba2345 100644
--- a/src/ipm/const.py
+++ b/src/ipm/const.py
@@ -1,2 +1,6 @@
+from pathlib import Path
+
DEBUG = True
+
INDEX = "https://ipm.hydroroll.team/index/"
+HOME = Path.home() / ".ipm" / "src"
diff --git a/src/ipm/utils/loader.py b/src/ipm/utils/loader.py
new file mode 100644
index 0000000..d74d85c
--- /dev/null
+++ b/src/ipm/utils/loader.py
@@ -0,0 +1,25 @@
+from pathlib import Path
+from ..api import extract
+from ..const import HOME
+
+import requests
+import tempfile
+
+
+def load(name: str, baseurl: str = "", filename: str = ""):
+ ipk_bytes = requests.get(baseurl.rstrip("/") + "/" + filename).content
+ hash_bytes = requests.get(baseurl.rstrip("/") + "/" + filename + ".hash").content
+
+ temp_dir = tempfile.TemporaryDirectory()
+ temp_path = Path(temp_dir.name).resolve()
+
+ ipk_file = (temp_path / f"{name}.ipk").open("w+b")
+ ipk_file.write(ipk_bytes)
+ ipk_file.close()
+
+ hash_file = (temp_path / f"{name}.ipk.hash").open("w+b")
+ hash_file.write(hash_bytes)
+ hash_file.close()
+
+ extract(ipk_file, HOME)
+ temp_dir.cleanup()