File size: 7,581 Bytes
740a4e4
7c6361a
989fb8b
7c6361a
 
3b218bb
627e398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b218bb
627e398
0974aba
 
3b218bb
740a4e4
 
 
 
7c6361a
3b218bb
961c20d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c6361a
 
 
 
 
627e398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b218bb
7c6361a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
961c20d
7c6361a
 
 
 
 
740a4e4
 
 
7c6361a
3b218bb
 
 
627e398
7c6361a
3b218bb
 
 
740a4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c6361a
627e398
 
 
 
 
7c6361a
627e398
 
 
989fb8b
627e398
 
 
989fb8b
627e398
 
 
989fb8b
627e398
 
 
7c6361a
627e398
 
 
961c20d
3b218bb
740a4e4
 
 
 
 
 
 
 
 
 
 
 
 
7c6361a
3b218bb
627e398
 
 
 
 
 
 
 
 
 
 
740a4e4
627e398
 
 
 
740a4e4
7c6361a
740a4e4
 
 
 
 
 
 
 
 
3b218bb
740a4e4
 
 
 
 
627e398
3b218bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
627e398
 
3b218bb
627e398
740a4e4
627e398
740a4e4
 
 
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os
import sqlite3

from peewee import *


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: SqliteDatabase = None
tag_cache_map = {}


def get_tag_by_id(tag_id):
    if tag_id not in tag_cache_map:
        tag_cache_map[tag_id] = Tag.get_by_id(tag_id)
    return tag_cache_map[tag_id]


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 Tag(BaseModel):
    id = IntegerField(primary_key=True)
    name = CharField(unique=True)
    type = EnumField(["general", "artist", "character", "copyright", "meta"])
    popularity = IntegerField()
    _posts: ManyToManyField
    _posts_cache = None

    @property
    def posts(self):
        if self._posts_cache is None:
            self._posts_cache = list(self._posts)
        return self._posts_cache

    def __str__(self):
        return f"<Tag '{self.name}'>"

    def __repr__(self):
        return f"<Tag|#{self.id}|{self.name}|{self.type[:2]}>"


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(null=True)
    large_file_url = CharField(null=True)
    preview_file_url = CharField(null=True)

    updated_at = CharField()  # "updated_at": "2024-05-19T01:30:25.096-04:00"

    _tags: ManyToManyField = None  # set by tags.bind
    _tags_cache = None

    def __hash__(self):
        return self.id

    @property
    def tag_count(self):
        return len(self.tag_list) if self.tag_list else 0

    @property
    def tag_count_general(self):
        return len(self.tag_list_general) if self.tag_list else 0

    @property
    def tag_count_artist(self):
        return len(self.tag_list_artist) if self.tag_list else 0

    @property
    def tag_count_character(self):
        return len(self.tag_list_character) if self.tag_list else 0

    @property
    def tag_count_copyright(self):
        return len(self.tag_list_copyright) if self.tag_list else 0

    @property
    def tag_count_meta(self):
        return len(self.tag_list_meta) if self.tag_list else 0

    @property
    def tag_list(self):
        if self._tags_cache is None:
            self._tags_cache = list(self._tags)
        return self._tags_cache

    @property
    def tag_list_general(self):
        return [tag for tag in self.tag_list if tag.type == "general"]

    @property
    def tag_list_artist(self):
        return [tag for tag in self.tag_list if tag.type == "artist"]

    @property
    def tag_list_character(self):
        return [tag for tag in self.tag_list if tag.type == "character"]

    @property
    def tag_list_copyright(self):
        return [tag for tag in self.tag_list if tag.type == "copyright"]

    @property
    def tag_list_meta(self):
        return [tag for tag in self.tag_list if tag.type == "meta"]


# create table LocalPost Table that can contain "filepath", "latentpath"
class LocalPost(BaseModel):
    # Usage : LocalPost.create(filepath="path/to/file", latentpath="path/to/latent", post=post)
    id = IntegerField(primary_key=True)
    filepath = CharField(null=True)
    latentpath = CharField(null=True)
    post = ForeignKeyField(Post, backref="localpost")

    def __str__(self):
        return f"<LocalPost '{self.filepath}'>"

    def __repr__(self):
        return f"<LocalPost|#{self.id}|{self.filepath}|{self.latentpath}|{self.post}>"


class PostTagRelation(BaseModel):
    post = ForeignKeyField(Post, backref="post_tags")
    tag = ForeignKeyField(Tag, backref="tag_posts")


tags = ManyToManyField(Tag, backref="_posts", through_model=PostTagRelation)
tags.bind(Post, "_tags", set_attribute=True)


def load_db(db_file: str):
    global db
    file_exists = os.path.exists(db_file)
    db = SqliteDatabase(db_file)
    Post._meta.database = db
    Tag._meta.database = db
    PostTagRelation._meta.database = db
    LocalPost._meta.database = db
    db.connect()
    print("Database connected.")
    if not file_exists:
        db.create_tables([Post, Tag, PostTagRelation])
        db.create_tables([LocalPost])
        db.commit()
        print("Database initialized.")
    assert db is not None, "Database is not loaded"
    return db


def print_post_info(post):
    """
    Debugging function to print out a post's tags
    """
    print(f"Post id : {post.id}")
    print(
        "General Tags: ",
        post.tag_count_general,
        len(post.tag_list_general),
        post.tag_list_general,
    )
    print(
        "Artist Tags: ",
        post.tag_count_artist,
        len(post.tag_list_artist),
        post.tag_list_artist,
    )
    print(
        "Character Tags: ",
        post.tag_count_character,
        len(post.tag_list_character),
        post.tag_list_character,
    )
    print(
        "Copyright Tags: ",
        post.tag_count_copyright,
        len(post.tag_list_copyright),
        post.tag_list_copyright,
    )
    print(
        "Meta Tags: ", post.tag_count_meta, len(post.tag_list_meta), post.tag_list_meta
    )


if __name__ == "__main__":
    db_existed = os.path.exists("danbooru2023.db")
    db = load_db("danbooru2023.db")
    if db_existed:
        # run sanity check
        post = Post.get_by_id(1)
        print_post_info(post)
        tag = Tag.select().where(Tag.name == "kousaka_kirino").get()
        print(f"Tag {tag} has {len(tag.posts)} posts")
        # get last post
        post = Post.select().order_by(Post.id.desc()).limit(1).get()
        print_post_info(post)