Fix bug : tags were referencing wrong data
Browse files- commit_difference.py +47 -5
commit_difference.py
CHANGED
@@ -61,10 +61,10 @@ def commit_difference(id, difference):
|
|
61 |
target_values_to_remove = old_dict[keys]
|
62 |
target_values_to_add = new_dict[keys]
|
63 |
# remove old values
|
64 |
-
for
|
65 |
-
name
|
66 |
-
|
67 |
-
current_value.remove(
|
68 |
# add new values
|
69 |
for values in target_values_to_add:
|
70 |
# no int values allowed
|
@@ -76,7 +76,6 @@ def commit_difference(id, difference):
|
|
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"):
|
@@ -110,6 +109,30 @@ FIELDS_TO_EXTRACT = {
|
|
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"
|
@@ -125,6 +148,25 @@ def save_post(idx:int):
|
|
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
|
|
|
61 |
target_values_to_remove = old_dict[keys]
|
62 |
target_values_to_add = new_dict[keys]
|
63 |
# remove old values
|
64 |
+
for name in target_values_to_remove:
|
65 |
+
if name in (tag.name for tag in current_value):
|
66 |
+
#print(f"{name} is in {[tag.name for tag in current_value]}, removing")
|
67 |
+
current_value.remove(Tag.get(Tag.name == name))
|
68 |
# add new values
|
69 |
for values in target_values_to_add:
|
70 |
# no int values allowed
|
|
|
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 |
else:
|
80 |
return
|
81 |
def main(filepath="difference_cache.jsonl"):
|
|
|
109 |
'tag_list_copyright': 'tag_list_copyright',
|
110 |
'tag_list_meta': 'tag_list_meta',
|
111 |
}
|
112 |
+
def pretty_print(post_id:int) -> str:
|
113 |
+
"""
|
114 |
+
Returns a pretty printed post
|
115 |
+
"""
|
116 |
+
post = Post.get_by_id(post_id)
|
117 |
+
def pretty_print_tag(tag):
|
118 |
+
"""
|
119 |
+
Returns a pretty printed tag
|
120 |
+
"""
|
121 |
+
return f"{tag.name}({tag.id})"
|
122 |
+
|
123 |
+
def pretty_print_tags(tags):
|
124 |
+
"""
|
125 |
+
Returns a pretty printed list of tags
|
126 |
+
"""
|
127 |
+
return ", ".join([pretty_print_tag(tag) for tag in tags])
|
128 |
+
|
129 |
+
fields = {"tag_list_general", "tag_list_artist", "tag_list_character", "tag_list_meta", "tag_list_copyright"}
|
130 |
+
string = ""
|
131 |
+
# start printing
|
132 |
+
string += f"Post {post_id}\n"
|
133 |
+
for field in fields:
|
134 |
+
string += f"\t{field}: {pretty_print_tags(post.__getattribute__(field))}\n"
|
135 |
+
return string
|
136 |
|
137 |
def save_post(idx:int):
|
138 |
folder = "danbooru2023_fixed"
|
|
|
148 |
dump[field] = [tag.name for tag in dump[field]]
|
149 |
json.dump(dump, file)
|
150 |
|
151 |
+
def test():
|
152 |
+
"""
|
153 |
+
This is a test function that you can test without running the entire script
|
154 |
+
"""
|
155 |
+
print(pretty_print(6719955))
|
156 |
+
diff = {"id": 6719955, "difference": [{"tag_list_general": ["virtual_youtuber"], "tag_list_character": ["otonose_kanade"], "tag_list_artist": ["jb_jagbung"], "tag_list_copyright": ["hololive", "hololive_dev_is"]}, {"tag_list_general": ["sad_keanu_(meme)"], "tag_list_character": ["regloss_(hololive)"], "tag_list_artist": ["xt0y4x"], "tag_list_copyright": ["tokino_sora_(old_design)", "juufuutei_raden"]}]}
|
157 |
+
# test, simulate commit, do not commit
|
158 |
+
with db.atomic() as transaction:
|
159 |
+
commit_difference(diff['id'], diff['difference'])
|
160 |
+
print(pretty_print(6719955))
|
161 |
+
transaction.rollback()
|
162 |
+
print(pretty_print(6719955))
|
163 |
+
|
164 |
+
def test2():
|
165 |
+
"""
|
166 |
+
After commit, you can check if now the post is different
|
167 |
+
"""
|
168 |
+
print(pretty_print(6719955))
|
169 |
+
|
170 |
if __name__ == "__main__":
|
171 |
jsonl_files = glob.glob(r"G:\database\finished\*.jsonl")
|
172 |
# sort by number
|