-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows.py
69 lines (61 loc) · 2.6 KB
/
windows.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
import os
import json
import sys
from datetime import datetime, timezone
import win32file
import win32con
def modify_time(file_path, timestamp):
dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
handle = win32file.CreateFile(file_path, win32file.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
win32file.SetFileTime(handle, dt, None, None)
handle.close()
def get_timestamp(data):
if 'photoTakenTime' in data and 'timestamp' in data['photoTakenTime']:
return int(data['photoTakenTime']['timestamp'])
elif 'created' in data:
dt = datetime.strptime(data['created'], "%Y-%m-%dT%H:%M:%S.%fZ")
return int(dt.timestamp())
else:
raise KeyError("No valid timestamp found in JSON data")
def process_file(directory, filename):
base_filename = os.path.splitext(filename)[-2]
json_paths = [
os.path.join(directory, f"{filename}.json"),
os.path.join(directory, f"{filename}-info.json"),
os.path.join(directory, f"{base_filename}.json"),
os.path.join(directory, f"{base_filename}-info.json")
]
jpg_path = os.path.join(directory, filename)
for json_path in json_paths:
if os.path.exists(json_path):
try:
with open(json_path, 'r') as json_file:
data = json.load(json_file)
timestamp = get_timestamp(data)
modify_time(jpg_path, timestamp)
print(f"Updated {jpg_path} with timestamp {timestamp}")
os.remove(json_path)
print(f"Removed {json_path}")
break
except Exception as e:
print(f"Failed to process {json_path}: {e}")
def process_directory(directory, recursive=False):
for root, _, files in os.walk(directory):
for filename in files:
try:
if not filename.lower().endswith('.json'):
process_file(root, filename)
except Exception as e:
print(f"Failed to process {filename} in {root}: {e}")
if not recursive:
break
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python windows.py <directory_path1> <directory_path2> ...")
else:
recursive = input("Do you want to search all subdirectories? (y/n): ").strip().lower() == 'y'
for directory in sys.argv[1:]:
if os.path.isdir(directory):
process_directory(directory, recursive)
else:
print(f"The path '{directory}' is not a valid directory.")