mirror of
https://gist.github.com/6ba37e4d4084e858f917e271550ce5f6.git
synced 2024-09-20 00:34:20 +03:00
Improve error handling
This commit is contained in:
parent
aa40b93401
commit
b6c36f0d24
@ -22,7 +22,7 @@ class Database:
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def is_exists(self, _id, provider) -> bool:
|
||||
def is_exists(self, provider, _id) -> bool:
|
||||
conn = sqlite3.connect(self.db_name)
|
||||
c = conn.cursor()
|
||||
c.execute("SELECT EXISTS(SELECT 1 FROM images WHERE id=? AND provider=?)", (_id, provider))
|
||||
|
@ -15,6 +15,7 @@ class Metadata:
|
||||
def __init__(self, dir_tmp: Path):
|
||||
self.dir_tmp = dir_tmp
|
||||
self.tmp_image_file = Path(self.dir_tmp, "tmp.jpg")
|
||||
self.tmp_fallback_download_file = Path(self.dir_tmp, "dl.jpg")
|
||||
|
||||
def process(self, url: str) -> Optional[tuple[Path, Tags]]:
|
||||
logging.info("Retrieving metadata for %s", url)
|
||||
@ -34,8 +35,14 @@ class Metadata:
|
||||
w = int(r.get("image_width", "0"))
|
||||
h = int(r.get("image_height", "0"))
|
||||
if (ext not in ["jpg", "jpeg", "png", "webp"]) or w == 0 or h == 0:
|
||||
logging.warning("Skipping due to unsupported extension: %s", ext)
|
||||
print("\033[93mSkipping due to unsupported extension:", ext, "\033[0m")
|
||||
return False
|
||||
file_url = r.get("file_url")
|
||||
if file_url is None:
|
||||
logging.warning("Skipping due to an empty file url")
|
||||
print("\033[93mSkipping due to an empty file url\033[0m")
|
||||
return False
|
||||
file_size_kb = int(r.get('file_size', "0")) / 1024
|
||||
|
||||
logging.info("Downloading image")
|
||||
|
@ -43,10 +43,12 @@ class PicSorter:
|
||||
filename = datetime.now().strftime('%Y-%m-%d.log')
|
||||
logfile = Path(dir_logs, filename)
|
||||
logging.basicConfig(
|
||||
filename=os.fspath(logfile),
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s %(levelname)s %(module)s: %(message)s',
|
||||
datefmt='%H:%M:%S',
|
||||
handlers=[
|
||||
logging.FileHandler(os.fspath(logfile))
|
||||
]
|
||||
)
|
||||
|
||||
def process(self, inputs: list[str]) -> None:
|
||||
@ -116,7 +118,7 @@ class PicSorter:
|
||||
return True
|
||||
|
||||
def __show_path(self, p: str) -> None:
|
||||
print("Saved to", 'file://' + p.replace(' ', '%20'))
|
||||
print("\033[92mSaved to", 'file://' + p.replace(' ', '%20'), "\033[0m")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
Loading…
Reference in New Issue
Block a user