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:
parent
b3020670b7
commit
a5ecbcb56d
@ -16,11 +16,12 @@ class Library:
|
|||||||
logging.info("%s move to orphan", p)
|
logging.info("%s move to orphan", p)
|
||||||
shutil.move(os.fspath(p), os.fspath(self.dir_orphan))
|
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 = self.__compute_path(tags)
|
||||||
new_path.mkdir(exist_ok=True, parents=True)
|
new_path.mkdir(exist_ok=True, parents=True)
|
||||||
logging.info("%s move to %s", p.name, new_path)
|
logging.info("%s move to %s", p.name, new_path)
|
||||||
shutil.move(os.fspath(p), os.fspath(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:
|
def __compute_path(self, tags: Tags) -> Path:
|
||||||
p = self.dir_root
|
p = self.dir_root
|
||||||
@ -51,3 +52,4 @@ class Library:
|
|||||||
def __sanitize(s: str) -> str:
|
def __sanitize(s: str) -> str:
|
||||||
s = "".join(x for x in s if x.isalnum() or x in "._-()")
|
s = "".join(x for x in s if x.isalnum() or x in "._-()")
|
||||||
return s.replace("_", " ").strip()
|
return s.replace("_", " ").strip()
|
||||||
|
|
||||||
|
10
picsorter.py
10
picsorter.py
@ -51,7 +51,7 @@ class PicSorter:
|
|||||||
|
|
||||||
def process(self, inputs: list[str]) -> None:
|
def process(self, inputs: list[str]) -> None:
|
||||||
for input in inputs:
|
for input in inputs:
|
||||||
if input.startswith("http"):
|
if input.startswith("http") or re.search(r"(\d{3,})", input):
|
||||||
print("Processing url", input)
|
print("Processing url", input)
|
||||||
self.__process_url(input)
|
self.__process_url(input)
|
||||||
else:
|
else:
|
||||||
@ -82,6 +82,7 @@ class PicSorter:
|
|||||||
from_path = os.fspath(filename)
|
from_path = os.fspath(filename)
|
||||||
to_path = os.fspath(self.config.dir_processed)
|
to_path = os.fspath(self.config.dir_processed)
|
||||||
shutil.move(from_path, to_path)
|
shutil.move(from_path, to_path)
|
||||||
|
print("Saved to", to_path)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -94,7 +95,7 @@ class PicSorter:
|
|||||||
return url
|
return url
|
||||||
|
|
||||||
def __process_url(self, url: str) -> bool:
|
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:
|
if not m:
|
||||||
return False
|
return False
|
||||||
post_id = int(m.group(1))
|
post_id = int(m.group(1))
|
||||||
@ -102,12 +103,13 @@ class PicSorter:
|
|||||||
logging.info("Skipping exists post %d", post_id)
|
logging.info("Skipping exists post %d", post_id)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
meta_result = self.metadata.process(url)
|
meta_result = self.metadata.process("https://danbooru.donmai.us/posts/" + url)
|
||||||
if meta_result is None:
|
if meta_result is None:
|
||||||
return False
|
return False
|
||||||
image_path, tags = meta_result
|
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)
|
self.db.add(post_id, tags.tags_string)
|
||||||
|
print("Saved to", to_path)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user