Maintenance codes
Browse files- add_post.py +128 -0
- commit_difference.py +133 -0
- db.py +116 -0
- fix_tags.py +29 -0
- view-dataset.py +57 -0
add_post.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
import glob
|
| 5 |
+
from tqdm import tqdm
|
| 6 |
+
from db import *
|
| 7 |
+
|
| 8 |
+
def yield_posts(file_dir=r"G:\database\post", from_id=0, end_id=7110548):
|
| 9 |
+
"""
|
| 10 |
+
Yields the posts
|
| 11 |
+
"""
|
| 12 |
+
# using listdir instead of glob because glob is slow
|
| 13 |
+
files = []
|
| 14 |
+
# walk through all files
|
| 15 |
+
for root, dirs, filenames in os.walk(file_dir):
|
| 16 |
+
for filename in filenames:
|
| 17 |
+
if "_" not in filename:
|
| 18 |
+
continue
|
| 19 |
+
# 0_19.jsonl -> 0, 19
|
| 20 |
+
start_id, end_id = filename.split(".")[0].split("_")
|
| 21 |
+
start_id = int(start_id)
|
| 22 |
+
end_id = int(end_id)
|
| 23 |
+
if start_id > end_id:
|
| 24 |
+
continue
|
| 25 |
+
if end_id < from_id:
|
| 26 |
+
continue
|
| 27 |
+
files.append(os.path.join(root, filename))
|
| 28 |
+
print(f"Total {len(files)} files")
|
| 29 |
+
for file in files:
|
| 30 |
+
with open(file, 'r') as f:
|
| 31 |
+
yield from f.readlines()
|
| 32 |
+
|
| 33 |
+
def add_post_if_not_exist(post_string):
|
| 34 |
+
try:
|
| 35 |
+
post_data = json.loads(post_string)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"Exception: {e} when loading {post_string}")
|
| 38 |
+
return
|
| 39 |
+
post_id = post_data['id']
|
| 40 |
+
if (post:=Post.get_or_none(Post.id == post_id)) is not None:
|
| 41 |
+
# check if it has valid tag list
|
| 42 |
+
if not post.tag_list:
|
| 43 |
+
# remove post
|
| 44 |
+
Post.delete_by_id(post_id)
|
| 45 |
+
else:
|
| 46 |
+
return
|
| 47 |
+
# prepare tags first
|
| 48 |
+
for keys in ["tag_string_general, tag_string_artist, tag_string_character, tag_string_meta", "tag_string", "tag_string_copyright"]:
|
| 49 |
+
if keys not in post_data:
|
| 50 |
+
continue
|
| 51 |
+
values = post_data[keys]
|
| 52 |
+
if not values:
|
| 53 |
+
continue
|
| 54 |
+
if not isinstance(values, list):
|
| 55 |
+
values = values.split(" ")
|
| 56 |
+
current_value = []
|
| 57 |
+
for value in values:
|
| 58 |
+
tag = Tag.get_or_none(name=value)
|
| 59 |
+
if tag is None:
|
| 60 |
+
tag = Tag.create(name=value, type=keys.split("_")[-1], popularity=0)
|
| 61 |
+
# assert unique, tag should not be iterable
|
| 62 |
+
assert not isinstance(tag, list), f"Error: {tag} is iterable"
|
| 63 |
+
current_value.append(tag.id)
|
| 64 |
+
post_data[keys.replace("tag_string", "tag_list")] = current_value
|
| 65 |
+
post_data[keys.replace("tag_string", "tag_count")] = len(current_value)
|
| 66 |
+
# add post
|
| 67 |
+
data = dict(
|
| 68 |
+
id=post_id,
|
| 69 |
+
created_at=post_data.get('created_at', None),
|
| 70 |
+
source=post_data.get('source', ""),
|
| 71 |
+
rating=post_data.get('rating', 'q'),
|
| 72 |
+
score=post_data.get('score', 0),
|
| 73 |
+
up_score=post_data.get('up_score', 0),
|
| 74 |
+
down_score=post_data.get('down_score', 0),
|
| 75 |
+
fav_count=post_data.get('fav_count', 0),
|
| 76 |
+
tag_list_general=post_data.get('tag_list_general', []),
|
| 77 |
+
tag_list_artist=post_data.get('tag_list_artist', []),
|
| 78 |
+
tag_list_character=post_data.get('tag_list_character', []),
|
| 79 |
+
tag_list_meta=post_data.get('tag_list_meta', []),
|
| 80 |
+
tag_list=post_data.get('tag_list', []),
|
| 81 |
+
tag_list_copyright=post_data.get('tag_list', []),
|
| 82 |
+
tag_count_general=post_data.get('tag_count_general', 0),
|
| 83 |
+
tag_count_artist=post_data.get('tag_count_artist', 0),
|
| 84 |
+
tag_count_character=post_data.get('tag_count_character', 0),
|
| 85 |
+
tag_count_meta=post_data.get('tag_count_meta', 0),
|
| 86 |
+
tag_count=post_data.get('tag_count', 0),
|
| 87 |
+
tag_count_copyright=post_data.get('tag_count', 0),
|
| 88 |
+
uploader_id=post_data.get('uploader_id', 0),
|
| 89 |
+
md5=post_data.get('md5', None),
|
| 90 |
+
parent_id=post_data.get('parent_id', None),
|
| 91 |
+
has_children=post_data.get('has_children', False),
|
| 92 |
+
is_deleted=post_data.get('is_deleted', False),
|
| 93 |
+
is_banned=post_data.get('is_banned', False),
|
| 94 |
+
pixiv_id=post_data.get('pixiv_id', None),
|
| 95 |
+
has_active_children=post_data.get('has_active_children', False),
|
| 96 |
+
bit_flags=post_data.get('bit_flags', 0),
|
| 97 |
+
has_large=post_data.get('has_large', False),
|
| 98 |
+
has_visible_children=post_data.get('has_visible_children', False),
|
| 99 |
+
image_width=post_data.get('image_width', 0),
|
| 100 |
+
image_height=post_data.get('image_height', 0),
|
| 101 |
+
file_size=post_data.get('file_size', 0),
|
| 102 |
+
file_ext=post_data.get('file_ext', "jpg"),
|
| 103 |
+
file_url=post_data.get('file_url', ""),
|
| 104 |
+
large_file_url=post_data.get('large_file_url', ""),
|
| 105 |
+
preview_file_url=post_data.get('preview_file_url', "")
|
| 106 |
+
)
|
| 107 |
+
# create from dict
|
| 108 |
+
Post.create(**data)
|
| 109 |
+
print(f"Added post {post_id}")
|
| 110 |
+
|
| 111 |
+
def main():
|
| 112 |
+
last_post_id = Post.select().order_by(Post.id.desc()).get().id
|
| 113 |
+
print(f"Last post id: {last_post_id}")
|
| 114 |
+
with db.atomic():
|
| 115 |
+
pbar = tqdm(total=7110548 - last_post_id)
|
| 116 |
+
for post_string in yield_posts(from_id=last_post_id):
|
| 117 |
+
try:
|
| 118 |
+
add_post_if_not_exist(post_string)
|
| 119 |
+
except Exception as e:
|
| 120 |
+
if isinstance(e, KeyboardInterrupt):
|
| 121 |
+
raise e
|
| 122 |
+
print(f"Exception: {e} when adding {post_string}")
|
| 123 |
+
raise e
|
| 124 |
+
pbar.update(1)
|
| 125 |
+
print("Done")
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
main()
|
commit_difference.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Reads and commits differences from difference_cache.jsonl files to database
|
| 3 |
+
Uses batch commit to speed up commit process
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import glob
|
| 7 |
+
from random import choice
|
| 8 |
+
from db import *
|
| 9 |
+
import os
|
| 10 |
+
import json
|
| 11 |
+
from tqdm import tqdm
|
| 12 |
+
|
| 13 |
+
def is_different(diff_0, diff_1):
|
| 14 |
+
if not diff_0 and not diff_1:
|
| 15 |
+
return False
|
| 16 |
+
return True
|
| 17 |
+
|
| 18 |
+
def get_differences(filepath):
|
| 19 |
+
"""
|
| 20 |
+
Reads differences from difference_cache.jsonl
|
| 21 |
+
Returns list of differences
|
| 22 |
+
"""
|
| 23 |
+
differences = {}
|
| 24 |
+
with open(filepath, 'r') as file:
|
| 25 |
+
for line in file:
|
| 26 |
+
try:
|
| 27 |
+
loaded_dict = json.loads(line)
|
| 28 |
+
if not loaded_dict['difference']:
|
| 29 |
+
continue
|
| 30 |
+
if not is_different(loaded_dict['difference'][0], loaded_dict['difference'][1]):
|
| 31 |
+
continue
|
| 32 |
+
differences[loaded_dict['id']] = loaded_dict['difference']
|
| 33 |
+
except:
|
| 34 |
+
pass
|
| 35 |
+
return differences
|
| 36 |
+
|
| 37 |
+
def commit_differences(differences):
|
| 38 |
+
"""
|
| 39 |
+
Commits differences to database
|
| 40 |
+
Returns number of differences committed
|
| 41 |
+
"""
|
| 42 |
+
for ids in tqdm(differences):
|
| 43 |
+
if not is_different(differences[ids][0], differences[ids][1]):
|
| 44 |
+
continue
|
| 45 |
+
commit_difference(ids, differences[ids])
|
| 46 |
+
def commit_difference(id, difference):
|
| 47 |
+
"""
|
| 48 |
+
Commits difference to database
|
| 49 |
+
"""
|
| 50 |
+
if difference:
|
| 51 |
+
post_by_id = Post.get_by_id(id)
|
| 52 |
+
new_dict, old_dict = difference
|
| 53 |
+
for keys in new_dict:
|
| 54 |
+
if keys not in old_dict:
|
| 55 |
+
post_by_id.__setattr__(keys, new_dict[keys])
|
| 56 |
+
else:
|
| 57 |
+
# replace old value with new value
|
| 58 |
+
if "tag" not in keys:
|
| 59 |
+
print(f"Warning: {keys} is not a tag")
|
| 60 |
+
current_value = post_by_id.__getattribute__(keys)
|
| 61 |
+
target_values_to_remove = old_dict[keys]
|
| 62 |
+
target_values_to_add = new_dict[keys]
|
| 63 |
+
# remove old values
|
| 64 |
+
for values in current_value:
|
| 65 |
+
name = values.name
|
| 66 |
+
if name in target_values_to_remove:
|
| 67 |
+
current_value.remove(values)
|
| 68 |
+
# add new values
|
| 69 |
+
for values in target_values_to_add:
|
| 70 |
+
# no int values allowed
|
| 71 |
+
assert isinstance(values, str), f"Error: {values} is not a string"
|
| 72 |
+
tag = Tag.get_or_none(Tag.name == values)
|
| 73 |
+
if tag is None:
|
| 74 |
+
print(f"Warning: {values} is not in database, adding")
|
| 75 |
+
tag = Tag.create(name=values, type=keys.split("_")[-1], popularity=0)
|
| 76 |
+
# assert unique, tag should not be iterable
|
| 77 |
+
assert not isinstance(tag, list), f"Error: {tag} is iterable"
|
| 78 |
+
current_value.append(tag)
|
| 79 |
+
post_by_id.save()
|
| 80 |
+
else:
|
| 81 |
+
return
|
| 82 |
+
def main(filepath="difference_cache.jsonl"):
|
| 83 |
+
print(f"Reading differences from {filepath}")
|
| 84 |
+
differences = get_differences(filepath)
|
| 85 |
+
with db.atomic():# commit all changes at once
|
| 86 |
+
commit_differences(differences)
|
| 87 |
+
# sample random post from differences and show
|
| 88 |
+
sample_post_id = choice(list(differences.keys()))
|
| 89 |
+
print(f"Sample Post: {sample_post_id}")
|
| 90 |
+
print(f"Sample Difference: {differences[sample_post_id]}")
|
| 91 |
+
# get real state of post
|
| 92 |
+
post = Post.get_by_id(sample_post_id)
|
| 93 |
+
for fields in FIELDS_TO_EXTRACT:
|
| 94 |
+
attr = post.__getattribute__(FIELDS_TO_EXTRACT[fields])
|
| 95 |
+
if isinstance(attr, list):
|
| 96 |
+
print(f"{fields}: {[tag.name for tag in attr]}")
|
| 97 |
+
else:
|
| 98 |
+
print(f"{fields}: {attr}")
|
| 99 |
+
|
| 100 |
+
FIELDS_TO_EXTRACT = {
|
| 101 |
+
'id': 'id',
|
| 102 |
+
'created_at': 'created_at',
|
| 103 |
+
'source': 'source',
|
| 104 |
+
'rating': 'rating',
|
| 105 |
+
'score': 'score',
|
| 106 |
+
'fav_count': 'fav_count',
|
| 107 |
+
'tag_list_general': 'tag_list_general',
|
| 108 |
+
'tag_list_artist': 'tag_list_artist',
|
| 109 |
+
'tag_list_character': 'tag_list_character',
|
| 110 |
+
'tag_list_copyright': 'tag_list_copyright',
|
| 111 |
+
'tag_list_meta': 'tag_list_meta',
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
def save_post(idx:int):
|
| 115 |
+
folder = "danbooru2023_fixed"
|
| 116 |
+
dump = {}
|
| 117 |
+
if not os.path.exists(f"{folder}/posts"):
|
| 118 |
+
os.makedirs(f"{folder}/posts")
|
| 119 |
+
post = Post.get_by_id(idx)
|
| 120 |
+
with open(f"{folder}/posts/{idx}.json",'w') as file:
|
| 121 |
+
for field in FIELDS_TO_EXTRACT:
|
| 122 |
+
dump[field] = post.__getattribute__(FIELDS_TO_EXTRACT[field])
|
| 123 |
+
# if list, convert to list of string instead of tag objects
|
| 124 |
+
if isinstance(dump[field], list):
|
| 125 |
+
dump[field] = [tag.name for tag in dump[field]]
|
| 126 |
+
json.dump(dump, file)
|
| 127 |
+
|
| 128 |
+
if __name__ == "__main__":
|
| 129 |
+
jsonl_files = glob.glob(r"G:\database\finished\*.jsonl")
|
| 130 |
+
# sort by number
|
| 131 |
+
jsonl_files.sort()
|
| 132 |
+
for file in tqdm(jsonl_files):
|
| 133 |
+
main(file)
|
db.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import pathlib
|
| 3 |
+
import sqlite3
|
| 4 |
+
from peewee import *
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class MemoryConnection(sqlite3.Connection):
|
| 8 |
+
def __init__(self, dbname, *args, **kwargs):
|
| 9 |
+
load_conn = sqlite3.connect(dbname)
|
| 10 |
+
super(MemoryConnection, self).__init__(":memory:", *args, **kwargs)
|
| 11 |
+
load_conn.backup(self)
|
| 12 |
+
load_conn.close()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class SqliteMemDatabase(SqliteDatabase):
|
| 16 |
+
def __init__(self, database, *args, **kwargs) -> None:
|
| 17 |
+
self.dbname = database
|
| 18 |
+
super().__init__(database, *args, factory=MemoryConnection, **kwargs)
|
| 19 |
+
|
| 20 |
+
def reload(self, dbname=None):
|
| 21 |
+
if dbname is None:
|
| 22 |
+
dbname = self.dbname
|
| 23 |
+
load_conn = sqlite3.connect(dbname)
|
| 24 |
+
try:
|
| 25 |
+
load_conn.backup(self._state.conn)
|
| 26 |
+
finally:
|
| 27 |
+
load_conn.close()
|
| 28 |
+
|
| 29 |
+
def save(self, dbname=None):
|
| 30 |
+
if dbname is None:
|
| 31 |
+
dbname = self.dbname
|
| 32 |
+
save_conn = sqlite3.connect(dbname)
|
| 33 |
+
try:
|
| 34 |
+
self._state.conn.backup(save_conn)
|
| 35 |
+
finally:
|
| 36 |
+
save_conn.close()
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# db = SqliteMemDatabase(pathlib.Path(__file__).parent.resolve() / "danbooru2023.db")
|
| 40 |
+
db = SqliteDatabase(pathlib.Path(__file__).parent.resolve() / "danbooru2023.db")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class TagListField(TextField):
|
| 44 |
+
def db_value(self, value):
|
| 45 |
+
assert all(isinstance(tag, (Tag, int)) for tag in value)
|
| 46 |
+
return json.dumps([tag.id if isinstance(tag, Tag) else tag for tag in value])
|
| 47 |
+
|
| 48 |
+
def python_value(self, value):
|
| 49 |
+
if value is not None:
|
| 50 |
+
id_list = json.loads(value)
|
| 51 |
+
tags = Tag.select().where(Tag.id << id_list)
|
| 52 |
+
return [tag for tag in tags]
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class BaseModel(Model):
|
| 56 |
+
class Meta:
|
| 57 |
+
database = db
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class Post(BaseModel):
|
| 61 |
+
id = IntegerField(primary_key=True)
|
| 62 |
+
created_at = CharField()
|
| 63 |
+
uploader_id = IntegerField()
|
| 64 |
+
source = CharField()
|
| 65 |
+
md5 = CharField(null=True)
|
| 66 |
+
parent_id = IntegerField(null=True)
|
| 67 |
+
has_children = BooleanField()
|
| 68 |
+
is_deleted = BooleanField()
|
| 69 |
+
is_banned = BooleanField()
|
| 70 |
+
pixiv_id = IntegerField(null=True)
|
| 71 |
+
has_active_children = BooleanField()
|
| 72 |
+
bit_flags = IntegerField()
|
| 73 |
+
has_large = BooleanField()
|
| 74 |
+
has_visible_children = BooleanField()
|
| 75 |
+
|
| 76 |
+
image_width = IntegerField()
|
| 77 |
+
image_height = IntegerField()
|
| 78 |
+
file_size = IntegerField()
|
| 79 |
+
file_ext = CharField()
|
| 80 |
+
|
| 81 |
+
rating = CharField()
|
| 82 |
+
score = IntegerField()
|
| 83 |
+
up_score = IntegerField()
|
| 84 |
+
down_score = IntegerField()
|
| 85 |
+
fav_count = IntegerField()
|
| 86 |
+
|
| 87 |
+
file_url = CharField()
|
| 88 |
+
large_file_url = CharField()
|
| 89 |
+
preview_file_url = CharField()
|
| 90 |
+
|
| 91 |
+
tag_list = TagListField()
|
| 92 |
+
tag_list_general = TagListField()
|
| 93 |
+
tag_list_artist = TagListField()
|
| 94 |
+
tag_list_character = TagListField()
|
| 95 |
+
tag_list_copyright = TagListField()
|
| 96 |
+
tag_list_meta = TagListField()
|
| 97 |
+
|
| 98 |
+
tag_count = IntegerField()
|
| 99 |
+
tag_count_general = IntegerField()
|
| 100 |
+
tag_count_artist = IntegerField()
|
| 101 |
+
tag_count_character = IntegerField()
|
| 102 |
+
tag_count_copyright = IntegerField()
|
| 103 |
+
tag_count_meta = IntegerField()
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
class Tag(BaseModel):
|
| 107 |
+
id = IntegerField(primary_key=True)
|
| 108 |
+
name = CharField(unique=True)
|
| 109 |
+
type = CharField()
|
| 110 |
+
popularity = IntegerField()
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
db.connect()
|
| 115 |
+
db.create_tables([Post, Tag])
|
| 116 |
+
db.save()
|
fix_tags.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Executing the script will update the popularity of tags with actual popularity
|
| 3 |
+
Warn : this may take a long time
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from db import *
|
| 7 |
+
from collections import Counter
|
| 8 |
+
from peewee import fn
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
|
| 11 |
+
def fix_tags_with_Counter():
|
| 12 |
+
# Calculate tag popularity
|
| 13 |
+
counter = Counter()
|
| 14 |
+
# post -> post select -> to tag list
|
| 15 |
+
get_tags_list_from_post_query = (Post.select(Post.id))
|
| 16 |
+
for post in tqdm(get_tags_list_from_post_query):
|
| 17 |
+
post = Post.get_by_id(post.id)
|
| 18 |
+
counter.update([tag.id for tag in post.tag_list])
|
| 19 |
+
# Bulk update tags
|
| 20 |
+
with db.atomic():
|
| 21 |
+
print("Updating tags for {} tags".format(len(counter)))
|
| 22 |
+
for tag, popularity in tqdm(counter.items()):
|
| 23 |
+
print(f"Updating {tag} with popularity {popularity}")
|
| 24 |
+
(Tag.update({Tag.popularity: popularity})
|
| 25 |
+
.where(Tag.id == tag)
|
| 26 |
+
.execute())
|
| 27 |
+
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
fix_tags_with_Counter()
|
view-dataset.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from db import *
|
| 2 |
+
|
| 3 |
+
# view 6719955 post
|
| 4 |
+
|
| 5 |
+
def pretty_print(post_id:int) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Returns a pretty printed post
|
| 8 |
+
"""
|
| 9 |
+
post = Post.get_by_id(post_id)
|
| 10 |
+
def pretty_print_tag(tag):
|
| 11 |
+
"""
|
| 12 |
+
Returns a pretty printed tag
|
| 13 |
+
"""
|
| 14 |
+
return f"{tag.name}({tag.id})"
|
| 15 |
+
|
| 16 |
+
def pretty_print_tags(tags):
|
| 17 |
+
"""
|
| 18 |
+
Returns a pretty printed list of tags
|
| 19 |
+
"""
|
| 20 |
+
return ", ".join([pretty_print_tag(tag) for tag in tags])
|
| 21 |
+
|
| 22 |
+
fields = {"tag_list_general", "tag_list_artist", "tag_list_character", "tag_list_meta", "tag_list_copyright"}
|
| 23 |
+
string = ""
|
| 24 |
+
# start printing
|
| 25 |
+
string += f"Post {post_id}\n"
|
| 26 |
+
for field in fields:
|
| 27 |
+
string += f"\t{field}: {pretty_print_tags(post.__getattribute__(field))}\n"
|
| 28 |
+
return string
|
| 29 |
+
|
| 30 |
+
diff = {"tag_list_general": ["virtual_youtuber"], "tag_list_character": ["otonose_kanade"], "tag_list_artist": ["jb_jagbung"], "tag_list_copyright": ["hololive", "hololive_dev_is"]}
|
| 31 |
+
|
| 32 |
+
def get_ids_of_tags_from_difference(difference):
|
| 33 |
+
"""
|
| 34 |
+
Returns a list of ids of tags from the difference
|
| 35 |
+
"""
|
| 36 |
+
ids = dict()
|
| 37 |
+
for tag_list in difference.values():
|
| 38 |
+
for tag in tag_list:
|
| 39 |
+
ids[tag] = Tag.get(Tag.name == tag).id
|
| 40 |
+
return ids
|
| 41 |
+
|
| 42 |
+
def pretty_print_tags_from_difference(difference):
|
| 43 |
+
"""
|
| 44 |
+
Returns a pretty printed string of tags from the difference
|
| 45 |
+
"""
|
| 46 |
+
target_ids = get_ids_of_tags_from_difference(difference)
|
| 47 |
+
string = ""
|
| 48 |
+
for tag in target_ids:
|
| 49 |
+
string += f"{tag}({target_ids[tag]})\n"
|
| 50 |
+
return string
|
| 51 |
+
|
| 52 |
+
def main():
|
| 53 |
+
print(pretty_print(6719955))
|
| 54 |
+
print(pretty_print_tags_from_difference(diff))
|
| 55 |
+
|
| 56 |
+
if __name__ == '__main__':
|
| 57 |
+
main()
|