feat(download): add progress bar
Browse files- .gitignore +2 -1
- .vscode/settings.json +9 -0
- download_images.py +19 -6
.gitignore
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
venv
|
2 |
-
__pycache__
|
|
|
|
1 |
venv
|
2 |
+
__pycache__
|
3 |
+
image
|
.vscode/settings.json
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"[python]": {
|
3 |
+
"editor.defaultFormatter": "ms-python.black-formatter",
|
4 |
+
"editor.formatOnSave": false,
|
5 |
+
"editor.codeActionsOnSave": {
|
6 |
+
"source.organizeImports": "explicit"
|
7 |
+
},
|
8 |
+
},
|
9 |
+
}
|
download_images.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
import
|
2 |
import os
|
3 |
-
import threading
|
4 |
import queue
|
5 |
-
import
|
6 |
-
|
7 |
|
8 |
-
import
|
|
|
9 |
|
10 |
parser = argparse.ArgumentParser()
|
11 |
parser.add_argument("--db_path", type=str, default="danbooru_metadata.db")
|
@@ -19,7 +19,7 @@ OUT_DIR = args.out_dir
|
|
19 |
NUM_THREADS = args.num_threads
|
20 |
|
21 |
# Task queue to hold file URLs and IDs
|
22 |
-
task_queue = queue.Queue()
|
23 |
|
24 |
IMAGE_EXTENSIONS = [
|
25 |
"png",
|
@@ -39,9 +39,22 @@ def fetch_posts_to_queue():
|
|
39 |
"""Fetch posts from SQLite database and put them in the task queue."""
|
40 |
conn = sqlite3.connect(DB_PATH)
|
41 |
cursor = conn.cursor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
print("Fetching posts")
|
43 |
cursor.execute("SELECT id, file_url, file_ext FROM posts")
|
44 |
while True:
|
|
|
45 |
post = cursor.fetchone()
|
46 |
if post is None:
|
47 |
break
|
|
|
1 |
+
import argparse
|
2 |
import os
|
|
|
3 |
import queue
|
4 |
+
import sqlite3
|
5 |
+
import threading
|
6 |
|
7 |
+
import requests
|
8 |
+
from rich.progress import BarColumn, Progress, TimeElapsedColumn, TimeRemainingColumn
|
9 |
|
10 |
parser = argparse.ArgumentParser()
|
11 |
parser.add_argument("--db_path", type=str, default="danbooru_metadata.db")
|
|
|
19 |
NUM_THREADS = args.num_threads
|
20 |
|
21 |
# Task queue to hold file URLs and IDs
|
22 |
+
task_queue = queue.Queue(64)
|
23 |
|
24 |
IMAGE_EXTENSIONS = [
|
25 |
"png",
|
|
|
39 |
"""Fetch posts from SQLite database and put them in the task queue."""
|
40 |
conn = sqlite3.connect(DB_PATH)
|
41 |
cursor = conn.cursor()
|
42 |
+
|
43 |
+
count = cursor.execute("SELECT COUNT(*) FROM posts").fetchone()[0]
|
44 |
+
progress = Progress(
|
45 |
+
"[progress.description]{task.description}",
|
46 |
+
BarColumn(),
|
47 |
+
"[progress.percentage]{task.percentage:>3.0f}%",
|
48 |
+
TimeElapsedColumn(),
|
49 |
+
TimeRemainingColumn(),
|
50 |
+
"[green] [{task.completed} / {task.total}] [/]",
|
51 |
+
)
|
52 |
+
task = progress.add_task("Downloading", total=count)
|
53 |
+
progress.start()
|
54 |
print("Fetching posts")
|
55 |
cursor.execute("SELECT id, file_url, file_ext FROM posts")
|
56 |
while True:
|
57 |
+
progress.update(task, advance=1)
|
58 |
post = cursor.fetchone()
|
59 |
if post is None:
|
60 |
break
|