def test_get_dt_captured(self): self.assertEqual( "1992:03:01 00:00:00", ImageUtils.get_original_date( "/Users/paulottley/Desktop/SortSource/Ottley_Home_Movies_19920301_3.mp4" ), "1900 check") self.assertEqual( "2017:04:14 00:51:40", ImageUtils.get_original_date( "/Users/paulottley/Desktop/SortSource/IMG_4739.JPG"), "Happy Path") self.assertEqual( "0000:00:00 00:00:00", ImageUtils.get_original_date( "/Users/paulottley/Desktop/SortSource/images_images_09-September_New_Malibu.JPG" ), "Malibu check") self.assertEqual( "2013-05-17 18:04:48", ImageUtils.get_original_date( "/Users/paulottley/Desktop/SortSource/Family_VID_20130517_130427.3gp" ), "3gp check") self.assertEqual( "2012-09-25 19:40:24", ImageUtils.get_original_date( "/Users/paulottley/Desktop/SortSource/videos_videos_Family_IMG_0063.MOV" ), "MOV check")
def test_remove_false_timestamp(self): test_name = "20150803-042006_20150505_221117_LLS.jpg" self.assertEqual("042006_20150505_221117_LLS.jpg", ImageUtils.remove_false_datestamp(test_name)) test_name = "042006_20150803-20150505_221117_LLS.jpg" self.assertEqual("042006_20150803-20150505_221117_LLS.jpg", ImageUtils.remove_false_datestamp(test_name)) test_name = "20150803-042006_20150505-221117_LLS.jpg" self.assertEqual("042006_20150505-221117_LLS.jpg", ImageUtils.remove_false_datestamp(test_name))
def test_set_date(self): self.assertEqual( "0000:00:00 00:00:00", ImageUtils.get_dt_captured( "/Users/paulottley/Desktop/SortSource/Family_VID_20130517_130053.mp4" ), "mp4 check") ImageUtils.set_date( "/Users/paulottley/Desktop/SortSource/Family_VID_20130517_130218.mp4", 2013, 5, 17) self.assertEqual( "2013:05:17 18:04:48", ImageUtils.get_dt_captured( "/Users/paulottley/Desktop/SortSource/Family_VID_20130517_130218.mp4" ), "mp4 check")
def test_get_dt_captured_split(self): dt = ImageUtils.get_dt_captured_split("2014:05:23 10:23:32") self.assertEqual(2014, dt.year) self.assertEqual(5, dt.month) dt2 = ImageUtils.get_dt_captured_split("2014:05:23") self.assertEqual(2014, dt2.year) self.assertEqual(5, dt2.month) dt3 = ImageUtils.get_dt_captured_split("2012-06-06 18:47:57") self.assertEqual(2012, dt3.year) self.assertEqual(6, dt3.month) dt3 = ImageUtils.get_dt_captured_split("0000:00:00 00:00:00") self.assertEqual(1900, dt3.year) self.assertEqual(1, dt3.month) """
def walk_dir(dir_to_walk, tgt_dir): # Print time started ts = time.time() start_time = datetime.datetime.fromtimestamp(ts).strftime( '%Y-%m-%d %H:%M:%S') print("Walk Dir started at {0}:".format(start_time)) # Build empty container for the files all_files = [] if os.path.isdir(dir_to_walk): # Walk the directory and count the files for root, dirs, files in os.walk(dir_to_walk): for file in files: # Step 1: Build the file object with name and location fs_file = FSfile() fs_file.set_filename(file) fs_file.set_src_dir(root + SEPARATOR) fs_file.set_tgt_dir(tgt_dir + SEPARATOR) # Step 2: Add metadata of size and date to object fs_file.set_size( FileUtils.get_file_size(fs_file.get_full_path())) fs_file.set_date_taken( ImageUtils.get_original_date(fs_file.get_full_path())) # Step 3: Determine file type and tag current_file = NavUtil.sort_file_type(fs_file) # Step 4: Add file to collection all_files.append(current_file) # Print time ended ts2 = time.time() end_time = datetime.datetime.fromtimestamp(ts2).strftime( '%Y-%m-%d %H:%M:%S') print("Walk Dir completed at {0} with {1} files collected.".format( end_time, len(all_files))) return all_files
# Find mp4 file of same name # Update date count = 0 zero_count = 0 for file in all_files1: count += 1 full_filename = file.get_full_path() path, fn = os.path.split(full_filename) date_taken = file.get_date_taken() if "0000" in date_taken: zero_count += 1 year, month, day, dt = ImageUtils.get_dt_captured_split() creation_date_str = "{0}-{1}-{2}".format(year, month, day) new_file = fn.replace("images_", "") new_file = new_file.replace("_.", ".") new_file = new_file.replace("~", "") new_file = new_file.replace("....", ".") new_file = "{0}_{1}".format(creation_date_str, fn) if FileUtils.does_file_exist(new_file, path) is not True: print(path + new_file) os.rename(full_filename, path + new_file) else: print(new_file + " file exists") print("Filename: {0} date: {1}".format(fn, date_taken))
if "." is file[0]: print("Moving..." + full_filename) FileUtils.move_file(full_filename, r"/Volumes/Elements2TB/Backups/Trash/", file) dot_file_count += 1 print("Number of dot files: {0}".format(dot_file_count)) # for file in all_files1: for root, dirs, files in os.walk(STR_DIR1): for file in files: count += 1 tgt_dir = root + os.sep filename = file full_path = root + os.sep + file orig_date = ImageUtils.get_original_date(full_path) line = "" dt_frm_embed = orig_date dt_frm_filename = ImageUtils.get_dt_from_name(filename) if None is dt_frm_filename: missing_filename_dt += 1 line += "\nMissing Date in Filename: {0}".format(full_path) elif None is dt_frm_embed: missing_embed_dt += 1 line += "\nMissing Date in Embed: {0}".format(full_path) elif "0000" in orig_date or "2000-01-01 00:00:00" == orig_date: zero_count += 1 line += "\nZero Date in Filename: {0}".format(full_path)
from app import Rules # STR_DIR = r"/Users/paulottley/Desktop/SortSource" STR_DIR = r"/Volumes/Elements2TB/Backups/Pictures/images" TGT_DIR = r"/Volumes/MyBook2TB/Backups/SortTarget" for root, dirs, files in os.walk(STR_DIR): for file in files: target_dir = root tgt_folder = FileUtils.get_file_category(file) try: dt, str_dt = ImageUtils.get_dt_from_name(file) if dt is None: print("Zero or None: " + file) target_dir = "{0}{1}{2}{3}".format( root, os.sep, "no_date", os.sep) else: target_dir = "{0}{1}{2}{3}{4}{5}".format( root + os.sep, tgt_folder + os.sep, dt.year, os.sep, dt.month, os.sep)
all_files1 = NavUtil.walk_dir(STR_DIR1, STR_DIR1) # Check date of original file # Parse date # Find mp4 file of same name # Update date for file in all_files1: print("Before Filename: {0} date: {1} filename parser date: {2}".format( file.get_filename(), file.get_date_taken(), ImageUtils.get_dt_from_parser(file.get_full_path()))) file_type = FileUtils.get_file_type(file.get_filename()) date = ImageUtils.get_dt_from_name(file.get_filename()) """ if file_type != "mp4" and file.get_type() != Rules.get_oth_tag(): full_path = file.get_full_path() mp4_full_path = full_path.replace("." + file_type, ".mp4") # mp4_full_path = full_path.replace("." + file_type.swapcase(), ".mp4") print("Date: {0} Filename: {1}".format(date, mp4_full_path)) """ full_filename = file.get_full_path() pdate = ImageUtils.get_dt_from_parser(file.get_full_path()) dt = ImageUtils.get_dt_captured_split(date) if int(pdate[:4]) != int(date[:4]): ImageUtils.set_date(file.get_full_path(), date, dt.year, dt.month, dt.day)
start_time = datetime.datetime.now() log_file.write("Correct Date started at {0} \n".format(start_time)) for root, dirs, files in os.walk(STR_DIR1): for file in files: full_filename = root + os.sep + file if "." is file[0]: print("Moving..." + full_filename) FileUtils.move_file(full_filename, r"/Volumes/MyBook2TB/Backups/Trash/", file) else: date_taken = ImageUtils.get_dt_from_name(file)[0] orig_datetime = ImageUtils.get_original_date(full_filename)[0] if orig_datetime is not None: orig_date = orig_datetime.split(" ")[0] else: orig_date = "0000-00-00" new_file = file # TODO Figure out if there is a need to reconcile date_taken and orig_date if "0000" in orig_date: if file[0] is not ".": zero_count += 1 log_file.write("0000000 Filename: {0} date: {1} \n".format( file, orig_date))
from app import ImageUtils START_DIR1 = r"/Volumes/MyBook2TB/Backups/Videos/Unsupported_Converted" INPUT_FILE = r"/Users/paulottley/PycharmProjects/filesorter/test/Check_Dates_Zero_Rename.txt" LOG_FILE = r"/Users/paulottley/PycharmProjects/filesorter/test/Check_Dates_log.txt" batch = False if batch is True: batch_input = open(INPUT_FILE, "r") for line in batch_input: line = line.rstrip() path, fn = os.path.split(line) dt = ImageUtils.get_dt_from_name(fn) print(dt) ImageUtils.set_date(line, dt) # FileUtils.move_file(line, path + "/Zeros/", fn) print(ImageUtils.get_dt_from_atom_parser(line)) print(line) batch_input.close() else: for root, dirs, files in os.walk(START_DIR1): for file in files: full_filename = root + os.sep + file if "." is file[0]: print("Moving..." + full_filename)
def test_get_dt_from_filename(self): self.assertEqual( "2013-09-23", ImageUtils.get_dt_from_name("video_Family_2013-09-23-09-36-45.mov") [0], "Test 1") self.assertEqual( "2013-09-23", ImageUtils.get_dt_from_name("video_Family_2013-9-23-09-36-45.mov") [0], "Test 2") self.assertEqual( "2013-09-03", ImageUtils.get_dt_from_name( "video_Apr 4, 2014_Family_2013-9-3.mov")[0], "Test 3") self.assertEqual( "2013-09-03", ImageUtils.get_dt_from_name("video_Family_2013-9-03.mov")[0], "Test 4") self.assertEqual( "2013-09-03", ImageUtils.get_dt_from_name( "video_222012062356045_Family_2013-09-3.mov")[0], "Test 5") self.assertEqual( "2013-10-09", ImageUtils.get_dt_from_name("video_Family_2013-10-9.mov")[0], "Test 6") self.assertEqual( "2013-11-23", ImageUtils.get_dt_from_name("video_Family_2013-11-23.mov")[0], "Test 7") self.assertEqual( "2013-09-13", ImageUtils.get_dt_from_name("video_Family_2013-9-13.mov")[0], "Test 8") self.assertEqual( "2013-09-23", ImageUtils.get_dt_from_name("video_Family_2013-09-23-09-36-45.3gp") [0], "Test 9") self.assertEqual( None, ImageUtils.get_dt_from_name("video_Family_10334467.3gp")[0], "Test 10") self.assertEqual( None, ImageUtils.get_dt_from_name("video_Family_19334467.3gp")[0], "Test 11") self.assertEqual( "2013-06-07", ImageUtils.get_dt_from_name("video.Family.2013.06.07.mov")[0], "Test 12") self.assertEqual( "2010-09-07", ImageUtils.get_dt_from_name("video_Family_20100907_family.mp4")[0], "Test 13") self.assertEqual( None, ImageUtils.get_dt_from_name("video_Family_19330907_family.mp4")[0], "Test 14") self.assertEqual("2003-11-12", ImageUtils.get_dt_from_name("20031112_.3gp")[0], "Test 15") self.assertEqual(None, ImageUtils.get_dt_from_name("")[0], "Test 16") self.assertEqual(None, ImageUtils.get_dt_from_name(None)[0], "Test 17") self.assertEqual( None, ImageUtils.get_dt_from_name("Megan and Emily Visit_P5020066_1.MOV") [0], "False positive") self.assertEqual(None, ImageUtils.get_dt_from_name(2)[0], "Test 18") self.assertEqual( "2010-09-07", ImageUtils.get_dt_from_name("video_Family_2010:09:07_family.mp4") [0], "Test 19") self.assertEqual(None, ImageUtils.get_dt_from_name("Frog")[0], "Test 20") self.assertEqual( "2012-12-20", ImageUtils.get_dt_from_name( "2002-12-08_20161112-00-20140535-4825_IMG_20121220_112808.jpg") [0], "Test 21")
def test_get_dimensions(self): self.assertEqual( "3264x2448", ImageUtils.get_dimensions( "/Users/paulottley/Google Drive/MomsDadsPhotos/IMG_0002.jpg"))