-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmp_timedatestamp.py
72 lines (57 loc) · 2.96 KB
/
xmp_timedatestamp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from pathlib import Path
import xml.etree.ElementTree as ET
import re
def prepend_date_to_filename(file_path, metadata_dir=None, whatif=False):
"""
Takes a file and prepends its filename with the DateCreated attribute
extracted from its XMP metadata, sanitizing unsafe characters. Optionally,
searches for the relevant XMP metadata file in a specified directory.
Args:
file_path (str or Path): Path to the file.
metadata_dir (str or Path, optional): Directory to search for the matching XMP metadata file.
whatif (bool, optional): If True, reports what changes would be made without making them.
Returns:
Path: The new file path with the prepended DateCreated attribute.
"""
try:
file_path = Path(file_path)
metadata_dir = Path(metadata_dir) if metadata_dir else None
# Determine the XMP metadata file path
if metadata_dir:
filename_without_ext = file_path.stem
xmp_file_path = metadata_dir / f"{filename_without_ext}.xmp"
else:
xmp_file_path = file_path
# Parse the XMP metadata
if not xmp_file_path.exists():
raise FileNotFoundError(f"XMP metadata file not found: {xmp_file_path}")
with xmp_file_path.open('r', encoding='utf-8') as file:
content = file.read()
# Parse XML and find DateCreated
root = ET.fromstring(content)
namespace = {'photoshop': 'http://ns.adobe.com/photoshop/1.0/'}
date_created = root.find('.//photoshop:DateCreated', namespace)
if date_created is None or not date_created.text:
raise ValueError("DateCreated attribute not found in the XMP metadata.")
# Extract and sanitize the date string
sanitized_date = re.sub(r'[^A-Za-z0-9_-]', '_', date_created.text)
# Prepend the sanitized date to the filename
new_filename = f"{sanitized_date}_{file_path.name}"
new_file_path = file_path.parent / new_filename
if whatif:
print(f"[WHATIF] Would rename '{file_path}' to '{new_file_path}'")
else:
# Rename the file
file_path.rename(new_file_path)
return new_file_path
except Exception as e:
raise RuntimeError(f"Error processing file '{file_path}': {e}")
# Example usage:
# file_path = "/path/to/your/image.jpg"
# metadata_dir = "/path/to/your/metadata"
# new_file_path = prepend_date_to_filename(file_path, metadata_dir=metadata_dir, whatif=True)
# print(f"File renamed to: {new_file_path}")
file_path = "/Users/davidmidlo/Library/Mobile Documents/iCloud~md~obsidian/Documents/iOS-Vault/Desk/AvarTec Records/Clients/Dorglass/Other/25-01-03/IMG_2293.png"
metadata_dir = "/Users/davidmidlo/Library/Mobile Documents/iCloud~md~obsidian/Documents/iOS-Vault/Desk/Unsorted Screenshots and Video/Raw and Metadata Files"
new_file_path = prepend_date_to_filename(file_path, metadata_dir=metadata_dir, whatif=False)
print(f"File renamed to: {new_file_path}")