Update db to 7.6M, add update_at attribute
#5
by
AngelBottomless
- opened
- create_db.py +19 -2
- danbooru2023.db +2 -2
- db.py +2 -0
create_db.py
CHANGED
@@ -3,6 +3,7 @@ import json
|
|
3 |
import os
|
4 |
from tqdm import tqdm
|
5 |
from functools import cache
|
|
|
6 |
|
7 |
@cache
|
8 |
def get_or_create_tag(tag_name: str, tag_type: str, optional_tag_id: int = None):
|
@@ -61,6 +62,7 @@ def create_post(json_data, policy="ignore"):
|
|
61 |
"""
|
62 |
assert "id" in json_data, "id is not in json_data"
|
63 |
post_id = json_data["id"]
|
|
|
64 |
all_tags = []
|
65 |
all_tags += [create_tags(json_data["tag_string_general"], "general")]
|
66 |
all_tags += [create_tags(json_data["tag_string_artist"], "artist")]
|
@@ -72,6 +74,18 @@ def create_post(json_data, policy="ignore"):
|
|
72 |
print(f"Post {post_id} already exists")
|
73 |
return
|
74 |
elif policy == "replace":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
Post.delete_by_id(post_id)
|
76 |
else:
|
77 |
raise ValueError(f"Unknown policy {policy}, must be 'ignore' or 'replace'")
|
@@ -88,6 +102,7 @@ def create_post(json_data, policy="ignore"):
|
|
88 |
up_score=json_data["up_score"], down_score=json_data["down_score"], fav_count=json_data["fav_count"],
|
89 |
file_url=json_data.get("file_url", None), large_file_url=json_data.get("large_file_url", None),
|
90 |
preview_file_url=json_data.get("preview_file_url", None),
|
|
|
91 |
#tag_list=json_data["tag_string"]
|
92 |
)
|
93 |
for tags in all_tags:
|
@@ -118,7 +133,7 @@ def create_db_from_folder(folder_path: str, policy="ignore"):
|
|
118 |
if file.endswith(".jsonl"):
|
119 |
all_jsonl_files.append(os.path.join(root, file))
|
120 |
with db.atomic():
|
121 |
-
for file in tqdm(all_jsonl_files):
|
122 |
read_and_create_posts(file, policy)
|
123 |
|
124 |
def sanity_check(order="random"):
|
@@ -157,8 +172,10 @@ def sanity_check(order="random"):
|
|
157 |
print(f"Post has_large: {random_post.has_large}")
|
158 |
print(f"Post has_visible_children: {random_post.has_visible_children}")
|
159 |
print(f"Post bit_flags: {random_post.bit_flags}")
|
|
|
160 |
|
161 |
if __name__ == "__main__":
|
162 |
db = load_db("danbooru2023.db")
|
163 |
#read_and_create_posts(r"C:\sqlite\0_99.jsonl")
|
164 |
-
|
|
|
|
3 |
import os
|
4 |
from tqdm import tqdm
|
5 |
from functools import cache
|
6 |
+
from datetime import datetime
|
7 |
|
8 |
@cache
|
9 |
def get_or_create_tag(tag_name: str, tag_type: str, optional_tag_id: int = None):
|
|
|
62 |
"""
|
63 |
assert "id" in json_data, "id is not in json_data"
|
64 |
post_id = json_data["id"]
|
65 |
+
#"updated_at": "2024-05-19T01:30:25.096-04:00"
|
66 |
all_tags = []
|
67 |
all_tags += [create_tags(json_data["tag_string_general"], "general")]
|
68 |
all_tags += [create_tags(json_data["tag_string_artist"], "artist")]
|
|
|
74 |
print(f"Post {post_id} already exists")
|
75 |
return
|
76 |
elif policy == "replace":
|
77 |
+
# get existing post, compare updated_at, delete if necessary
|
78 |
+
current_post = Post.get_by_id(post_id)
|
79 |
+
if current_post.updated_at == json_data["updated_at"]:
|
80 |
+
print(f"Post {post_id} already exists")
|
81 |
+
return
|
82 |
+
# compare the date
|
83 |
+
datetime_current = datetime.fromisoformat(current_post.updated_at)
|
84 |
+
datetime_new = datetime.fromisoformat(json_data["updated_at"])
|
85 |
+
if datetime_new <= datetime_current:
|
86 |
+
print(f"Post {post_id} is older than the existing post")
|
87 |
+
return
|
88 |
+
print(f"Post {post_id} is being replaced, old updated_at: {current_post.updated_at}, new updated_at: {json_data['updated_at']}")
|
89 |
Post.delete_by_id(post_id)
|
90 |
else:
|
91 |
raise ValueError(f"Unknown policy {policy}, must be 'ignore' or 'replace'")
|
|
|
102 |
up_score=json_data["up_score"], down_score=json_data["down_score"], fav_count=json_data["fav_count"],
|
103 |
file_url=json_data.get("file_url", None), large_file_url=json_data.get("large_file_url", None),
|
104 |
preview_file_url=json_data.get("preview_file_url", None),
|
105 |
+
updated_at=json_data["updated_at"],
|
106 |
#tag_list=json_data["tag_string"]
|
107 |
)
|
108 |
for tags in all_tags:
|
|
|
133 |
if file.endswith(".jsonl"):
|
134 |
all_jsonl_files.append(os.path.join(root, file))
|
135 |
with db.atomic():
|
136 |
+
for file in tqdm(all_jsonl_files[::-1]):
|
137 |
read_and_create_posts(file, policy)
|
138 |
|
139 |
def sanity_check(order="random"):
|
|
|
172 |
print(f"Post has_large: {random_post.has_large}")
|
173 |
print(f"Post has_visible_children: {random_post.has_visible_children}")
|
174 |
print(f"Post bit_flags: {random_post.bit_flags}")
|
175 |
+
print(f"Post updated_at: {random_post.updated_at}")
|
176 |
|
177 |
if __name__ == "__main__":
|
178 |
db = load_db("danbooru2023.db")
|
179 |
#read_and_create_posts(r"C:\sqlite\0_99.jsonl")
|
180 |
+
sanity_check()
|
181 |
+
#create_db_from_folder(r'D:\danbooru\post_danboorus', 'replace') # if you have a folder of jsonl files
|
danbooru2023.db
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d93a99a6137533ef0d028299887dfc08ca05181753a51205adcebd0c284d57cc
|
3 |
+
size 21316800512
|
db.py
CHANGED
@@ -115,6 +115,8 @@ class Post(BaseModel):
|
|
115 |
file_url = CharField(null=True)
|
116 |
large_file_url = CharField(null=True)
|
117 |
preview_file_url = CharField(null=True)
|
|
|
|
|
118 |
|
119 |
_tags: ManyToManyField = None # set by tags.bind
|
120 |
_tags_cache = None
|
|
|
115 |
file_url = CharField(null=True)
|
116 |
large_file_url = CharField(null=True)
|
117 |
preview_file_url = CharField(null=True)
|
118 |
+
|
119 |
+
updated_at = CharField() # "updated_at": "2024-05-19T01:30:25.096-04:00"
|
120 |
|
121 |
_tags: ManyToManyField = None # set by tags.bind
|
122 |
_tags_cache = None
|