up lowest date possible, refacto search date in exif tags

This commit is contained in:
Tykayn 2023-12-24 00:12:44 +01:00 committed by tykayn
parent 23f79d92df
commit e0dca75316
2 changed files with 82 additions and 54 deletions

View File

@ -12,6 +12,24 @@ import log from 'loglevel'
--------------------- */ --------------------- */
import rangement_instance from './conf/configs' import rangement_instance from './conf/configs'
import finder from './utils/finder' import finder from './utils/finder'
import minimist from "minimist";
const mini_arguments = minimist(process.argv.slice(2))
if (mini_arguments['help'] || mini_arguments['h']) {
console.log('Rangement - aide:', `
Permet de renommer en masse des photos avec des ajouts de filetags automatiques.
Documentation: https://www.cipherbliss.com/organiser-ses-fichiers-avec-des-tags-avec-filetags
syntaxe:
rangement [fichiers] [options].
options:
-h, --help : affiche l'aide.
-lowestYear=1970 : ne garde les dates exif pour le renommage que si elles sont supérieures à ce numéro d'année
`)
} else {
log.setLevel(rangement_instance.log_level) log.setLevel(rangement_instance.log_level)
@ -21,5 +39,4 @@ log.info(' ')
finder.parseArguments() finder.parseArguments()
finder.guessFileNameOnAllFilesFromArguments() finder.guessFileNameOnAllFilesFromArguments()
}
console.log('hello ts', finder)

View File

