Consolidated in media class

This commit is contained in:
2025-07-11 02:20:52 +02:00
parent bc4ed5e8d4
commit e305ea098c

38
main.py
View File

@@ -1,12 +1,15 @@
import os, re, shutil, calendar, datetime import os, re, shutil, calendar, datetime
import exifread import exifread
class Photos(): class Media():
def __init__(self, path, name): def __init__(self, path, name, kind):
self.path = path self.path = path
self.name = name self.name = name
self.kind = kind
def order_by_tag(self, export_dir): def order_by_tag(self, export_dir):
if self.kind != 'photo':
raise NotImplementedError("order_by_tag is only for photos.")
with open(self.path, 'rb') as ph: with open(self.path, 'rb') as ph:
tags = exifread.process_file(ph) tags = exifread.process_file(ph)
if 'Image DateTime' in tags: if 'Image DateTime' in tags:
@@ -42,26 +45,6 @@ class Photos():
shutil.move(self.path, os.path.join(month_dir, self.name)) shutil.move(self.path, os.path.join(month_dir, self.name))
os.utime(os.path.join(month_dir, self.name), (date_obj.timestamp(), date_obj.timestamp())) os.utime(os.path.join(month_dir, self.name), (date_obj.timestamp(), date_obj.timestamp()))
class Videos():
def __init__(self, path, name):
self.path = path
self.name = name
def order_by_match(self, matches, export_dir):
m = next((re.search(pattern, self.path) for pattern in matches if re.search(pattern, self.path)), None)
if not m:
return
year = int(m.group(1))
if year < 2000 or year > 2030:
return
month = int(m.group(2))
day = int(m.group(3))
date_obj = datetime.datetime(year, month, day)
month_dir = os.path.join(export_dir, str(year), calendar.month_name[month])
os.makedirs(month_dir, exist_ok=True)
shutil.move(self.path, os.path.join(month_dir, self.name))
os.utime(os.path.join(month_dir, self.name), (date_obj.timestamp(), date_obj.timestamp()))
root_dir = './' root_dir = './'
export_dir = './Export/' export_dir = './Export/'
@@ -74,15 +57,14 @@ for root, dirs, files in os.walk(root_dir):
path = os.path.join(root, file) path = os.path.join(root, file)
ext = os.path.splitext(path)[1].lower() ext = os.path.splitext(path)[1].lower()
if ext in ('.jpg', '.jpeg', '.png', '.webp', '.heic'): if ext in ('.jpg', '.jpeg', '.png', '.webp', '.heic'):
media.append(Photos(path, file)) media.append(Media(path, file, 'photo'))
elif ext in ('.mov', '.mp4', '.3gp'): elif ext in ('.mov', '.mp4', '.3gp'):
media.append(Videos(path, file)) media.append(Media(path, file, 'video'))
for element in media: for element in media:
try: try:
if hasattr(element, 'order_by_tag'): element.order_by_tag(export_dir)
element.order_by_tag(export_dir) except (ValueError, NotImplementedError):
else: element.order_by_match(matches, export_dir)
element.order_by_match(matches, export_dir)
except Exception as e: except Exception as e:
print(f'Error processing {element.name}: {e}') print(f'Error processing {element.name}: {e}')