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

Improve url detecting

This commit is contained in:
Victor 2021-05-06 23:18:24 +03:00
parent b3020670b7
commit a5ecbcb56d
2 changed files with 9 additions and 5 deletions

View File

@ -16,11 +16,12 @@ class Library:
logging.info("%s move to orphan", p)
shutil.move(os.fspath(p), os.fspath(self.dir_orphan))
def move(self, p: Path, tags: Tags) -> None:
def move(self, p: Path, tags: Tags) -> str:
new_path = self.__compute_path(tags)
new_path.mkdir(exist_ok=True, parents=True)
logging.info("%s move to %s", p.name, new_path)
shutil.move(os.fspath(p), os.fspath(new_path))
return str(new_path).replace("\\", "/") + "/" + p.name
def __compute_path(self, tags: Tags) -> Path:
p = self.dir_root
@ -51,3 +52,4 @@ class Library:
def __sanitize(s: str) -> str:
s = "".join(x for x in s if x.isalnum() or x in "._-()")
return s.replace("_", " ").strip()

View File

@ -51,7 +51,7 @@ class PicSorter:
def process(self, inputs: list[str]) -> None:
for input in inputs:
if input.startswith("http"):
if input.startswith("http") or re.search(r"(\d{3,})", input):
print("Processing url", input)
self.__process_url(input)
else:
@ -82,6 +82,7 @@ class PicSorter:
from_path = os.fspath(filename)
to_path = os.fspath(self.config.dir_processed)
shutil.move(from_path, to_path)
print("Saved to", to_path)
return True
return False
@ -94,7 +95,7 @@ class PicSorter:
return url
def __process_url(self, url: str) -> bool:
m = re.search(r".*posts/(\d{3,})", url)
m = re.search(r"(?:posts/)?(\d{3,})", url)
if not m:
return False
post_id = int(m.group(1))
@ -102,12 +103,13 @@ class PicSorter:
logging.info("Skipping exists post %d", post_id)
return False
meta_result = self.metadata.process(url)
meta_result = self.metadata.process("https://danbooru.donmai.us/posts/" + url)
if meta_result is None:
return False
image_path, tags = meta_result
self.library.move(image_path, tags)
to_path = self.library.move(image_path, tags)
self.db.add(post_id, tags.tags_string)
print("Saved to", to_path)
return True