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

126 lines
4.2 KiB
Python
Raw Permalink Normal View History

2021-04-15 14:21:36 +03:00
import argparse
2021-04-15 12:30:26 +03:00
import logging
import os
import re
import shutil
import time
from datetime import datetime
from pathlib import Path
2021-04-15 14:21:36 +03:00
from typing import Optional
2021-04-15 12:30:26 +03:00
from config import Config
from database import Database
2021-04-14 23:23:56 +03:00
from iqdb import Iqdb
from library import Library
from metadata import Metadata
class PicSorter:
2021-04-15 14:21:36 +03:00
@staticmethod
def parse_args():
parser = argparse.ArgumentParser(
description='Finds an image on danbooru, writes tags as IPTC keywords, than places the image in the library'
)
parser.add_argument('-c', '--config',
type=Path,
default='config.yml',
help='config.yml file path')
parser.add_argument('input', nargs=argparse.REMAINDER)
args = parser.parse_args()
if len(args.input) >= 1:
PicSorter(args.config).process(args.input)
def __init__(self, config_file='config.yml'):
config = Config.load(config_file)
self.config = config
self.__setup_logging(config.dir_logs)
self.library = Library(config.dir_library)
self.metadata = Metadata(config.dir_tmp)
self.db = Database()
2021-04-14 23:23:56 +03:00
2021-04-15 14:21:36 +03:00
@staticmethod
def __setup_logging(dir_logs: Path):
2021-04-15 12:30:26 +03:00
filename = datetime.now().strftime('%Y-%m-%d.log')
2021-04-15 14:21:36 +03:00
logfile = Path(dir_logs, filename)
2021-04-14 23:23:56 +03:00
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(module)s: %(message)s',
datefmt='%H:%M:%S',
2023-07-29 17:59:01 +03:00
handlers=[
logging.FileHandler(os.fspath(logfile))
]
2021-04-14 23:23:56 +03:00
)
2021-04-15 14:21:36 +03:00
def process(self, inputs: list[str]) -> None:
for input in inputs:
2021-05-06 23:18:24 +03:00
if input.startswith("http") or re.search(r"(\d{3,})", input):
2021-04-15 14:21:36 +03:00
print("Processing url", input)
self.__process_url(input)
2021-04-18 13:44:50 +03:00
else:
p = Path(input)
if p.is_dir():
self.__process_folder(p)
elif p.is_file():
print("Processing file", input)
self.__process_file(input)
2021-04-15 14:21:36 +03:00
def __process_folder(self, dir_input: Path) -> None:
files = {p for p in dir_input.iterdir()
2021-04-15 12:30:26 +03:00
if p.suffix in [".jpg", ".png"]}
2021-04-15 00:38:10 +03:00
for filename in files:
2021-04-15 14:21:36 +03:00
print("Processing", filename)
2021-04-14 23:23:56 +03:00
try:
2021-04-15 14:21:36 +03:00
self.__process_file(filename)
2021-04-14 23:23:56 +03:00
except Exception as ex:
raise ex
2021-04-15 14:21:36 +03:00
time.sleep(5)
def __process_file(self, filename: str) -> bool:
url = self.__search_iqdb(filename)
if url is None:
return False
if self.__process_url(url):
self.config.dir_processed.mkdir(exist_ok=True, parents=True)
from_path = os.fspath(filename)
to_path = os.fspath(self.config.dir_processed)
shutil.move(from_path, to_path)
2021-07-27 12:50:38 +03:00
self.__show_path(to_path)
2021-04-15 14:21:36 +03:00
return True
return False
def __search_iqdb(self, filename: str) -> Optional[str]:
url = Iqdb.search(filename)
if url is None:
logging.warning("%s not found", filename)
self.library.move_to_orphan(Path(filename))
return None
return url
def __process_url(self, url: str) -> bool:
2023-03-20 19:09:25 +02:00
m = re.search(r"(https://((?:dan|ai)booru|yande).*?(?:post(?:s|/show)/)?(\d{3,}))", url)
2021-04-15 14:21:36 +03:00
if not m:
return False
2023-03-20 19:09:25 +02:00
provider = m.group(2)
post_id = int(m.group(3))
if provider not in ['danbooru', 'aibooru']:
return False
if self.db.is_exists(provider, post_id):
logging.info("Skipping exists post %s %d", provider, post_id)
2021-04-15 14:21:36 +03:00
return False
2023-03-20 19:09:25 +02:00
meta_result = self.metadata.process(m.group(1))
2021-04-15 14:21:36 +03:00
if meta_result is None:
return False
image_path, tags = meta_result
2021-05-06 23:18:24 +03:00
to_path = self.library.move(image_path, tags)
2023-03-20 19:09:25 +02:00
self.db.add(post_id, provider, tags.tags_string)
2021-07-27 12:50:38 +03:00
self.__show_path(to_path)
2021-04-15 14:21:36 +03:00
return True
2021-04-15 12:30:26 +03:00
2021-07-27 12:50:38 +03:00
def __show_path(self, p: str) -> None:
2023-07-29 17:59:01 +03:00
print("\033[92mSaved to", 'file://' + p.replace(' ', '%20'), "\033[0m")
2021-07-27 12:50:38 +03:00
2021-04-15 12:30:26 +03:00
2021-04-14 23:23:56 +03:00
if __name__ == '__main__':
2021-04-15 14:21:36 +03:00
PicSorter.parse_args()