Skip to content

Commit 3b96b9e

Browse files
committed
feat(api): expose download percentage
1 parent 96b9e91 commit 3b96b9e

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ The creation API will return the task object:
5656
"largest_file": null,
5757
"largest_file_size": null,
5858
"is_downloading": False,
59+
"sample_download_percent": 0.0,
60+
"sample_downloaded_files": 0,
61+
"download_percent": 0.0,
62+
"downloaded_size": 0,
5963
"current_progessing_stage": "waiting_assign",
6064
"done": false,
6165
"failed": false,

pytest.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[pytest]
22
DJANGO_SETTINGS_MODULE = baidupcsleecher.settings
33
python_files = tests.py test_*.py *_tests.py
4-
addopts = -ra --doctest-modules --last-failed --durations=3 --cov --cov-report term-missing --no-cov-on-fail --disable-socket
4+
addopts = -ra --doctest-modules --last-failed --durations=3 --cov --cov-report term-missing --no-cov-on-fail --disable-socket -vv

task/models.py

+29-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,11 @@ def list_remote_files(self, files_only=True):
152152
def remote_files(self):
153153
return self.list_remote_files(files_only=True)
154154

155-
def list_local_files(self):
156-
data_path = self.data_path
155+
def list_local_files(self, samples_only=False):
156+
if samples_only:
157+
data_path = self.sample_data_path
158+
else:
159+
data_path = self.data_path
157160
for root, dirs, files in walk(data_path):
158161
for file in files:
159162
filepath = join(root, file)
@@ -164,6 +167,10 @@ def list_local_files(self):
164167
def local_files(self):
165168
return list(self.list_local_files())
166169

170+
@property
171+
def local_sample_files(self) -> list:
172+
return list(self.list_local_files(samples_only=True))
173+
167174
@property
168175
def total_files(self):
169176
return len([f for f in self.load_files() if f["is_file"]])
@@ -380,3 +387,23 @@ def delete_files(self):
380387
def erase(self):
381388
self.delete_files()
382389
self.delete()
390+
391+
@property
392+
def sample_downloaded_files(self) -> int:
393+
return len(list(self.list_local_files()))
394+
395+
@property
396+
def sample_download_percent(self) -> float:
397+
if self.total_files == 0:
398+
return 0.0
399+
return 100.0 * self.sample_downloaded_files / self.total_files
400+
401+
@property
402+
def downloaded_size(self) -> int:
403+
return self.local_size
404+
405+
@property
406+
def download_percent(self) -> float:
407+
if self.total_files == 0:
408+
return 0.0
409+
return 100.0 * self.downloaded_size / self.total_size

task/serializers.py

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class Meta:
3030
"largest_file",
3131
"largest_file_size",
3232
"is_downloading",
33+
"sample_download_percent",
34+
"sample_downloaded_files",
35+
"download_percent",
36+
"downloaded_size",
3337
"current_progressing_stage",
3438
"done",
3539
"failed",

task/tests/test_api.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
def touch_file(path: Path):
1717
path.parent.mkdir(parents=True, exist_ok=True)
18-
open(path, "w").write("")
18+
open(path, "w").write("hello" * 1024)
1919

2020

2121
def touch_task_files(task: Task):
@@ -72,6 +72,10 @@ def test_create_task(self):
7272
assert data["full_download_now"] is False
7373
assert data["current_progressing_stage"] == "waiting_assign"
7474
assert data["is_downloading"] is False
75+
assert data["download_percent"] == 0
76+
assert data["sample_download_percent"] == 0
77+
assert data["downloaded_size"] == 0
78+
assert data["sample_downloaded_files"] == 0
7579
assert set(data.keys()) == {
7680
"callback",
7781
"captcha_required",
@@ -80,6 +84,8 @@ def test_create_task(self):
8084
"created_at",
8185
"current_progressing_stage",
8286
"done",
87+
"download_percent",
88+
"downloaded_size",
8389
"failed",
8490
"file_listed_at",
8591
"finished_at",
@@ -93,7 +99,9 @@ def test_create_task(self):
9399
"path",
94100
"recoverable",
95101
"retry_times",
102+
"sample_download_percent",
96103
"sample_downloaded_at",
104+
"sample_downloaded_files",
97105
"sample_path",
98106
"shared_id",
99107
"shared_link",
@@ -365,3 +373,16 @@ def test_purge_nothing(self):
365373

366374
assert response.json() == {"done": True}
367375
assert sorted(list_files(settings.DATA_DIR)) == files
376+
377+
def test_download_percent(self):
378+
touch_task_files(self.task)
379+
380+
url = reverse("task-detail", args=[self.task.id])
381+
382+
response = self.client.get(url)
383+
384+
data = response.json()
385+
assert f'{data["download_percent"]:0.3f}' == "0.095"
386+
assert f'{data["sample_download_percent"]:0.2f}' == "100.00"
387+
assert data["downloaded_size"] == 10240
388+
assert data["sample_downloaded_files"] == 2

0 commit comments

Comments
 (0)