#!/usr/bin/env python import glob import os import sys import shutil from datetime import datetime MOUNT_DIR = '/mnt/dcim' DST_DIR = '/home/lorien/photos' def find_source(): dirs = glob.glob(MOUNT_DIR + '/*_pana') pairs = [(int(x.split('/')[-1].split('_')[0]), x) for x in dirs] last = sorted(pairs, key=lambda x: x[0], reverse=True)[0] return last[1] def process_source(source): dates = {} for fname in glob.glob(source + '/*.jpg'): ctime = datetime.fromtimestamp(os.stat(fname).st_ctime) dates.setdefault(ctime.date(), []).append(fname) for day, fnames in sorted(dates.iteritems(), key=lambda x: x[0]): dst_dir = DST_DIR + '/%d/%02d/%02d' % (day.year, day.month, day.day) print 'Processing %s. Directory: %s' % (day.strftime('%d %b %Y'), dst_dir) if not os.path.exists(dst_dir): os.makedirs(dst_dir) for fname in fnames: img_fname = os.path.basename(fname) dst_path = os.path.join(dst_dir, img_fname) if not os.path.exists(dst_path): sys.stdout.write('+') sys.stdout.flush() shutil.copy(fname, dst_path) else: sys.stdout.write('e') sys.stdout.flush() print if __name__ == '__main__': source = find_source() process_source(source)