Commits

Gregory Petukhov  committed 92db8f6

Initial import

  • Participants

Comments (0)

Files changed (2)

+syntax:glob
+
+# Common
+*.pyc
+*.pyo
+*.swp
+*.swo
+*.orig

File photosync.py

+#!/usr/bin/env python
+import glob
+import os
+import sys
+import shutil
+from datetime import datetime
+from PIL import Image
+from PIL.ExifTags import TAGS
+
+MOUNT_DIR = '/mnt/dcim'
+DST_DIR = '/home/lorien/photos'
+
+def get_exif(fn):
+    ret = {}
+    i = Image.open(fn)
+    info = i._getexif()
+    for tag, value in info.items():
+        decoded = TAGS.get(tag, tag)
+        ret[decoded] = value
+    return ret
+
+def find_sources():
+    dirs = glob.glob(MOUNT_DIR + '/*_pana')
+    return dirs
+    #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)
+        exif = get_exif(fname)
+        ctime = datetime.strptime(exif['DateTime'], '%Y:%m:%d %H:%M:%S')
+        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()
+    for source in find_sources():
+        process_source(source)