1
0
mirror of https://gist.github.com/6ba37e4d4084e858f917e271550ce5f6.git synced 2024-09-20 00:34:20 +03:00

Sanitize character tags by removing copyright suffix

This commit is contained in:
Victor 2021-04-14 23:59:28 +03:00
parent 40aac6203b
commit 5e22d1d9a6
4 changed files with 25 additions and 9 deletions

3
_README.md Normal file
View File

@ -0,0 +1,3 @@
## PicSorter
Finds an image on danbooru, writes tags as IPTC keywords, than places the image in the library

View File

@ -7,7 +7,7 @@ import os
class Library:
def __init__(self, dir_root: Path):
self.dir_root = dir_root
self.dir_orphan = Path(dir_root, 'orphan')
self.dir_orphan = Path(dir_root, '_orphan')
self.dir_orphan.mkdir(exist_ok=True, parents=True)
def move_to_orphan(self, p: Path):
@ -37,14 +37,9 @@ class Library:
if tags.characters == "":
return p
# Characters section
characters = tags.characters.split(" ")
characters = tags.characters_sanitized()
if len(characters) == 1:
character = characters[0] \
.replace(copyright, "") \
.replace("("+copyright+")", "") \
.replace("()", "") \
.strip()
p = p / self.__sanitize(character)
p = p / self.__sanitize(characters[0])
else:
p = p / "_multiple"
return p

View File

@ -91,7 +91,7 @@ class Metadata:
def __format_filename(self, tags: Tags):
filename = '{} {} by {} at {}.jpg'.format(
tags.copyrights.split(" ")[0] or "",
", ".join(tags.characters.split(" ")[:2]),
", ".join(tags.characters_sanitized()[:2]),
tags.artists.split(" ")[0] or "",
datetime.now().strftime('%Y%m%d_%H%M%S')
)

18
tags.py
View File

@ -14,6 +14,24 @@ class Tags:
self.tags = self.__union_tags()
self.tags_string = " ".join(self.tags)
def characters_sanitized(self) -> list:
if self.copyrights == "":
# No need to sanitize tags
return self.characters.split(" ")
copyrights = self.copyrights.split(" ")
return _(self.characters) \
.split(" ") \
.filter(lambda s: s != "") \
.map(lambda s: self.__rename(s, copyrights)) \
._
def __rename(self, s: str, substrings: list) -> str:
for substring in substrings:
s = s.replace("_("+substring+")", "") \
.replace("("+substring+")", "") \
.strip()
return s
def __union_tags(self):
tags = self.general.split(" ")
tags += self.__prefix_tags(self.copyrights, 'copyright_')