Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import os, shutil
  5. import subprocess
  6. import os.path
  7. from datetime import datetime
  8.  
  9. ######################## Functions #########################
  10.  
  11. def photoDate(f):
  12. "Return the date/time on which the given photo was taken."
  13.  
  14. cDate = subprocess.check_output(['sips', '-g', 'creation', f])
  15. cDate = cDate.split('\n')[1].lstrip().split(': ')[1]
  16. return datetime.strptime(cDate, "%Y:%m:%d %H:%M:%S")
  17.  
  18.  
  19. ###################### Main program ########################
  20.  
  21. # Where the photos are and where they're going.
  22. sourceDir = os.environ['HOME'] + '/Pictures/iPhone Incoming'
  23. destDir = os.environ['HOME'] + '/Pictures/iPhone'
  24. errorDir = destDir + '/Unsorted/'
  25.  
  26. # The format for the new file names.
  27. fmt = "%Y-%m-%d %H-%M-%S"
  28.  
  29. # The problem files.
  30. problems = []
  31.  
  32. # Get all the JPEGs in the source folder.
  33. photos = os.listdir(sourceDir)
  34. photos = [ x for x in photos if x[-4:] == '.jpg' or x[-4:] == '.JPG' ]
  35.  
  36. # Prepare to output as processing occurs
  37. lastMonth = 0
  38. lastYear = 0
  39.  
  40. # Create the destination folder if necessary
  41. if not os.path.exists(destDir):
  42. os.makedirs(destDir)
  43. if not os.path.exists(errorDir):
  44. os.makedirs(errorDir)
  45.  
  46. # Copy photos into year and month subfolders. Name the copies according to
  47. # their timestamps. If more than one photo has the same timestamp, add
  48. # suffixes 'a', 'b', etc. to the names.
  49. for photo in photos:
  50. # print "Processing %s..." % photo
  51. original = sourceDir + '/' + photo
  52. suffix = 'a'
  53. try:
  54. pDate = photoDate(original)
  55. yr = pDate.year
  56. mo = pDate.month
  57.  
  58. if not lastYear == yr or not lastMonth == mo:
  59. sys.stdout.write('\nProcessing %04d-%02d...' % (yr, mo))
  60. lastMonth = mo
  61. lastYear = yr
  62. else:
  63. sys.stdout.write('.')
  64.  
  65. newname = pDate.strftime(fmt)
  66. thisDestDir = destDir + '/%04d/%04d-%02d' % (yr, yr, mo)
  67. if not os.path.exists(thisDestDir):
  68. os.makedirs(thisDestDir)
  69.  
  70. duplicate = thisDestDir + '/%s.jpg' % (newname)
  71. while os.path.exists(duplicate):
  72. newname = pDate.strftime(fmt) + suffix
  73. duplicate = destDir + '/%04d/%02d/%s.jpg' % (yr, mo, newname)
  74. suffix = chr(ord(suffix) + 1)
  75. shutil.copy2(original, duplicate)
  76. except Exception:
  77. shutil.copy2(original, errorDir + photo)
  78. problems.append(photo)
  79. except:
  80. sys.exit("Execution stopped.")
  81.  
  82. # Report the problem files, if any.
  83. if len(problems) > 0:
  84. print "\nProblem files:"
  85. print "\n".join(problems)
  86. print "These can be found in: %s" % errorDir
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement