Created
October 11, 2016 22:40
-
-
Save DrLulz/9b782da4f3ccce6620e5b4f0976e4d61 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/local/bin/python | |
import os, pickle, glob, shutil, subprocess, traceback, errno | |
from datetime import datetime as dt | |
TIME = dt.utcnow().strftime('%f') | |
ABS_DIR = os.path.dirname(os.path.realpath(__file__)) | |
TMP_DIR = os.path.join(ABS_DIR, TIME) | |
TXT_FILE = os.path.join(ABS_DIR, 'imported_counter') | |
IMG_DIR = '/Volumes/a6000/DCIM/' | |
VID_DIR = '/Volumes/a6000/PRIVATE/M4ROOT/CLIP/' | |
MAC_HD = 'Mac HD:Users:DrLulz:Pictures:Photos' | |
PHOTOS = os.path.join('/Users/DrLulz/Pictures/Photos/', TIME) | |
FLICKR = '/Users/DrLulz/Pictures/Flickr/' | |
def imported(): | |
'''Load previous imports''' | |
try: | |
with open(TXT_FILE, 'rb') as _file_: | |
_list_ = [] | |
while True: | |
try: | |
_list_.append(pickle.load(_file_)) | |
except EOFError: | |
break | |
except: | |
_list_ = [] | |
return _list_ | |
def new_files(): | |
_list_ = [] | |
# Get all directories under image directory, exclude IMG_DIR | |
img_dirs = [x[0] for x in os.walk(IMG_DIR) if x[0] != IMG_DIR] | |
# IMAGES | |
# Go through directories, add full path to image to _list_ | |
for directory in img_dirs: | |
images = glob.glob(os.path.join(directory, '*.*')) | |
if images: | |
_list_ += images | |
# VIDEOS | |
videos = [os.path.join(VID_DIR, x) for x in os.listdir(VID_DIR) if not x.endswith('.XML') ] | |
_list_ += videos | |
return _list_ | |
def rename(FILE_LIST): | |
# http://strftime.org | |
if not FILE_LIST: | |
return | |
old_new = [] | |
for OLD in FILE_LIST: | |
DIR, FILE = os.path.split(OLD) | |
NAME, EXT = os.path.splitext(FILE) | |
DATE = dt.fromtimestamp(os.stat(OLD).st_birthtime) | |
DATE_TIME = DATE.strftime('%Y-%m-%d--%H-%M-%S') | |
SETFILE = DATE.strftime('%m/%d/%Y %H:%M:%S') | |
NEW = '{}/{}{}'.format(PHOTOS, DATE_TIME, EXT) | |
if any([item for item in old_new if item[1] == NEW]): | |
NEW = '{}/{}-{}{}'.format(PHOTOS, DATE_TIME, '(2)', EXT) | |
old_new.append((OLD, NEW, SETFILE)) | |
else: | |
old_new.append((OLD, NEW, SETFILE)) | |
return old_new | |
def PhotosDir(PATHS): | |
for path in PATHS: | |
shutil.copyfile(path[0], path[1]) | |
print 'Copied {}'.format(path[1]) | |
try: | |
arglist = ['/usr/bin/SetFile', '-md', path[2], path[1]] | |
sp = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
except: | |
print "Traceback:\n%s"%traceback.format_exc() | |
stdout, stderr = sp.communicate() | |
if stderr: | |
print "stderr:\n'%s'" % stderr | |
with open(TXT_FILE, 'ab') as pickle_out: | |
#outfile = open(TXT_FILE, 'wb') | |
pickle.dump(path[0], pickle_out) | |
''' | |
Need to first copy to Photos subfolder, then convert to Alfred temp folder, then correct time of temp file, then copy contents to Flickr Folder | |
''' | |
def PhotosApp(): | |
scpt = ''' | |
on run {_dir_} | |
tell application "Finder" to set theFiles to files of (folder _dir_) as alias list | |
tell application "Photos" | |
activate | |
delay 5 | |
import theFiles | |
end tell | |
end run''' | |
args = ['{}:{}'.format(MAC_HD, TIME)] | |
print args | |
try: | |
sp = subprocess.Popen(['osascript', '-'] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
except: | |
print "Traceback:\n%s"%traceback.format_exc() | |
stdout, stderr = sp.communicate(scpt) | |
if stderr: | |
print "stderr:\n'%s'" % stderr | |
def Convert(PATHS): | |
if not os.path.isdir(TMP_DIR): | |
os.mkdir(TMP_DIR) | |
for path in PATHS: | |
if path[1].endswith('ARW'): | |
ARW = path[1] | |
DIR, FILE = os.path.split(ARW) | |
NAME, EXT = os.path.splitext(FILE) | |
JPG = os.path.join(TMP_DIR, '{}.{}'.format(NAME, 'jpg')) | |
FLK = os.path.join(FLICKR, '{}.{}'.format(NAME, 'jpg')) | |
# -------------------------------------------- | |
'''Convert RAW to JPG''' | |
try: | |
arglist = ['/usr/bin/sips', '-s', 'format', 'jpeg', ARW, '-s', 'formatOptions', 'best', '--out', JPG] | |
sp = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
except: | |
print "Traceback:\n%s"%traceback.format_exc() | |
stdout, stderr = sp.communicate() | |
if stderr: | |
print "stderr:\n'%s'" % stderr | |
# -------------------------------------------- | |
'''Change creation and modified timestamp''' | |
try: | |
arglist = ['/usr/bin/SetFile', '-md', path[2], JPG] | |
sp = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
except: | |
print "Traceback:\n%s"%traceback.format_exc() | |
stdout, stderr = sp.communicate() | |
if stderr: | |
print "stderr:\n'%s'" % stderr | |
# -------------------------------------------- | |
'''Move to Flickr folder''' | |
shutil.move(JPG, FLK) | |
shutil.rmtree(TMP_DIR) | |
def main(): | |
os.system('open /Applications/Photos.app') | |
if not os.path.isdir(PHOTOS): | |
os.mkdir(PHOTOS) | |
'''Find difference between lists''' | |
import_list = list(set(new_files()).symmetric_difference(set(imported()))) | |
import_path = rename(import_list) | |
PhotosDir(import_path) | |
PhotosApp() | |
Convert(import_path) | |
os.system('diskutil unmount /Volumes/PMHOME') | |
os.system('diskutil unmount /Volumes/a6000') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment