# sortByDate.rb # June 24, 2007 # require "rexml/document" require "date" require "fileutils" require "ftools" require "album_map" include REXML webalbumKeywordName = ARGV[0] targetRootOnDate = ARGV[1] targetRootOnSubject = ARGV[2] albumDataXmlFile = ARGV[3] mapDefinitionFile = ARGV[4] lastRunFileName = targetRootOnDate + File::SEPARATOR + "LastRun" puts 'LastRun filename: ' + lastRunFileName mustRun = true if File.exists?(lastRunFileName) if File.mtime(lastRunFileName) > File.mtime(albumDataXmlFile) puts 'No need to run' exit -1 end end FileUtils.mkdir_p targetRootOnDate lastRunFile = File.new(lastRunFileName, 'w'); lastRunFile.write(Date.new()) dateOffset = Date.new(2001, 01, 01) secondsPerDay = 24 * 60 * 60 puts 'Loading AlbumData XML file: ' + albumDataXmlFile xmlfile = File.new( albumDataXmlFile, "r" ) doc = Document.new( xmlfile ) puts 'Determining the key for keyword: ' + webalbumKeywordName keywordToKeyIds = Hash.new webalbumKeyId = '' doc.elements.each('/plist/dict/dict/string') do |ele| keywordToKeyIds[ele.text] = ele.previous_element.text if ele.text == webalbumKeywordName webalbumKeyId = ele.previous_element.text end end puts 'Key ' + webalbumKeyId + ' identifies keyword: ' + webalbumKeywordName puts 'loading the Album map definitions: ' + mapDefinitionFile albumMaps = Array.new File.open(mapDefinitionFile, "r") do |infile| while (line = infile.gets) albumMaps << AlbumMap.new(line, keywordToKeyIds) end end puts 'Searching all images with this keyword...' doc.elements.each('/plist/dict/dict/dict/array/string') do |ele| if ele.text == webalbumKeyId imageElement = ele.parent.parent imagePath = '' relativeOffset = 0 imageElement.get_elements('*').each do |child| if child.text == 'ImagePath' imagePath = child.next_element.text end if child.text == 'DateAsTimerInterval' relativeOffset = child.next_element.text end end # Copy image to year/month folder relativeOffset = relativeOffset.to_f imageDate = dateOffset + (relativeOffset / secondsPerDay) yearMonth = "" + imageDate.year.to_s + File::SEPARATOR if imageDate.month < 10 yearMonth = yearMonth + '0' end yearMonth = yearMonth + imageDate.month.to_s yearMonth = yearMonth + ' ' + Date::MONTHNAMES[imageDate.month] targetDirName = targetRootOnDate + File::SEPARATOR + yearMonth FileUtils.mkdir_p targetDirName pos = imagePath.rindex(File::SEPARATOR) if pos >= 0 fileName = imagePath[pos + 1, imagePath.length] else fileName = imagePath end targetFileName = targetDirName + File::SEPARATOR + fileName File.copy(imagePath, targetFileName) if File.size(targetFileName) == 0 puts 'WARN: File had size 0 after copy and will be removed: ' + targetFileName + ' orginal file location was: ' + imagePath File.delete(targetFileName) end # copy image to subject folder imageKeywordIds = Array.new keywordContainer = ele.parent keywordContainer.each_element do |child| imageKeywordIds << child.text end # puts imagePath bestMaps = Array.new bestScore = -1 albumMaps.each do |map| fitScore = map.fitScore(imageKeywordIds); if fitScore == bestScore bestMaps << map end if fitScore > bestScore bestMaps = [ map ] bestScore = fitScore end end bestMaps.each do |map| # puts imagePath + " bestScore: " + bestScore.to_s + " map kw " + map.mapKeywordIds.join('-') + " imageKeywordIds: " + imageKeywordIds.join('-') map.copyImage(targetRootOnSubject, imagePath, imageDate.year.to_s) end end end exit 0