Created
October 17, 2016 23:44
-
-
Save mludvig/7ff30aae609897be29750094e81be626 to your computer and use it in GitHub Desktop.
Rename images in DropBox folder to facilitate sorting by date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Rename files in DropBox folder to allow easy sorting by date. | |
# If a JPG files contains EXIF data recognised by DropBox we | |
# rename it from e.g. "IMG_1234.JPG" to "2016-01-01 10:20:30 IMG_1234.JPG" | |
# Requires access token obtained from DropBox API page. | |
# By Michael Ludvig <mludvig@logix.net.nz> | |
# License Public Domain (it's too simple a script to deserve any licensing protection :) | |
from __future__ import print_function | |
import os | |
import sys | |
import re | |
import dropbox | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--folder", type=str, default='', help="Dropbox folder name to process, e.g. '/Family Photos/Sunday Trip'") | |
parser.add_argument("--access-token", type=str, required=True, help="Dropbox API access token. Can be a file containing only the token and nothing else.") | |
parser.add_argument("--do-it", action="store_true", help="Rename the files as suggested. Otherwise only print what would be done.") | |
args = parser.parse_args() | |
if os.access(args.access_token, os.R_OK): | |
access_token = open(args.access_token, 'r').readline().strip() | |
else: | |
access_token = args.access_token | |
dbx = dropbox.Dropbox(access_token) | |
files = dbx.files_list_folder(args.folder, include_media_info=True) | |
for item in files.entries: | |
ts = None | |
if item.__class__ == dropbox.files.FolderMetadata: | |
print("%-25s %s" % ("DIR", item.path_display)) | |
continue | |
if item.__class__ != dropbox.files.FileMetadata: | |
print("%-25s %s" % ("UNKNOWN", item.path_display)) | |
continue | |
dirname = "/".join(item.path_display.split('/')[:-1]) | |
basename = item.path_display.split('/')[-1] | |
if re.match('[12]\d{3}-\d{2}-\d{2}', basename): | |
# Probably starts with a timestamp: 2016-01-01 ... | |
print("%-25s %s" % ("NO-CHANGE", item.path_display)) | |
continue | |
if not item.media_info or not item.media_info.is_metadata(): | |
print("%-25s %s" % ("NO-TIMESTAMP", item.path_display)) | |
continue | |
md = item.media_info.get_metadata() | |
if not md.time_taken: | |
print("%-25s %s" % ("NO-TIMESTAMP", item.path_display)) | |
continue | |
ts = md.time_taken.strftime('%Y-%m-%d %H-%M-%S') | |
new_name = "%s/%s %s" % (dirname, ts, basename) | |
if not args.do_it: | |
print('%-25s %s -> %s' % ("RENAME", item.path_display, new_name)) | |
else: | |
print('%-25s %s -> %s' % ("RENAMING", item.path_display, new_name), end=" ") | |
sys.stdout.flush() | |
dbx.files_move(item.path_display, new_name) | |
print("[done]") | |
if not args.do_it: | |
print("\nNo changes have been made. Use --do-it to actually rename the files.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment