diff --git a/library.py b/library.py index a632679..f6fb1b3 100644 --- a/library.py +++ b/library.py @@ -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() + diff --git a/picsorter.py b/picsorter.py index a85dcca..ef4ddfa 100644 --- a/picsorter.py +++ b/picsorter.py @@ -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