Guest User

Untitled

a guest
Jun 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #!/usr/bin/python
  2. # coding: utf8
  3.  
  4. import io
  5. import re
  6. import time
  7. import shutil
  8. import os.path
  9. import hashlib
  10. import argparse
  11. import mimetypes
  12. import exifread
  13. from datetime import datetime
  14.  
  15. class PreparePhotos:
  16.  
  17. def __init__(self, target_folder = ".", destination_folder = "./output"):
  18. self.target_folder = target_folder
  19. self.destination_folder = destination_folder
  20.  
  21. def sync(self, args):
  22. for directory, _, files in os.walk(self.target_folder):
  23. for filename in files:
  24. path = os.path.join(directory, filename)
  25. mimetype, _ = mimetypes.guess_type(path)
  26. name, ext = os.path.splitext(path)
  27. if (mimetype and mimetype.startswith('image/')) or ext.upper() == '.HEIC':
  28. tags = exifread.process_file(open(path, 'rb'), details=False)
  29. self.move(path, tags)
  30. print 'Done'
  31.  
  32. def md5(self, path):
  33. hash_md5 = hashlib.md5()
  34. with open(path, 'rb') as f:
  35. for chunk in iter(lambda: f.read(4096), b""):
  36. hash_md5.update(chunk)
  37. return hash_md5.hexdigest()
  38.  
  39. def move(self, path, tags):
  40. date = None
  41. if 'EXIF DateTimeOriginal' in tags.keys():
  42. # 2013:06:01 18:26:49
  43. date = str(tags['EXIF DateTimeOriginal'])
  44. try:
  45. date = datetime.strptime(date, '%Y:%m:%d %H:%M:%S')
  46. except ValueError:
  47. date = datetime.fromtimestamp(os.stat(path).st_birthtime)
  48. else:
  49. date = datetime.fromtimestamp(os.stat(path).st_birthtime)
  50. model = ''
  51. if 'Image Model' in tags.keys():
  52. model = '_' + str(tags['Image Model']).replace(' ', '_')
  53.  
  54. filename, ext = os.path.splitext(path)
  55. yearstr = date.strftime('%Y')
  56. datestr = date.strftime('%Y-%m-%d')
  57. source_dir = os.path.join(self.destination_folder, yearstr, datestr)
  58. print 'Moving', path, '...'
  59. if not os.path.isdir(source_dir):
  60. print 'Creating', source_dir, '...'
  61. os.makedirs(source_dir)
  62. source_name = date.strftime('%Y%m%d-%H%M%S') + model
  63. source_fullpath = os.path.join(source_dir, source_name + ext)
  64. if os.path.exists(source_fullpath):
  65. if self.md5(path) == self.md5(source_fullpath):
  66. print 'Skipping', source_fullpath, '...'
  67. return source_fullpath
  68. postfix = 1
  69. while True:
  70. new_fullpath = os.path.join(source_dir, source_name + '-' + str(postfix) + ext)
  71. if os.path.exists(new_fullpath) and self.md5(path) == self.md5(new_fullpath):
  72. print 'Skipping', new_fullpath, '...'
  73. return new_fullpath
  74. if not os.path.exists(new_fullpath):
  75. source_fullpath = new_fullpath
  76. break
  77. postfix += 1
  78.  
  79. utime = time.mktime(date.timetuple())
  80. shutil.copy(path, source_fullpath)
  81. os.utime(source_fullpath, (utime, utime))
  82. print '->', source_fullpath
  83. return source_fullpath
  84.  
  85. parser = argparse.ArgumentParser(description="Foo")
  86. parser.add_argument("--quite", help="quite")
  87. parser.add_argument("target_folder", help="target folder")
  88. parser.add_argument("destination_folder", help="destination folder")
  89.  
  90. args = parser.parse_args()
  91.  
  92. tf = args.target_folder
  93. df = args.destination_folder
  94.  
  95. #tf = "photos"
  96. #df = "output"
  97.  
  98. pp = PreparePhotos(target_folder = tf, destination_folder = df)
  99. pp.sync(args)
Add Comment
Please, Sign In to add comment