def Test3(Folder=r"[Temp]\20120723_202707.jpg"): FilePath = r"c:\temp\test.tif" Home.dump_media_date_info(FilePath) return Folder = ExpandPath(r"[Temp]\FolderComp") Folder2 = ExpandPath(r"[Temp]\FolderComp2") RemovePath(Folder) RemovePath(Folder2) CopyDirectory(r"[ScriptFolder]\..\Html", Folder) CopyDirectory(r"[ScriptFolder]\..\Html", Folder2) import FolderComp as FolderComp FolderComp.FolderComp.clear() comp = FolderComp.FolderComp(Folder, True) comp.ScanFolders() FolderComp.FolderComp.find_all_dups() dups = FolderComp.FolderComp.select_all_dups() PrettyPrint(dups) comp = FolderComp.FolderComp(Folder2, True) comp.ScanFolders() dups = FolderComp.FolderComp.find_all_dups() dups = FolderComp.FolderComp.select_all_dups() PrettyPrint(dups) return
def FixPictureCreationDate(Folder=r"d:\pictures\Download"): Trace() Folder = Expand(Folder); for mediaType in list(Globals.Media.keys()): extensions = Globals.Media[mediaType].Extensions destRoot = Globals.Media[mediaType].Folders[0] Log(lex(r'Processing: [mediaType] Dest=[destRoot]')) for root, dirnames, filenames in os.walk(Folder): for ext in extensions: for filename in fnmatch.filter(filenames, '*.' + ext): filepath = os.path.join(root, filename) Home.change_file_creation_time_to_picture_date(filepath)
def CopyStarredPictures(FileList=r"C:\Users\[USERNAME]\AppData\Local\Google\Picasa2\db3\starlist.txt", MoveAll=False): Trace(FileList) FileList = ExpandPath(FileList); Globals.update(JSON.load_from_file(Globals.StarredPicturesJSON)) def IsDupFile(FilePath): for item in Globals.AllMediaTypes.split(','): finder = DupUtil.FindDups(item) if len(finder.GetFileDups(FilePath)) > 0: return True return False for mediaType in list(Globals.Media.keys()): extensions = Globals.Media[mediaType].Extensions destRoot = Globals.Media[mediaType].Folders[0] finder = DupUtil.FindDups(mediaType) Log(lex(r'Processing: [mediaType] Dest=[destRoot]')) fpList = open(FileList, 'r') for filepath in fpList: filepath = filepath.strip() try: Home.change_file_creation_time_to_picture_date(filepath) except: pass try: filename = os.path.basename(filepath) stats = os.stat(filepath) creation_date = time.strftime('20%y-%m-%d', time.localtime(stats.st_ctime)) year, month, day = creation_date.split('-') if not MoveAll and IsDupFile(filepath): Log('Skipping dup [filepath]') continue destFolder = Expand(r'[destRoot]\[year]', True) finder.AddNewFile(filepath, destFolder, True) except: Log(r'ERROR [filepath]') ReportException() DupUtil.ImportLog.close() Log() Log(r'Results in [LogFile]')
def MoveMediaToFolder(Folder, ImportAll=False, KeepOriginals=False, TestMode=False, KeepPath=False): Folder = ExpandPath(Folder); Trace(Folder) extensions = flatten([ media.Extensions for media in Globals.Media.values() ]) extensions = list(map(str.lower, extensions)) moved = 0 for filepath in FindFiles(Folder): try: filepath = Plain(filepath) mediaType = Home.MediaTypeFromExtension(os.path.splitext(filepath)[1]) if not mediaType: continue if not ImportAll and IsDupFile(filepath, ScanFiles=False): if TestMode: LogPlain('TestMode: Duplicate', filepath) continue LogPlain('Skipping dup %s' % filepath) if not KeepOriginals: DeleteFile(filepath) continue moved += 1 destRoot = ExpandPath(Globals.Media[mediaType].DefaultFolders[0]) if not KeepPath and mediaType in [ 'Pictures', 'Video' ]: ctime = Home.change_file_creation_time_to_picture_date(filepath, TestMode=TestMode, DebugMode=False) date, _ = ctime.split(' ') year, month, day = date.split(':') destFolder = ExpandPath(r'[destRoot]\[MediaPrefix]\[year]\[MediaPrefix]-[month]-[year]') else: destFolder = os.path.dirname(filepath) destFolder = destFolder[len(Folder):] destFolder = '%s%s' % (destRoot, destFolder) destFolder = Plain(destFolder) uniqFile = '' srcFolder = os.path.dirname(filepath) if srcFolder.lower() == destFolder.lower(): LogPlain('Already in destination folder' , filepath) elif TestMode: LogPlain('TestMode: AddNewFile', filepath, destFolder) elif KeepOriginals: uniqFile = CopyToUniqueFilePlain(filepath, destFolder) else: uniqFile = MoveToUniqueFilePlain(filepath, destFolder, '') if uniqFile: ImportLog.log(filepath, uniqFile) except (KeyboardInterrupt, SystemExit): raise except: ReportException() return LogPlainError('Failed to move %s' % (filepath)) return moved
def Test2(Folder=""): # FilePath = r"g:\Backup Files\CarlosMac\Applications\Carbon Copy Cloner.app\Contents\MacOS\ccc_helper.app\Contents\Resources\arrow.tif" FilePath = r"c:\temp\test.tif" date_str, date_st = Home.MediaDateTime.get_creation_date(FilePath) ctime = DateTime.stats_ctime(FilePath) Log(date_str, ctime, date_st) return mediaFolder = ExpandPath(r"[Temp]\Media") mediaFolder2 = ExpandPath(r"[Temp]\Media2") CopyDirectory(r"[ScriptFolder]\TestData\Media", mediaFolder) CopyDirectory(r"[ScriptFolder]\TestData\ImportMedia", mediaFolder2) fileList = list(FindFiles(mediaFolder)) fileList.extend(list(FindFiles(mediaFolder2))) for filePath in fileList: stats = os.stat(filePath) Log("before ctime", DateTime.to_file_date_str(stats.st_ctime)) Log("before atime", DateTime.to_file_date_str(stats.st_atime)) Log("before mtime", DateTime.to_file_date_str(stats.st_mtime)) date_str = Home.change_file_creation_time_to_picture_date(filePath) stats = os.stat(filePath) Log("after ctime", DateTime.to_file_date_str(stats.st_ctime)) Log("after atime", DateTime.to_file_date_str(stats.st_atime)) Log("after mtime", DateTime.to_file_date_str(stats.st_mtime)) Log() Exit() filePath = r"/Users/cgomes/Pictures/2011/Download/Latest/DSC00157.JPG" stats = os.stat(filePath) Log("ctime", DateTime.to_file_date_str(stats.st_ctime)) Log("atime", DateTime.to_file_date_str(stats.st_atime)) Log("mtime", DateTime.to_file_date_str(stats.st_mtime)) print(Home.change_file_creation_time_to_picture_date(filePath, TestMode=True)) # Exit() CopyDirectory(r"[ScriptFolder]/TestData/Media/", r"[Temp]/Media") testFiles = [ExpandPath(r"[Temp]/Test.jpg"), ExpandPath(r"[Temp]/20120723_202707.jpg")] for testFile in testFiles: CopyFile(r"[ScriptFolder]/TestData/Media/Pictures/IMG_0004.JPG", testFile) print(Home.change_file_creation_time_to_picture_date(testFile)) folders = [r"/Users/cgomes/Pictures/Kai/2010"] for folder in folders: robocopy(folder, r"[Temp]\Media") for filePath in FindFiles(r"[Temp]\Media"): date_str = Home.change_file_creation_time_to_picture_date(filePath, TestMode=False) folder = os.path.dirname(filePath) dirname = os.path.basename(folder) dirname_split = dirname.split("-") if len(dirname_split) == 3: prefix, folderMonth, folderYear = dirname_split year, month, day = (date_str.split(" ")[0]).split(":") if int(month) != int(folderMonth) or int(year) != int(folderYear): Home.change_file_creation_time_to_picture_date(filePath, TestMode=True, DebugMode=False) Warning(r"folder mismatch [date_str] [month]-[year] != [folderMonth]-[folderYear] for [filePath]") stats = os.stat(filePath) ctime = DateTime.stats_ctime(filePath) atime = DateTime.to_file_date_str(stats.st_atime) mtime = DateTime.to_file_date_str(stats.st_mtime) if ctime != date_str: Warning("ctime mismatch [date_str] != [ctime] [atime] [mtime] - [filePath]") pass Exit() DupUtil.Import(r"[Temp]/Media", TestMode=True)
def MediaImport_UnitTest(GenerateResults=False): Trace() ImportMediaFolder = ExpandPath(r'[TestOutputFolder]\ImportMedia') ImportMediaFolder2 = ExpandPath(r'[TestOutputFolder]\ImportMedia2') ImportMediaFolder3 = ExpandPath(r'[TestOutputFolder]\ImportMedia3') ChangeMediaFolder = ExpandPath(r'[TestOutputFolder]\ChangeMedia') Globals.MediaPrefix = 'Test' Globals.DefaultMediaFolders = [ ExpandPath('[TestOutputFolder]\Media\Pictures'), ExpandPath('[TestOutputFolder]\Media\Music'), ExpandPath('[TestOutputFolder]\Media\Videos') ] Globals['Media']['Pictures']['DefaultFolders'] = [ Globals.DefaultMediaFolders[0] ] Globals['Media']['Music']['DefaultFolders'] = [ Globals.DefaultMediaFolders[1] ] Globals['Media']['Video']['DefaultFolders'] = [ Globals.DefaultMediaFolders[2] ] CopyDirectory(r'[ScriptFolder]\TestData\Media', r'[TestOutputFolder]\Media') CopyDirectory(r'[ScriptFolder]\TestData\ImportMedia', ImportMediaFolder) CopyDirectory(r'[ScriptFolder]\TestData\ImportMedia', ImportMediaFolder2) CopyDirectory(r'[ScriptFolder]\TestData\Media', ImportMediaFolder3) CopyDirectory(r'[ScriptFolder]\TestData\ImportMedia', ChangeMediaFolder) expectedData = UnitTest.LoadExpectedResults() UnitTest.Step('Change File Creation Time') actualResults = dictn() actualResults['change'] = dictn() for filePath in FindFiles(ChangeMediaFolder): Home.change_file_creation_time_to_picture_date(filePath, DebugMode=True) stats = os.stat(filePath) actualResults['change'][filePath] = { #'atime' : DateTime.to_file_date_str(stats.st_atime), #'ctime' : DateTime.to_file_date_str(stats.st_ctime), 'mtime' : DateTime.to_file_date_str(stats.st_mtime), } if not GenerateResults: UnitTest.VerifyMatch(actualResults.change, expectedData.change) UnitTest.Step('Import Media') actualResults['imported'] = dictn() FindDups.FindFolderDups() imported = FindDups.Import(ImportMediaFolder) actualResults['imported'][ImportMediaFolder] = { 'count' : imported } imported = FindDups.Import(ImportMediaFolder2) actualResults['imported'][ImportMediaFolder2] = { 'count' : imported } imported = FindDups.Import(ImportMediaFolder3) actualResults['imported'][ImportMediaFolder3] = { 'count' : imported } for folder in Globals.DefaultMediaFolders: for filePath in FindFiles(folder): stats = os.stat(filePath) actualResults['imported'][filePath] = { #'atime' : DateTime.to_file_date_str(stats.st_atime), #'ctime' : DateTime.to_file_date_str(stats.st_ctime), 'mtime' : DateTime.to_file_date_str(stats.st_mtime), } # PrettyPrint(actualResults) if not GenerateResults: UnitTest.VerifyMatch(actualResults.imported, expectedData.imported) if GenerateResults: UnitTest.SaveExpectedResults(actualResults)