Skip to content

Instantly share code, notes, and snippets.

@dsteinberger
Last active October 20, 2020 11:29
Show Gist options
  • Save dsteinberger/2cc610b01605477b5d606bc655d21e2a to your computer and use it in GitHub Desktop.
Save dsteinberger/2cc610b01605477b5d606bc655d21e2a to your computer and use it in GitHub Desktop.
Update created and updated date from data from Google's takeout from json file and delete json file.
"""
Update created and updated system date from data from Google's takeout from json file
and delete json file.
Change the PATH_TO_SCAN and execute this script.
Sometimes we have missing files from takeout (see error logs)
"""
import glob
import os
import json
import subprocess
from datetime import datetime
PATH_TO_SCAN = "/Users/david/Documents/migration/tak/"
def search_pattern_files(path):
txtfiles = []
for file in glob.glob(path):
txtfiles.append(file)
return txtfiles
def clean_title(image_name):
# replace ' by _
return image_name.replace('\u0027', '_').replace('%', '_')
def split_title(image_name):
# Split and cut
name_splits = image_name.split('.')
extension = name_splits[len(name_splits)-1:][0]
name = ".".join(name_splits[:len(name_splits)-1])
name = name[:47]
return f"{name}.{extension}"
def update_image_date_system(dir_path, image_name, date):
image_name = clean_title(image_name)
date = datetime.fromtimestamp(int(date))
print(f"Directory: {dir_path}, image: {image_name}, for date: {date}")
try:
subprocess.check_call(["ExifTool", "-r",
f"-FileCreateDate={date}",
f"-FileModifyDate={date}",
f"{dir_path}{image_name}"])
except subprocess.CalledProcessError:
# google have 47 chars max image name
image_clean_name = split_title(image_name)
print(f"### RETRY ### Directory: {dir_path}, image: {image_clean_name}, for date: {date}")
subprocess.check_call(["ExifTool", "-r",
f"-FileCreateDate={date}",
f"-FileModifyDate={date}",
f"{dir_path}{image_clean_name}"])
if __name__ == '__main__':
errors = []
count_success = 0
for root, dirs, files in os.walk(PATH_TO_SCAN):
print(f"Root: {root} with {len(dirs)} folders")
for _dir in dirs:
path_dir = f'{root}{_dir}/'
json_files_path = search_pattern_files(f"{path_dir}*.json")
for json_path in json_files_path:
with open(json_path) as json_file:
data = json.load(json_file)
print(f"Json PATH: {json_path}")
photo_taken_time = data.get('photoTakenTime') or None
if not photo_taken_time:
print('Json invalid - stop')
print(f'****** DELETE ****** {json_path}')
os.remove(json_path)
continue
image_name_to_update = data['title']
timestamp_to_replace = photo_taken_time['timestamp']
try:
update_image_date_system(path_dir,
image_name_to_update,
timestamp_to_replace)
except subprocess.CalledProcessError as e:
errors.append(f"ERROR {e} - "
f"json file: {json_path}")
else:
count_success += 1
print(f'****** DELETE ****** {json_path}')
os.remove(json_path)
print("###################")
print("-------------------")
for er in errors:
print(er)
print("-------------------")
print(f'SUCCESS: {count_success} ERROR: {len(errors)}')
print("###################")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment