def test_datetime(): # Data from exif metadata photo_path = str(Path(__file__).parent / 'photos' / 'snow.jpg') parsed_datetime = get_datetime(photo_path) assert parsed_datetime.year == 2018 assert parsed_datetime.isoformat() == '2018-02-28T07:16:25+00:00' # Date extraction from filename photo_path = str(Path(__file__).parent / 'photos' / 'snow-1999-12-31.jpg') parsed_datetime = get_datetime(photo_path) assert parsed_datetime.isoformat() == '1999-12-31T00:00:00' photo_path = str(Path(__file__).parent / 'photos' / 'snow-20100603.jpg') parsed_datetime = get_datetime(photo_path) assert parsed_datetime.isoformat() == '2010-06-03T00:00:00' # Date is parseable but has slashes instead of colons photo_path = str(Path(__file__).parent / 'photos' / 'bad_date.jpg') parsed_datetime = get_datetime(photo_path) assert parsed_datetime.year == 2000 assert parsed_datetime.isoformat() == '2000-01-01T00:00:00+00:00' # Some of the date digits are the letter X so fall back to file creation date photo_path = str(Path(__file__).parent / 'photos' / 'unreadable_date.jpg') parsed_datetime = get_datetime(photo_path) assert parsed_datetime.isoformat() == '2021-09-02T10:43:49.739248+00:00'
def test_datetime(): # Data from exif metadata photo_path = str(Path(__file__).parent / 'photos' / 'snow.jpg') parsed_datetime = get_datetime(photo_path) assert parsed_datetime.year == 2018 assert parsed_datetime.isoformat() == '2018-02-28T07:16:25+00:00' # Date extraction from filename photo_path = str(Path(__file__).parent / 'photos' / 'snow-1999-12-31.jpg') parsed_datetime = get_datetime(photo_path) assert parsed_datetime.isoformat() == '1999-12-31T00:00:00' photo_path = str(Path(__file__).parent / 'photos' / 'snow-20100603.jpg') parsed_datetime = get_datetime(photo_path) assert parsed_datetime.isoformat() == '2010-06-03T00:00:00'
def import_photos_from_dir(orig, move=False): imported = 0 were_duplicates = 0 were_bad = 0 for r, d, f in os.walk(orig): if SYNOLOGY_THUMBNAILS_DIR_NAME in r: continue for fn in sorted(f): filepath = os.path.join(r, fn) dest = determine_destination(filepath) if blacklisted_type(fn): # Blacklisted type were_bad += 1 elif not dest: # No filters match this file type pass else: t = get_datetime(filepath) if t: destpath = '%02d/%02d/%02d' % (t.year, t.month, t.day) destpath = os.path.join(dest, destpath) mkdir_p(destpath) destpath = os.path.join(destpath, fn) if filepath == destpath: # File is already in the right place so be very careful not to do anything like delete it pass elif not os.path.exists(destpath): if move: shutil.move(filepath, destpath) else: shutil.copyfile(filepath, destpath) record_photo(destpath) imported += 1 print('IMPORTED {} -> {}'.format(filepath, destpath)) else: print('PATH EXISTS {} -> {}'.format(filepath, destpath)) same = determine_same_file(filepath, destpath) print('PHOTO IS THE SAME') if same: if move: os.remove(filepath) were_duplicates += 1 print('DELETED FROM SOURCE') else: print('NEED TO IMPORT UNDER DIFFERENT NAME') exit(1) destpath = find_new_file_name(destpath) shutil.move(filepath, destpath) record_photo(destpath) imported += 1 # print 'IMPORTED {} -> {}'.format(filepath, destpath) else: print('ERROR READING DATE: {}'.format(filepath)) were_bad += 1 if imported or were_duplicates: print('\n{} PHOTOS IMPORTED\n{} WERE DUPLICATES\n{} WERE BAD'.format(imported, were_duplicates, were_bad))