80 lines
4.2 KiB
Python
Executable File
80 lines
4.2 KiB
Python
Executable File
import os, re, shutil, calendar, datetime
|
|
import exifread
|
|
|
|
match = r'(.*)(\..*)'
|
|
root_dir = '/mnt/dati/process'
|
|
photos_dir = '/mnt/dati/export/Photos'
|
|
videos_dir = '/mnt/dati/export/Videos'
|
|
|
|
matches = (r'(\d{4})-(\d{2})-(\d{2})', r'(\d{4})(\d{2})(\d{2})')
|
|
|
|
|
|
for root, dirs, files in os.walk(root_dir):
|
|
for file in files:
|
|
data = os.path.join(root, file)
|
|
file_match = re.search(match, data, re.IGNORECASE)
|
|
if file_match and file_match.group(2).lower() in ('.jpg', '.jpeg', '.png', '.webp', '.heic'):
|
|
with open(data, 'rb') as ph:
|
|
foundMatch = False
|
|
try:
|
|
tags = exifread.process_file(ph)
|
|
if tags:
|
|
if 'Image DateTime' in tags:
|
|
date_taken = str(tags["Image DateTime"]).split()
|
|
elif 'EXIF DateTimeOriginal' in tags:
|
|
date_taken = str(tags["EXIF DateTimeOriginal"]).split()
|
|
year, month, day = map(int, date_taken[0].split(':'))
|
|
hour, minute, second = map(int, date_taken[1].split(':'))
|
|
date_obj = datetime.datetime(year, month, day, hour, minute, second)
|
|
month_dir = os.path.join(os.path.join(photos_dir, str(year)), calendar.month_name[month])
|
|
os.makedirs(month_dir, exist_ok=True)
|
|
shutil.copyfile(data, os.path.join(month_dir, file))
|
|
os.utime(os.path.join(month_dir, file), (date_obj.timestamp(), date_obj.timestamp()))
|
|
foundMatch = True
|
|
else:
|
|
for pattern in matches:
|
|
m = re.search(pattern, data)
|
|
if m:
|
|
year = int(m.group(1))
|
|
if year < 2000 or year > 2030:
|
|
continue
|
|
if re.search(r's100', data):
|
|
continue
|
|
if re.search(r'Screenshot', data, re.IGNORECASE):
|
|
continue
|
|
month = int(m.group(2))
|
|
day = int(m.group(3))
|
|
date_obj = datetime.datetime(year, month, day)
|
|
month_dir = os.path.join(os.path.join(photos_dir, str(year)), calendar.month_name[month])
|
|
os.makedirs(month_dir, exist_ok=True)
|
|
shutil.copyfile(data, os.path.join(month_dir, file))
|
|
os.utime(os.path.join(month_dir, file), (date_obj.timestamp(), date_obj.timestamp()))
|
|
foundMatch = True
|
|
break
|
|
except Exception as e:
|
|
print(f'Error processing photo {data}: {e}')
|
|
if not foundMatch:
|
|
print(f'No date pattern found in filename: {data}\n')
|
|
|
|
elif file_match and file_match.group(2).lower() in ('.mov', '.mp4', '.3gp'):
|
|
foundMatch = False
|
|
for pattern in matches:
|
|
try:
|
|
m = re.search(pattern, data)
|
|
if m:
|
|
year = int(m.group(1))
|
|
month = int(m.group(2))
|
|
day = int(m.group(3))
|
|
if year < 2000 or year > 2030:
|
|
continue
|
|
date_obj = datetime.datetime(year, month, day)
|
|
month_dir = os.path.join(os.path.join(videos_dir, str(year)), calendar.month_name[month])
|
|
os.makedirs(month_dir, exist_ok=True)
|
|
shutil.copyfile(data, os.path.join(month_dir, file))
|
|
os.utime(os.path.join(month_dir, file), (date_obj.timestamp(), date_obj.timestamp()))
|
|
foundMatch = True
|
|
break
|
|
except Exception as e:
|
|
print(f'Error processing video {data}: {e}')
|
|
if not foundMatch:
|
|
print(f'No date pattern found in filename: {data}\n') |