Upload 3 files
Browse files- danbooru2023.db +2 -2
- db.py +27 -2
- db_utils.py +19 -0
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:7e3f2c1ac2eb698c83f76d14f8b1bde575f5c70fa91f23e4884f2984aafe2d49
|
3 |
+
size 10714025984
|
db.py
CHANGED
@@ -3,6 +3,7 @@ import sqlite3
|
|
3 |
|
4 |
from peewee import *
|
5 |
from playhouse.sqlite_ext import FTS5Model, SearchField
|
|
|
6 |
|
7 |
|
8 |
class MemoryConnection(sqlite3.Connection):
|
@@ -62,6 +63,23 @@ class TagListField(TextField, SearchField):
|
|
62 |
]
|
63 |
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
class BaseModel(Model):
|
66 |
class Meta:
|
67 |
database = db
|
@@ -88,7 +106,7 @@ class Post(BaseModel):
|
|
88 |
file_size = IntegerField()
|
89 |
file_ext = CharField()
|
90 |
|
91 |
-
rating =
|
92 |
score = IntegerField()
|
93 |
up_score = IntegerField()
|
94 |
down_score = IntegerField()
|
@@ -128,13 +146,20 @@ class PostFTS(FTS5Model):
|
|
128 |
class Tag(BaseModel):
|
129 |
id = IntegerField(primary_key=True)
|
130 |
name = CharField(unique=True)
|
131 |
-
type =
|
132 |
popularity = IntegerField()
|
133 |
|
|
|
|
|
|
|
134 |
|
135 |
if __name__ == "__main__":
|
|
|
|
|
136 |
db.connect()
|
137 |
for index in db.get_indexes("post"):
|
138 |
print(index)
|
|
|
|
|
139 |
# db.create_tables([Post, Tag])
|
140 |
# db.save()
|
|
|
3 |
|
4 |
from peewee import *
|
5 |
from playhouse.sqlite_ext import FTS5Model, SearchField
|
6 |
+
from playhouse.shortcuts import model_to_dict
|
7 |
|
8 |
|
9 |
class MemoryConnection(sqlite3.Connection):
|
|
|
63 |
]
|
64 |
|
65 |
|
66 |
+
class EnumField(IntegerField):
|
67 |
+
def __init__(self, enum_list, *args, **kwargs):
|
68 |
+
super().__init__(*args, **kwargs)
|
69 |
+
self.enum_list = enum_list
|
70 |
+
self.enum_map = {value: index for index, value in enumerate(enum_list)}
|
71 |
+
|
72 |
+
def db_value(self, value):
|
73 |
+
if isinstance(value, str):
|
74 |
+
return self.enum_map[value]
|
75 |
+
assert isinstance(value, int)
|
76 |
+
return value
|
77 |
+
|
78 |
+
def python_value(self, value):
|
79 |
+
if value is not None:
|
80 |
+
return self.enum_list[value]
|
81 |
+
|
82 |
+
|
83 |
class BaseModel(Model):
|
84 |
class Meta:
|
85 |
database = db
|
|
|
106 |
file_size = IntegerField()
|
107 |
file_ext = CharField()
|
108 |
|
109 |
+
rating = EnumField(["general", "sensitive", "questionable", "explicit"])
|
110 |
score = IntegerField()
|
111 |
up_score = IntegerField()
|
112 |
down_score = IntegerField()
|
|
|
146 |
class Tag(BaseModel):
|
147 |
id = IntegerField(primary_key=True)
|
148 |
name = CharField(unique=True)
|
149 |
+
type = EnumField(["general", "artist", "character", "copyright", "meta"])
|
150 |
popularity = IntegerField()
|
151 |
|
152 |
+
def __str__(self):
|
153 |
+
return f"<Tag: {self.name}>"
|
154 |
+
|
155 |
|
156 |
if __name__ == "__main__":
|
157 |
+
from objprint import objprint
|
158 |
+
|
159 |
db.connect()
|
160 |
for index in db.get_indexes("post"):
|
161 |
print(index)
|
162 |
+
post = Post.select().first()
|
163 |
+
objprint(model_to_dict(post))
|
164 |
# db.create_tables([Post, Tag])
|
165 |
# db.save()
|
db_utils.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from numpy import base_repr
|
2 |
+
from db import *
|
3 |
+
|
4 |
+
|
5 |
+
def get_post_by_tag(tag: Tag):
|
6 |
+
id_str = f"${base_repr(tag.id, 36)}#"
|
7 |
+
return (
|
8 |
+
Post.select()
|
9 |
+
.join(PostFTS, on=(Post.id == PostFTS.rowid))
|
10 |
+
.where(PostFTS.tag_list.contains(id_str))
|
11 |
+
)
|
12 |
+
|
13 |
+
|
14 |
+
def get_tag_by_name(name: str):
|
15 |
+
return Tag.get(name=name)
|
16 |
+
|
17 |
+
|
18 |
+
def get_tags_by_popularity(n: int):
|
19 |
+
return Tag.select().where(Tag.popularity >= n)
|