File size: 5,087 Bytes
7c6361a 989fb8b 7c6361a 989fb8b 961c20d 7c6361a 0974aba 7c6361a 989fb8b 7c6361a 0974aba 7c6361a 0974aba 7c6361a 0974aba 7c6361a 961c20d 7c6361a 961c20d 7c6361a 989fb8b 7c6361a 961c20d 7c6361a 961c20d deaefe2 7c6361a deaefe2 961c20d 7c6361a 0974aba 961c20d deaefe2 0974aba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import pathlib
import sqlite3
from peewee import *
from playhouse.sqlite_ext import FTS5Model, SearchField
from playhouse.shortcuts import model_to_dict
class MemoryConnection(sqlite3.Connection):
def __init__(self, dbname, *args, **kwargs):
load_conn = sqlite3.connect(dbname)
super(MemoryConnection, self).__init__(":memory:", *args, **kwargs)
load_conn.backup(self)
load_conn.close()
class SqliteMemDatabase(SqliteDatabase):
def __init__(self, database, *args, **kwargs) -> None:
self.dbname = database
super().__init__(database, *args, factory=MemoryConnection, **kwargs)
def reload(self, dbname=None):
if dbname is None:
dbname = self.dbname
load_conn = sqlite3.connect(dbname)
try:
load_conn.backup(self._state.conn)
finally:
load_conn.close()
def save(self, dbname=None):
if dbname is None:
dbname = self.dbname
save_conn = sqlite3.connect(dbname)
try:
self._state.conn.backup(save_conn)
finally:
save_conn.close()
# db = SqliteMemDatabase(pathlib.Path(__file__).parent.resolve() / "danbooru2023.db")
db = SqliteDatabase(pathlib.Path(__file__).parent.resolve() / "danbooru2023.db")
tag_cache_map = {}
def get_tag_by_id(id):
if id not in tag_cache_map:
tag_cache_map[id] = Tag.get_by_id(id)
return tag_cache_map[id]
class TagListField(TextField, SearchField):
def db_value(self, value):
if isinstance(value, str):
return value
assert all(isinstance(tag, (Tag, int)) for tag in value)
return "".join(f"${tag.id if isinstance(tag, Tag) else tag}#" for tag in value)
def python_value(self, value):
if value is not None:
return [
get_tag_by_id(int(tag, 36)) for tag in value[1:-1].split("#$") if tag
]
class EnumField(IntegerField):
def __init__(self, enum_list, *args, **kwargs):
super().__init__(*args, **kwargs)
self.enum_list = enum_list
self.enum_map = {value: index for index, value in enumerate(enum_list)}
def db_value(self, value):
if isinstance(value, str):
return self.enum_map[value]
assert isinstance(value, int)
return value
def python_value(self, value):
if value is not None:
return self.enum_list[value]
class BaseModel(Model):
class Meta:
database = db
class Post(BaseModel):
id = IntegerField(primary_key=True)
created_at = CharField()
uploader_id = IntegerField()
source = CharField()
md5 = CharField(null=True)
parent_id = IntegerField(null=True)
has_children = BooleanField()
is_deleted = BooleanField()
is_banned = BooleanField()
pixiv_id = IntegerField(null=True)
has_active_children = BooleanField()
bit_flags = IntegerField()
has_large = BooleanField()
has_visible_children = BooleanField()
image_width = IntegerField()
image_height = IntegerField()
file_size = IntegerField()
file_ext = CharField()
rating = EnumField(["general", "sensitive", "questionable", "explicit"])
score = IntegerField()
up_score = IntegerField()
down_score = IntegerField()
fav_count = IntegerField()
file_url = CharField()
large_file_url = CharField()
preview_file_url = CharField()
tag_list = TagListField()
tag_list_general = TagListField()
tag_list_artist = TagListField()
tag_list_character = TagListField()
tag_list_copyright = TagListField()
tag_list_meta = TagListField()
tag_count = IntegerField()
tag_count_general = IntegerField()
tag_count_artist = IntegerField()
tag_count_character = IntegerField()
tag_count_copyright = IntegerField()
tag_count_meta = IntegerField()
class PostFTS(FTS5Model):
class Meta:
database = db
tag_list = TagListField()
tag_list_general = TagListField()
tag_list_artist = TagListField()
tag_list_character = TagListField()
tag_list_copyright = TagListField()
tag_list_meta = TagListField()
class Tag(BaseModel):
id = IntegerField(primary_key=True)
name = CharField(unique=True)
type = EnumField(["general", "artist", "character", "copyright", "meta"])
popularity = IntegerField()
def __str__(self):
return f"<Tag: {self.name}>"
class TagWiki(Model):
id = IntegerField(primary_key=True)
title = CharField(unique=True)
body = TextField()
created_at = DateTimeField()
updated_at = DateTimeField()
is_locked = BooleanField()
is_deleted = BooleanField()
other_names = TextField()
tag_id = IntegerField(unique=True)
class Meta:
table_name = "tag_wikis"
database = db
if __name__ == "__main__":
#from objprint import objprint
db.connect()
for index in db.get_indexes("post"):
print(index)
post = Post.select().first()
#objprint(model_to_dict(post))
# db.create_tables([Post, Tag])
# db.save()
|