Temporary DB Upload. The tags are currently all "unknown" categories
Browse files- .gitattributes +1 -0
- create_db.py +208 -0
- db.py +228 -0
- gelbooru2024-02.db +3 -0
.gitattributes
CHANGED
|
@@ -53,3 +53,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 54 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 55 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 54 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 55 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
| 56 |
+
gelbooru2024-02.db filter=lfs diff=lfs merge=lfs -text
|
create_db.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from db import *
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from tqdm import tqdm
|
| 5 |
+
from functools import cache
|
| 6 |
+
import tarfile
|
| 7 |
+
|
| 8 |
+
Tag = None
|
| 9 |
+
Post = None
|
| 10 |
+
PostTagRelation = None
|
| 11 |
+
LocalPost = None
|
| 12 |
+
tags = None
|
| 13 |
+
get_tag_by_id = None
|
| 14 |
+
db = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@cache
|
| 18 |
+
def get_or_create_tag(tag_name: str, tag_type: str, optional_tag_id: int = None):
|
| 19 |
+
"""
|
| 20 |
+
Get a tag if it exists, otherwise create it.
|
| 21 |
+
"""
|
| 22 |
+
tag = Tag.get_or_none(Tag.name == tag_name)
|
| 23 |
+
if tag is None:
|
| 24 |
+
if optional_tag_id is not None:
|
| 25 |
+
assert isinstance(optional_tag_id, int), f"optional_tag_id must be an integer, not {type(optional_tag_id)}"
|
| 26 |
+
tag = Tag.create(id=optional_tag_id, name=tag_name, type=tag_type, popularity=0)
|
| 27 |
+
else:
|
| 28 |
+
tag = Tag.create(name=tag_name, type=tag_type, popularity=0)
|
| 29 |
+
return tag
|
| 30 |
+
|
| 31 |
+
def create_tag_or_use(tag_name: str, tag_type: str, optional_tag_id: int = None):
|
| 32 |
+
"""
|
| 33 |
+
Create a tag if it does not exist, otherwise return the existing tag.
|
| 34 |
+
This function also increments the popularity of the tag.
|
| 35 |
+
"""
|
| 36 |
+
tag = get_or_create_tag(tag_name, tag_type, optional_tag_id)
|
| 37 |
+
# we don't have to check tag type since its unique
|
| 38 |
+
tag.popularity = 1
|
| 39 |
+
return tag
|
| 40 |
+
|
| 41 |
+
def iterate_jsonl(file_path: str):
|
| 42 |
+
"""
|
| 43 |
+
Iterate through a jsonl file and yield each line as a dictionary.
|
| 44 |
+
"""
|
| 45 |
+
if isinstance(file_path, str) and os.path.exists(file_path):
|
| 46 |
+
with open(file_path, "r") as f:
|
| 47 |
+
for line in f:
|
| 48 |
+
yield json.loads(line)
|
| 49 |
+
elif hasattr(file_path, "read"):
|
| 50 |
+
for line in file_path:
|
| 51 |
+
yield json.loads(line)
|
| 52 |
+
else:
|
| 53 |
+
raise ValueError("file_path must be a string or a file-like object")
|
| 54 |
+
JSONL_RATING_CONVERSION = {
|
| 55 |
+
"q": "questionable",
|
| 56 |
+
"s": "sensitive",
|
| 57 |
+
"e": "explicit",
|
| 58 |
+
"g": "general",
|
| 59 |
+
"safe" : "general",
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
def create_tags(tag_string:str, tag_type:str):
|
| 63 |
+
"""
|
| 64 |
+
Create tags from a tag string.
|
| 65 |
+
"""
|
| 66 |
+
for tag in tag_string.split(" "):
|
| 67 |
+
if not tag or tag.isspace():
|
| 68 |
+
continue
|
| 69 |
+
tag = create_tag_or_use(tag, tag_type)
|
| 70 |
+
yield tag
|
| 71 |
+
# 'id', 'created_at', 'score', 'width', 'height', 'md5', 'directory', 'image', 'rating', 'source', 'change', 'owner', 'creator_id', 'parent_id', 'sample', 'preview_height', 'preview_width', 'tags', 'title', 'has_notes', 'has_comments', 'file_url', 'preview_url', 'sample_url', 'sample_height', 'sample_width', 'status', 'post_locked', 'has_children'
|
| 72 |
+
|
| 73 |
+
def get_conversion_key(data, key: str):
|
| 74 |
+
"""
|
| 75 |
+
Get the conversion key for a key.
|
| 76 |
+
"""
|
| 77 |
+
access_key = DANBOORU_KEYS_TO_GELBOORU.get(key, key)
|
| 78 |
+
if access_key == "rating":
|
| 79 |
+
return JSONL_RATING_CONVERSION.get(data.get(access_key, None), data.get(access_key, None))
|
| 80 |
+
return data.get(access_key, None)
|
| 81 |
+
|
| 82 |
+
def create_post(json_data, policy="ignore"):
|
| 83 |
+
"""
|
| 84 |
+
Create a post from a json dictionary.
|
| 85 |
+
Policy can be 'ignore' or 'replace'
|
| 86 |
+
Note that file_url, large_file_url, and preview_file_url are optional.
|
| 87 |
+
"""
|
| 88 |
+
assert "id" in json_data, "id is not in json_data"
|
| 89 |
+
post_id = json_data["id"]
|
| 90 |
+
all_tags = []
|
| 91 |
+
all_tags += [create_tags(json_data.get("tag_string_general", ""), "general")]
|
| 92 |
+
all_tags += [create_tags(json_data.get("tag_string_artist", ""), "artist")]
|
| 93 |
+
all_tags += [create_tags(json_data.get("tag_string_character", ""), "character")]
|
| 94 |
+
all_tags += [create_tags(json_data.get("tag_string_copyright", ""), "copyright")]
|
| 95 |
+
all_tags += [create_tags(json_data.get("tag_string_meta", ""), "meta")]
|
| 96 |
+
# tags -> unknown
|
| 97 |
+
all_tags += [create_tags(json_data.get("tags", ""), "unknown")]
|
| 98 |
+
if Post.get_or_none(Post.id == post_id) is not None:
|
| 99 |
+
if policy == "ignore":
|
| 100 |
+
print(f"Post {post_id} already exists")
|
| 101 |
+
return
|
| 102 |
+
elif policy == "replace":
|
| 103 |
+
Post.delete_by_id(post_id)
|
| 104 |
+
else:
|
| 105 |
+
raise ValueError(f"Unknown policy {policy}, must be 'ignore' or 'replace'")
|
| 106 |
+
post = Post.create(
|
| 107 |
+
**{key: get_conversion_key(json_data, key) for key in[
|
| 108 |
+
"id", "created_at", "uploader_id", "source", "md5", "parent_id", "has_children", "is_deleted", "is_banned", "pixiv_id", "has_active_children", "bit_flags", "has_large", "has_visible_children", "image_width", "image_height", "file_size", "file_ext", "rating", "score", "up_score", "down_score", "fav_count", "file_url", "large_file_url", "preview_file_url"
|
| 109 |
+
]
|
| 110 |
+
}
|
| 111 |
+
)
|
| 112 |
+
for tags in all_tags:
|
| 113 |
+
for tag in tags:
|
| 114 |
+
PostTagRelation.create(post=post, tag=tag)
|
| 115 |
+
return post
|
| 116 |
+
|
| 117 |
+
def read_and_create_posts(file_path: str, policy="ignore"):
|
| 118 |
+
"""
|
| 119 |
+
Read a jsonl file and create the posts in the database.
|
| 120 |
+
Policy can be 'ignore' or 'replace'
|
| 121 |
+
"""
|
| 122 |
+
for json_data in iterate_jsonl(file_path):
|
| 123 |
+
create_post(json_data, policy)
|
| 124 |
+
#print(f"Created post {json_data['id']}")
|
| 125 |
+
|
| 126 |
+
def create_db_from_folder(folder_path: str, policy="ignore"):
|
| 127 |
+
"""
|
| 128 |
+
Create a database from a folder of jsonl files.
|
| 129 |
+
This recursively searches the folder for jsonl files.
|
| 130 |
+
Policy can be 'ignore' or 'replace'
|
| 131 |
+
"""
|
| 132 |
+
global db
|
| 133 |
+
assert db is not None, "Database is not loaded"
|
| 134 |
+
all_jsonl_files = []
|
| 135 |
+
for root, dirs, files in os.walk(folder_path):
|
| 136 |
+
for file in files:
|
| 137 |
+
if file.endswith(".jsonl"):
|
| 138 |
+
all_jsonl_files.append(os.path.join(root, file))
|
| 139 |
+
with db.atomic():
|
| 140 |
+
for file in tqdm(all_jsonl_files):
|
| 141 |
+
read_and_create_posts(file, policy)
|
| 142 |
+
|
| 143 |
+
def create_db_from_tarfile(tarfile_path: str, policy="ignore", read_method="r:gz"):
|
| 144 |
+
"""
|
| 145 |
+
Create a database from a tarfile of jsonl files.
|
| 146 |
+
Policy can be 'ignore' or 'replace'
|
| 147 |
+
"""
|
| 148 |
+
global db
|
| 149 |
+
assert db is not None, "Database is not loaded"
|
| 150 |
+
all_jsonl_files = []
|
| 151 |
+
with tarfile.open(tarfile_path, read_method) as tar:
|
| 152 |
+
for tarinfo in tar:
|
| 153 |
+
if tarinfo.isfile() and tarinfo.name.endswith(".jsonl"):
|
| 154 |
+
all_jsonl_files.append(tarinfo)
|
| 155 |
+
with db.atomic():
|
| 156 |
+
for tarinfo in tqdm(all_jsonl_files):
|
| 157 |
+
with tar.extractfile(tarinfo) as f:
|
| 158 |
+
for json_data in iterate_jsonl(f):
|
| 159 |
+
create_post(json_data, policy)
|
| 160 |
+
#print(f"Created post {json_data['id']}")
|
| 161 |
+
|
| 162 |
+
def sanity_check(order="random"):
|
| 163 |
+
"""
|
| 164 |
+
Print out a random post and its informations
|
| 165 |
+
"""
|
| 166 |
+
if order == "random":
|
| 167 |
+
random_post = Post.select().order_by(fn.Random()).limit(1).get()
|
| 168 |
+
else:
|
| 169 |
+
random_post = Post.select().limit(1).get()
|
| 170 |
+
print(f"Post id : {random_post.id}")
|
| 171 |
+
print(f"Post tags: {random_post.tag_list}, {len(random_post.tag_list)} tags, {random_post.tag_count} tags")
|
| 172 |
+
print(f"Post general tags: {random_post.tag_list_general}, {len(random_post.tag_list_general)} tags, {random_post.tag_count_general} tags")
|
| 173 |
+
print(f"Post artist tags: {random_post.tag_list_artist}, {len(random_post.tag_list_artist)} tags, {random_post.tag_count_artist} tags")
|
| 174 |
+
print(f"Post character tags: {random_post.tag_list_character}, {len(random_post.tag_list_character)} tags, {random_post.tag_count_character} tags")
|
| 175 |
+
print(f"Post copyright tags: {random_post.tag_list_copyright}, {len(random_post.tag_list_copyright)} tags, {random_post.tag_count_copyright} tags")
|
| 176 |
+
print(f"Post meta tags: {random_post.tag_list_meta}, {len(random_post.tag_list_meta)} tags, {random_post.tag_count_meta} tags")
|
| 177 |
+
print(f"Post rating: {random_post.rating}")
|
| 178 |
+
print(f"Post score: {random_post.score}")
|
| 179 |
+
print(f"Post fav_count: {random_post.fav_count}")
|
| 180 |
+
print(f"Post source: {random_post.source}")
|
| 181 |
+
print(f"Post created_at: {random_post.created_at}")
|
| 182 |
+
print(f"Post file_url: {random_post.file_url}")
|
| 183 |
+
print(f"Post large_file_url: {random_post.large_file_url}")
|
| 184 |
+
print(f"Post preview_file_url: {random_post.preview_file_url}")
|
| 185 |
+
print(f"Post image_width: {random_post.image_width}")
|
| 186 |
+
print(f"Post image_height: {random_post.image_height}")
|
| 187 |
+
print(f"Post file_size: {random_post.file_size}")
|
| 188 |
+
print(f"Post file_ext: {random_post.file_ext}")
|
| 189 |
+
print(f"Post uploader_id: {random_post.uploader_id}")
|
| 190 |
+
print(f"Post pixiv_id: {random_post.pixiv_id}")
|
| 191 |
+
print(f"Post has_children: {random_post.has_children}")
|
| 192 |
+
print(f"Post is_deleted: {random_post.is_deleted}")
|
| 193 |
+
print(f"Post is_banned: {random_post.is_banned}")
|
| 194 |
+
print(f"Post has_active_children: {random_post.has_active_children}")
|
| 195 |
+
print(f"Post has_large: {random_post.has_large}")
|
| 196 |
+
print(f"Post has_visible_children: {random_post.has_visible_children}")
|
| 197 |
+
print(f"Post bit_flags: {random_post.bit_flags}")
|
| 198 |
+
|
| 199 |
+
if __name__ == "__main__":
|
| 200 |
+
db_dict = load_db("gelbooru2024-02.db")
|
| 201 |
+
Post, Tag, PostTagRelation = db_dict["Post"], db_dict["Tag"], db_dict["PostTagRelation"]
|
| 202 |
+
db = db_dict["db"]
|
| 203 |
+
LocalPost = db_dict["LocalPost"]
|
| 204 |
+
|
| 205 |
+
#read_and_create_posts(r"C:\sqlite\0_99.jsonl")
|
| 206 |
+
#create_db_from_folder(r'D:\danbooru-0319') # if you have a folder of jsonl files
|
| 207 |
+
for i in range(2,10):
|
| 208 |
+
create_db_from_tarfile(rf"G:\gelboorupost\{i}M.tar.gz")
|
db.py
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from peewee import *
|
| 2 |
+
import tarfile
|
| 3 |
+
import jsonlines
|
| 4 |
+
import json
|
| 5 |
+
import sqlite3
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
GELBOORU_KEYS_TO_DANBOORU = {
|
| 9 |
+
"creator_id": "uploader_id",
|
| 10 |
+
"width": "image_width",
|
| 11 |
+
"has_children": "has_active_children",
|
| 12 |
+
"file_url": "large_file_url",
|
| 13 |
+
"preview_url": "file_url",
|
| 14 |
+
"sample_url": "preview_file_url",
|
| 15 |
+
"width": "image_width",
|
| 16 |
+
"height": "image_height",
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
DANBOORU_KEYS_TO_GELBOORU = {value: key for key, value in GELBOORU_KEYS_TO_DANBOORU.items()}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def load_db(db_file: str):
|
| 23 |
+
"""
|
| 24 |
+
Return a dictionary with the database objects.
|
| 25 |
+
This allows multiple databases to be loaded in one program.
|
| 26 |
+
"""
|
| 27 |
+
tag_cache_map = {}
|
| 28 |
+
class BaseModel(Model):
|
| 29 |
+
class Meta:
|
| 30 |
+
database = SqliteDatabase(db_file)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class EnumField(IntegerField):
|
| 34 |
+
def __init__(self, enum_list, *args, **kwargs):
|
| 35 |
+
super().__init__(*args, **kwargs)
|
| 36 |
+
self.enum_list = enum_list
|
| 37 |
+
self.enum_map = {value: index for index, value in enumerate(enum_list)}
|
| 38 |
+
|
| 39 |
+
def db_value(self, value):
|
| 40 |
+
if isinstance(value, str):
|
| 41 |
+
return self.enum_map[value]
|
| 42 |
+
assert isinstance(value, int)
|
| 43 |
+
return value
|
| 44 |
+
|
| 45 |
+
def python_value(self, value):
|
| 46 |
+
if value is not None:
|
| 47 |
+
return self.enum_list[value]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# 'id', 'created_at', 'score', 'width', 'height', 'md5', 'directory', 'image', 'rating', 'source', 'change', 'owner', 'creator_id', 'parent_id', 'sample', 'preview_height', 'preview_width', 'tags', 'title', 'has_notes', 'has_comments', 'file_url', 'preview_url', 'sample_url', 'sample_height', 'sample_width', 'status', 'post_locked', 'has_children'
|
| 51 |
+
|
| 52 |
+
class Post(BaseModel):
|
| 53 |
+
# "id", "created_at", "uploader_id", "source", "md5", "parent_id", "has_children", "is_deleted", "is_banned", "pixiv_id", "has_active_children", "bit_flags", "has_large", "has_visible_children", "image_width", "image_height", "file_size", "file_ext", "rating", "score", "up_score", "down_score", "fav_count", "file_url", "large_file_url", "preview_file_url"
|
| 54 |
+
id = IntegerField(primary_key=True)
|
| 55 |
+
created_at = CharField()
|
| 56 |
+
uploader_id = IntegerField() # creator_id in gelbooru
|
| 57 |
+
source = CharField()
|
| 58 |
+
md5 = CharField(null=True)
|
| 59 |
+
parent_id = IntegerField(null=True)
|
| 60 |
+
has_children = BooleanField()
|
| 61 |
+
is_deleted = BooleanField(default=False, null=True)
|
| 62 |
+
is_banned = BooleanField(default=False, null=True)
|
| 63 |
+
pixiv_id = IntegerField(null=True)
|
| 64 |
+
has_active_children = BooleanField(default=False, null=True) # has_children in gelbooru
|
| 65 |
+
bit_flags = IntegerField(default=0, null=True)
|
| 66 |
+
has_large = BooleanField(default=False, null=True)
|
| 67 |
+
has_visible_children = BooleanField(default=False, null=True)
|
| 68 |
+
|
| 69 |
+
image_width = IntegerField() # width in gelbooru
|
| 70 |
+
image_height = IntegerField() # height in gelbooru
|
| 71 |
+
file_size = IntegerField(default=0, null=True)
|
| 72 |
+
file_ext = CharField(default="jpg", null=True)
|
| 73 |
+
|
| 74 |
+
rating = EnumField(["general", "sensitive", "questionable", "explicit"])
|
| 75 |
+
score = IntegerField()
|
| 76 |
+
up_score = IntegerField(default=0, null=True)
|
| 77 |
+
down_score = IntegerField(default=0, null=True)
|
| 78 |
+
fav_count = IntegerField(default=0, null=True)
|
| 79 |
+
|
| 80 |
+
file_url = CharField(null=True) # preview_url in gelbooru
|
| 81 |
+
large_file_url = CharField(null=True) # sample_url in gelbooru
|
| 82 |
+
preview_file_url = CharField(null=True) # file_url in gelbooru
|
| 83 |
+
|
| 84 |
+
_tags: ManyToManyField = None # set by tags.bind
|
| 85 |
+
_tags_cache = None
|
| 86 |
+
|
| 87 |
+
@property
|
| 88 |
+
def tag_count(self):
|
| 89 |
+
return len(self.tag_list) if self.tag_list else 0
|
| 90 |
+
|
| 91 |
+
@property
|
| 92 |
+
def tag_count_general(self):
|
| 93 |
+
return len(self.tag_list_general) if self.tag_list else 0
|
| 94 |
+
|
| 95 |
+
@property
|
| 96 |
+
def tag_count_artist(self):
|
| 97 |
+
return len(self.tag_list_artist) if self.tag_list else 0
|
| 98 |
+
|
| 99 |
+
@property
|
| 100 |
+
def tag_count_character(self):
|
| 101 |
+
return len(self.tag_list_character) if self.tag_list else 0
|
| 102 |
+
|
| 103 |
+
@property
|
| 104 |
+
def tag_count_copyright(self):
|
| 105 |
+
return len(self.tag_list_copyright) if self.tag_list else 0
|
| 106 |
+
|
| 107 |
+
@property
|
| 108 |
+
def tag_count_meta(self):
|
| 109 |
+
return len(self.tag_list_meta) if self.tag_list else 0
|
| 110 |
+
|
| 111 |
+
@property
|
| 112 |
+
def tag_list(self):
|
| 113 |
+
if self._tags_cache is None:
|
| 114 |
+
self._tags_cache = list(self._tags)
|
| 115 |
+
return self._tags_cache
|
| 116 |
+
|
| 117 |
+
@property
|
| 118 |
+
def tag_list_general(self):
|
| 119 |
+
return [tag for tag in self.tag_list if tag.type == "general"]
|
| 120 |
+
|
| 121 |
+
@property
|
| 122 |
+
def tag_list_artist(self):
|
| 123 |
+
return [tag for tag in self.tag_list if tag.type == "artist"]
|
| 124 |
+
|
| 125 |
+
@property
|
| 126 |
+
def tag_list_character(self):
|
| 127 |
+
return [tag for tag in self.tag_list if tag.type == "character"]
|
| 128 |
+
|
| 129 |
+
@property
|
| 130 |
+
def tag_list_copyright(self):
|
| 131 |
+
return [tag for tag in self.tag_list if tag.type == "copyright"]
|
| 132 |
+
|
| 133 |
+
@property
|
| 134 |
+
def tag_list_meta(self):
|
| 135 |
+
return [tag for tag in self.tag_list if tag.type == "meta"]
|
| 136 |
+
|
| 137 |
+
@property
|
| 138 |
+
def tag_list_unknown(self):
|
| 139 |
+
return [tag for tag in self.tag_list if tag.type == "unknown"]
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
class Tag(BaseModel):
|
| 143 |
+
id = IntegerField(primary_key=True)
|
| 144 |
+
name = CharField(unique=True)
|
| 145 |
+
type = EnumField(["general", "artist", "character", "copyright", "meta", "unknown"]) # unknown is for gelbooru unbased tags, should be fixed in future
|
| 146 |
+
popularity = IntegerField()
|
| 147 |
+
_posts: ManyToManyField = None
|
| 148 |
+
_posts_cache = None
|
| 149 |
+
|
| 150 |
+
@property
|
| 151 |
+
def posts(self):
|
| 152 |
+
if self._posts_cache is None:
|
| 153 |
+
self._posts_cache = list(self._posts)
|
| 154 |
+
return self._posts_cache
|
| 155 |
+
|
| 156 |
+
def __str__(self):
|
| 157 |
+
return f"<Tag '{self.name}'>"
|
| 158 |
+
|
| 159 |
+
def __repr__(self):
|
| 160 |
+
return f"<Tag|#{self.id}|{self.name}|{self.type[:2]}>"
|
| 161 |
+
|
| 162 |
+
def get_tag_by_id(tag_id):
|
| 163 |
+
if tag_id not in tag_cache_map:
|
| 164 |
+
tag_cache_map[tag_id] = Tag.get_by_id(tag_id)
|
| 165 |
+
return tag_cache_map[tag_id]
|
| 166 |
+
|
| 167 |
+
class PostTagRelation(BaseModel):
|
| 168 |
+
post = ForeignKeyField(Post, backref="post_tags")
|
| 169 |
+
tag = ForeignKeyField(Tag, backref="tag_posts")
|
| 170 |
+
|
| 171 |
+
class LocalPost(BaseModel):
|
| 172 |
+
id = IntegerField(primary_key=True)
|
| 173 |
+
filepath = CharField(null=True)
|
| 174 |
+
latentpath = CharField(null=True)
|
| 175 |
+
post = ForeignKeyField(Post, backref="localpost")
|
| 176 |
+
|
| 177 |
+
def __str__(self):
|
| 178 |
+
return f"<LocalPost '{self.filepath}'>"
|
| 179 |
+
|
| 180 |
+
def __repr__(self):
|
| 181 |
+
return f"<LocalPost|#{self.id}|{self.filepath}|{self.latentpath}|{self.post}>"
|
| 182 |
+
|
| 183 |
+
tags = ManyToManyField(Tag, backref="_posts", through_model=PostTagRelation)
|
| 184 |
+
tags.bind(Post, "_tags", set_attribute=True)
|
| 185 |
+
file_exists = os.path.exists(db_file)
|
| 186 |
+
db = SqliteDatabase(db_file)
|
| 187 |
+
Post._meta.database = db
|
| 188 |
+
Tag._meta.database = db
|
| 189 |
+
PostTagRelation._meta.database = db
|
| 190 |
+
LocalPost._meta.database = db
|
| 191 |
+
db.connect()
|
| 192 |
+
print("Database connected.")
|
| 193 |
+
if not file_exists:
|
| 194 |
+
db.create_tables([Post, Tag, PostTagRelation])
|
| 195 |
+
db.create_tables([LocalPost])
|
| 196 |
+
db.commit()
|
| 197 |
+
print("Database initialized.")
|
| 198 |
+
assert db is not None, "Database is not loaded"
|
| 199 |
+
return {
|
| 200 |
+
"Post": Post,
|
| 201 |
+
"Tag": Tag,
|
| 202 |
+
"PostTagRelation": PostTagRelation,
|
| 203 |
+
"LocalPost": LocalPost,
|
| 204 |
+
"tags": tags,
|
| 205 |
+
"get_tag_by_id": get_tag_by_id,
|
| 206 |
+
"db": db,
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
if __name__ == "__main__":
|
| 210 |
+
test_tarfile = r'G:\gelboorupost\0M.tar.gz'
|
| 211 |
+
# get first jsonl file from tarfile
|
| 212 |
+
with tarfile.open(test_tarfile, 'r:gz') as tar:
|
| 213 |
+
while True:
|
| 214 |
+
member = tar.next()
|
| 215 |
+
if member is None:
|
| 216 |
+
break
|
| 217 |
+
if member.isfile():
|
| 218 |
+
if member.name.endswith('.jsonl'):
|
| 219 |
+
f = tar.extractfile(member)
|
| 220 |
+
json_data = jsonlines.Reader(f)
|
| 221 |
+
for line in json_data:
|
| 222 |
+
# get keys
|
| 223 |
+
print(line.keys())
|
| 224 |
+
print(line.values())
|
| 225 |
+
break
|
| 226 |
+
break
|
| 227 |
+
else:
|
| 228 |
+
print('not a file')
|
gelbooru2024-02.db
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4f2ae54e29175dfc64d9cfa277498c1a79430311b12403b4d28998573cabdd65
|
| 3 |
+
size 14257557504
|