#!/usr/bin/python # Pull photos off my digital camera and stick them in the right directory, # timestamped and numbered. # Adam Sampson import sys, os, time, subprocess from offog import run_command, ensure_dir, temporary_dir, push_dir images_dir = os.getenv("HOME") + "/Work/photos" temp_dir = "/var/tmp/camera" def main(): output_dir = "%s/%s" % (images_dir, time.strftime("%Y%m%d")) ensure_dir(output_dir) temp_dir = "%s/new-%d" % (output_dir, os.getpid()) with temporary_dir(temp_dir): with push_dir(temp_dir): run_command(["gphoto2", "--get-all-files"]) for fn in os.listdir(temp_dir): input_fn = temp_dir + "/" + fn dot = fn.rfind(".") if dot == -1: ext = "" else: ext = fn[dot + 1:].lower() if ext in ("jpg", "jpeg"): ftype = "photo" elif ext in ("avi", "mpg", "mp4"): ftype = "video" else: die("Unrecognised extension: ", fn) mtime = time.gmtime(os.stat(input_fn).st_mtime) output_fn = ("%s/%s-%s-%s-%s" % (output_dir, ftype, os.getenv("LOGNAME"), time.strftime("%Y%m%dT%H%M%S", mtime), fn)) print "%s -> %s" % (fn, output_fn) os.rename(input_fn, output_fn) run_command(["gphoto2", "--delete-all-files", "--recurse"]) if __name__ == "__main__": main()