@ -25,6 +25,7 @@ export default class finder {
filesModified: 0, filesModified: 0,
filesNotModified: 0, filesNotModified: 0,
} }
private static moments: any[] = []
private static otherRenames: any = []; private static otherRenames: any = [];
private static fileList: any = []; private static fileList: any = [];
private static expandedFileList: any = []; private static expandedFileList: any = [];
@ -86,7 +87,7 @@ export default class finder {
} else { } else {
log.debug(' -------- le fichier existe bien, déstructuration') log.debug(' -------- le fichier existe bien, déstructuration')
let structureForFile = this.destructurateFileName(fullPath) let structureForFile: fileDestructuration = this.destructurateFileName(fullPath)
log.debug(' -------- ', fullPath) log.debug(' -------- ', fullPath)
log.debug(' -------- ', structureForFile) log.debug(' -------- ', structureForFile)
@ -101,15 +102,23 @@ export default class finder {
this.findExifCreationDate(structureForFile.fullPath) this.findExifCreationDate(structureForFile.fullPath)
.then(data => { .then(data => {
log.info(' ... chercher la date de création : "' + structureForFile.freeText + '"') log.info(' ... chercher la date de création : "' + structureForFile.freeText + '"')
log.debug('data', data)
let foundDate = this.findEarliestDateInExifData(data) let foundDate = this.findEarliestDateInExifData(data)
let gpsData = this.findGpsCoordinates(data) let gpsData = this.findGpsCoordinates(data)
if (gpsData) { if (gpsData) {
structureForFile.tags.push('has-gps') structureForFile.tags.push('has-gps')
} }
let isScreenshot = this.findScreenshot(structureForFile.fileNameOriginal)
if (isScreenshot) {
structureForFile.freeText.replace(`Capture d'écran`, '')
structureForFile.freeText.replace(`Screenshot`, '')
structureForFile.tags.push('screenshot')
}
log.info(' =>>>>>>> foundDate : ', foundDate) log.info(' =>>>>>>> foundDate : ', foundDate)
if (foundDate) { if (foundDate && (foundDate !== 'Invalid Date')) {
structureForFile.dateStampExif = foundDate structureForFile.dateStampExif = foundDate
} else { } else {
log.info('pas de date trouvée dans le nom') log.info('pas de date trouvée dans le nom')
@ -256,9 +265,13 @@ export default class finder {
static parseArguments() { static parseArguments() {
this.mini_arguments = minimist(process.argv.slice(2)) this.mini_arguments = minimist(process.argv.slice(2))
log.debug('arguments', this.mini_arguments) log.debug('arguments', this.mini_arguments)
if (this.mini_arguments['lowerYear']) {
log.info('année minimale à retenir pour la sélection des dates:' , this.mini_arguments['lowerYear'])
}
if (!this.mini_arguments._.length) { if (!this.mini_arguments._.length) {
log.info('pas de fichier ou de dossier demandé, veuillez spécifier un chemin en argument') log.info('pas de fichier ou de dossier demandé, veuillez spécifier un chemin en argument')
} }
console.log('this.mini_arguments', this.mini_arguments)
} }
static shouldWeChangeName(structureForFile: fileDestructuration) { static shouldWeChangeName(structureForFile: fileDestructuration) {
@ -498,6 +511,36 @@ export default class finder {
return result return result
} }
static dateIsGood(date: Date):boolean {
if (!date) {
return false
}
let year: number = date.getFullYear();
console.log('date', typeof date, year)
console.log('(year > 1970)', (year > 1970))
let lowerYearPossible = this.mini_arguments['lowerYear']
if(!lowerYearPossible){
lowerYearPossible = 2004
}
let today: Date = new Date();
let higherYearPossible = today.getFullYear()
let isDateGood = (year >= lowerYearPossible) && (year <= higherYearPossible)
console.log('lowerYearPossible', lowerYearPossible)
if(!isDateGood){
log.warn(' date is not good :', date)
}
return isDateGood
}
private static pushToMomentDatesIfGood(someDate: Date): void {
if (this.dateIsGood(someDate)) {
this.moments.push(someDate)
}
}
/** /**
* finds the earliest part in several exif date info * finds the earliest part in several exif date info
* @param exifData * @param exifData
@ -505,57 +548,24 @@ export default class finder {
*/ */
static findEarliestDateInExifData(exifData: any) { static findEarliestDateInExifData(exifData: any) {
log.debug(' finder - findEarliestDateInExifData') log.debug(' finder - findEarliestDateInExifData')
if (exifData) { if (exifData) {
let moments = [] let exifDates: string[] = ['DateTimeOriginal', 'FileModificationDateTime', 'FileModificationDate', 'ModificationDateTime', 'ModifyDate', 'FileAccessDateTime', 'DateTime', 'FileInodeChangeDateTime', 'CreateDate']
exifDates.forEach((exifDateKind: string): void => {
if (exifData[exifDateKind]) {
log.debug(' finder exifDates - : ', exifDateKind, exifData[exifDateKind])
this.pushToMomentDatesIfGood(exifData[exifDateKind])
}
})
console.log(' finder - exif data : ', exifData) this.moments = this.moments.map(d => {
log.info(' finder - exif data : ', exifData)
if (exifData.DateTimeOriginal) {
log.debug(' finder - image créée le : DateTimeOriginal : ', exifData.DateTimeOriginal)
moments.push(exifData.DateTimeOriginal)
}
if (exifData.FileModificationDateTime) {
log.debug(' finder - image créée le : File Modification Date/Time : ', exifData.FileModificationDateTime)
moments.push(exifData.FileModificationDateTime)
}
if (exifData.FileModificationDate) {
log.debug(' finder - image créée le : File Modification Date/Time : ', exifData.FileModificationDate)
moments.push(exifData.FileModificationDate)
}
if (exifData.ModificationDateTime) {
log.debug(' finder - image créée le : ModificationDateTime : ', exifData.ModificationDateTime)
moments.push(exifData.ModificationDateTime)
}
if (exifData.ModifyDate) {
log.debug(' finder - image créée le : ModifyDate : ', exifData.ModifyDate)
moments.push(exifData.ModifyDate)
}
if (exifData.FileAccessDateTime) {
moments.push(exifData.FileAccessDateTime)
}
if (exifData.DateTime) {
moments.push(exifData.DateTime)
}
if (exifData.FileInodeChangeDateTime) {
moments.push(exifData.FileInodeChangeDateTime)
}
if (exifData.FileModificationDateTime) {
log.debug(' finder - image créée le : FileModificationDateTime : ', exifData.FileModificationDateTime)
moments.push(exifData.FileModificationDateTime)
}
if (exifData.CreateDate) {
// donne des dates trop anciennes par rapport à la réalité
// log.debug(' finder - image créée le : CreateDate : ', exifData.CreateDate)
// moments.push(exifData.CreateDate)
}
moments = moments.map(d => {
let newdate = moment(d) let newdate = moment(d)
return newdate return newdate
}) })
let minDate = moment.min(moments) let minDate = moment.min(this.moments)
log.info(' finder - dates moments:', this.moments)
log.debug(' finder - minDate :::::::::', minDate) log.debug(' finder - minDate :::::::::', minDate)
log.info(' finder - minDate :::::::::', minDate.format(rangement_instance.iso_date_format)) log.info(' finder - minDate :::::::::', minDate.format(rangement_instance.iso_date_format))
@ -592,7 +602,7 @@ export default class finder {
} }
let exifPromise = await exifr.parse(filepath); let exifPromise = await exifr.parse(filepath);
log.debug(' -------- exifdata', exifPromise) // log.debug(' -------- exifdata', exifPromise)
return exifPromise return exifPromise
@ -608,4 +618,5 @@ export default class finder {
return [folders, fileName] return [folders, fileName]
} }
} }