xjdpj 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
xjdpj-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: xjdpj
3
+ Version: 0.1.0
4
+ Summary: Command-line uploader for XinJiangDaPanJi
5
+ Author-email: Benny <benny.think@gmail.com>
6
+ License-Expression: MIT
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+
10
+ # xjdpj CLI
11
+
12
+ Standard-library command-line uploader for [XinJiangDaPanJi](https://github.com/BennyThink/xjdpj).
13
+
14
+ ```bash
15
+ pip install xjdpj
16
+ xjdpj 1.mp4 --host https://your-server.example --key your-key
17
+ ```
18
+
19
+ After the package is published to PyPI, you can run it without installing it permanently:
20
+
21
+ ```bash
22
+ uvx xjdpj 1.mp4 --host https://your-server.example --key your-key
23
+ ```
24
+
25
+ Before publication, run the CLI from this repository instead:
26
+
27
+ ```bash
28
+ uvx --from ./cli xjdpj 1.mp4 --host https://your-server.example --key your-key
29
+ ```
30
+
31
+ Set `XJDPJ_HOST` and `XJDPJ_KEY` to avoid repeating options (the default host is `http://127.0.0.1:44777`):
32
+
33
+ ```bash
34
+ export XJDPJ_HOST=https://your-server.example
35
+ export XJDPJ_KEY=your-key
36
+ uvx xjdpj 1.mp4
37
+ ```
38
+
39
+ Use `--upload-id ID` to resume an interrupted upload. The command prints the upload ID to stderr and the final download URL to stdout.
xjdpj-0.1.0/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # xjdpj CLI
2
+
3
+ Standard-library command-line uploader for [XinJiangDaPanJi](https://github.com/BennyThink/xjdpj).
4
+
5
+ ```bash
6
+ pip install xjdpj
7
+ xjdpj 1.mp4 --host https://your-server.example --key your-key
8
+ ```
9
+
10
+ After the package is published to PyPI, you can run it without installing it permanently:
11
+
12
+ ```bash
13
+ uvx xjdpj 1.mp4 --host https://your-server.example --key your-key
14
+ ```
15
+
16
+ Before publication, run the CLI from this repository instead:
17
+
18
+ ```bash
19
+ uvx --from ./cli xjdpj 1.mp4 --host https://your-server.example --key your-key
20
+ ```
21
+
22
+ Set `XJDPJ_HOST` and `XJDPJ_KEY` to avoid repeating options (the default host is `http://127.0.0.1:44777`):
23
+
24
+ ```bash
25
+ export XJDPJ_HOST=https://your-server.example
26
+ export XJDPJ_KEY=your-key
27
+ uvx xjdpj 1.mp4
28
+ ```
29
+
30
+ Use `--upload-id ID` to resume an interrupted upload. The command prints the upload ID to stderr and the final download URL to stdout.
@@ -0,0 +1,19 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "xjdpj"
7
+ version = "0.1.0"
8
+ description = "Command-line uploader for XinJiangDaPanJi"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "MIT"
12
+ authors = [{ name = "Benny", email = "benny.think@gmail.com" }]
13
+ dependencies = []
14
+
15
+ [project.scripts]
16
+ xjdpj = "xjdpj.cli:main"
17
+
18
+ [tool.setuptools.packages.find]
19
+ where = ["src"]
xjdpj-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1 @@
1
+ """Command-line client for XinJiangDaPanJi."""
@@ -0,0 +1,93 @@
1
+ """Upload files to a XinJiangDaPanJi server using only the standard library."""
2
+
3
+ import argparse
4
+ import json
5
+ import os
6
+ import sys
7
+ from pathlib import Path
8
+ from urllib.error import HTTPError, URLError
9
+ from urllib.parse import urlsplit
10
+ from urllib.request import Request, urlopen
11
+
12
+
13
+ def request_json(host, key, method, path, data=None, content_type=None):
14
+ headers = {}
15
+ if key:
16
+ headers["X-Upload-Key"] = key
17
+ if content_type:
18
+ headers["Content-Type"] = content_type
19
+ request = Request(host + path, data=data, headers=headers, method=method)
20
+ try:
21
+ with urlopen(request, timeout=3600) as response:
22
+ return json.load(response)
23
+ except HTTPError as error:
24
+ try:
25
+ message = json.load(error).get("message", error.reason)
26
+ except (ValueError, AttributeError):
27
+ message = error.reason
28
+ raise RuntimeError(f"HTTP {error.code}: {message}") from error
29
+ except URLError as error:
30
+ raise RuntimeError(f"Connection failed: {error.reason}") from error
31
+
32
+
33
+ def upload(file, host, key, upload_id=None):
34
+ size = file.stat().st_size
35
+ if not size:
36
+ raise ValueError("Empty files are not supported")
37
+
38
+ if upload_id:
39
+ status = request_json(host, key, "GET", f"/api/uploads/{upload_id}")
40
+ if status["filename"] != file.name or status["size"] != size:
41
+ raise ValueError("File name or size does not match the upload session")
42
+ chunk_size = status["chunk_size"]
43
+ received = set(status["received"])
44
+ else:
45
+ metadata = json.dumps({"filename": file.name, "size": size}).encode("utf-8")
46
+ status = request_json(host, key, "POST", "/api/uploads", metadata, "application/json")
47
+ upload_id = status["upload_id"]
48
+ chunk_size = status["chunk_size"]
49
+ received = set()
50
+ print(f"Upload ID: {upload_id} (use --upload-id to resume)", file=sys.stderr)
51
+
52
+ if not isinstance(chunk_size, int) or chunk_size <= 0 or chunk_size > 8 * 1024 * 1024:
53
+ raise ValueError("Server returned an invalid chunk size")
54
+ total = (size + chunk_size - 1) // chunk_size
55
+ with file.open("rb") as source:
56
+ for index in range(total):
57
+ if index in received:
58
+ continue
59
+ source.seek(index * chunk_size)
60
+ chunk = source.read(min(chunk_size, size - index * chunk_size))
61
+ if not chunk:
62
+ raise RuntimeError("File changed during upload")
63
+ request_json(host, key, "PUT", f"/api/uploads/{upload_id}/chunks/{index}",
64
+ chunk, "application/octet-stream")
65
+ print(f"Uploaded {index + 1}/{total} chunks", file=sys.stderr)
66
+
67
+ result = request_json(host, key, "POST", f"/api/uploads/{upload_id}/complete", b"")
68
+ return result["link"]
69
+
70
+
71
+ def main(argv=None):
72
+ parser = argparse.ArgumentParser(description="Upload a file to XinJiangDaPanJi")
73
+ parser.add_argument("file", type=Path, help="file to upload")
74
+ parser.add_argument("--host", default=os.getenv("XJDPJ_HOST", "http://127.0.0.1:44777"),
75
+ help="server URL (or XJDPJ_HOST)")
76
+ parser.add_argument("--key", default=os.getenv("XJDPJ_KEY", ""),
77
+ help="upload key (or XJDPJ_KEY)")
78
+ parser.add_argument("--upload-id", help="resume a previous upload session")
79
+ args = parser.parse_args(argv)
80
+ host = args.host.rstrip("/")
81
+ parsed = urlsplit(host)
82
+ if parsed.scheme not in ("http", "https") or not parsed.netloc or parsed.path or parsed.query or parsed.fragment:
83
+ parser.error("--host must be an http(s) origin without a path")
84
+ if not args.file.is_file():
85
+ parser.error(f"file does not exist: {args.file}")
86
+ try:
87
+ print(upload(args.file, host, args.key, args.upload_id))
88
+ except (OSError, ValueError, RuntimeError, KeyError) as error:
89
+ parser.exit(1, f"xjdpj: {error}\n")
90
+
91
+
92
+ if __name__ == "__main__":
93
+ main()
@@ -0,0 +1,39 @@
1
+ Metadata-Version: 2.4
2
+ Name: xjdpj
3
+ Version: 0.1.0
4
+ Summary: Command-line uploader for XinJiangDaPanJi
5
+ Author-email: Benny <benny.think@gmail.com>
6
+ License-Expression: MIT
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+
10
+ # xjdpj CLI
11
+
12
+ Standard-library command-line uploader for [XinJiangDaPanJi](https://github.com/BennyThink/xjdpj).
13
+
14
+ ```bash
15
+ pip install xjdpj
16
+ xjdpj 1.mp4 --host https://your-server.example --key your-key
17
+ ```
18
+
19
+ After the package is published to PyPI, you can run it without installing it permanently:
20
+
21
+ ```bash
22
+ uvx xjdpj 1.mp4 --host https://your-server.example --key your-key
23
+ ```
24
+
25
+ Before publication, run the CLI from this repository instead:
26
+
27
+ ```bash
28
+ uvx --from ./cli xjdpj 1.mp4 --host https://your-server.example --key your-key
29
+ ```
30
+
31
+ Set `XJDPJ_HOST` and `XJDPJ_KEY` to avoid repeating options (the default host is `http://127.0.0.1:44777`):
32
+
33
+ ```bash
34
+ export XJDPJ_HOST=https://your-server.example
35
+ export XJDPJ_KEY=your-key
36
+ uvx xjdpj 1.mp4
37
+ ```
38
+
39
+ Use `--upload-id ID` to resume an interrupted upload. The command prints the upload ID to stderr and the final download URL to stdout.
@@ -0,0 +1,9 @@
1
+ README.md
2
+ pyproject.toml
3
+ src/xjdpj/__init__.py
4
+ src/xjdpj/cli.py
5
+ src/xjdpj.egg-info/PKG-INFO
6
+ src/xjdpj.egg-info/SOURCES.txt
7
+ src/xjdpj.egg-info/dependency_links.txt
8
+ src/xjdpj.egg-info/entry_points.txt
9
+ src/xjdpj.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ xjdpj = xjdpj.cli:main
@@ -0,0 +1 @@
1
+ xjdpj