Add strict Lightroom check, various bug fixes for overwrite and check

If -s (--strict) argument is given it will check the path too and not
only the file basename.

The following bug fixes have been done:
- if verbose was not given it was unset and so google lookup debug
output crashed. Is set to 0 on unset
- if data is found multiple times in the Lightroom DB we print out a
different warning and not put it into the not found group
- after a google lookup write the data back into the base set for debug
output purposes
- if Lightroom data is used for overwrite data was never written because
the unset check was done on the already set data and not the original
unset data
This commit is contained in:
2018-02-23 16:27:10 +09:00
parent 3e68351e8f
commit 58d9f4ebde
2 changed files with 35 additions and 8 deletions

View File

@@ -10,7 +10,7 @@ This script used the [Python XMP Tool kit](http://python-xmp-toolkit.readthedocs
reverse_geolocate.py [-h] -x
[XMP SOURCE FOLDER [XMP SOURCE FOLDER ...]]
[-l LIGHTROOM FOLDER]
[-l LIGHTROOM FOLDER] [-s]
[-f <overwrite, location, city, state, country, countrycode>]
[-g GOOGLE API KEY] [-n] [-v] [--debug] [--test]
@@ -20,6 +20,7 @@ Argument | Argument Value | Description
--- | --- | ---
-x, --xmp | XMP sidecar source folder or XMP sidecar file itself | Must given argument. It sets the path where the script will search for XMP sidecar files. It will traverse into subdirectories. A single XMP sidecar file can also be given. If the same file folder combination is found only one is processed.
-l, --lightroom | Lightroom DB base folder | The folder where the .lrcat file is located. Optional, if this is set, LR values are read before any Google maps connection is done. Fills the Latitude and Longitude and the location names. Lightroom data never overwrites data already set in the XMP sidecar file. It is recommended to have Lightroom write the XMP sidecar file before this script is run
-s, --strict | | Do strict check for Lightroom files and include the path into the check
-f, --field | Keyword: overwrite, location, city, state, country, countrycode | In the default no data is overwritten if it is already set. With the 'overwrite' flag all data is set new from the Google Maps location data. Other arguments are each of the location fields and if set only this field will be set. This can be combined with the 'overwrite' flag to overwrite already set data
-n, --nobackup | | Do not create a backup of XMP sidecar file when it is changed
-g, --google | Google Maps API Key | If available, to avoid the access limitations to the reverse location lookup
@@ -29,7 +30,7 @@ Argument | Argument Value | Description
The script will created a backup of the current sidecar file named <original name>.BK.xmp in the same location as the original file.
The Lightroom lookup currently only uses the file name. Not that this can and will fail if there are more than one file with the same name in the database. It is planned to use the base path as additional search key. If more than one is found, no Lightroom data is used.
If the Lightroom lookup is used without the --strict argument and several files with the same name are found, they will be skipped for usage.
#### Example

View File

@@ -259,6 +259,13 @@ parser.add_argument('-l', '--lightroom',
help = 'Lightroom catalogue base folder'
)
# strict LR check with base path next to the file base name
parser.add_argument('-s', '--strict',
dest = 'lightroom_strict',
action = 'store_true',
help = 'Do strict check for Lightroom files including Path in query'
)
# set behaviour override
# FLAG: default: only set not filled
# other: overwrite all or overwrite if one is missing, overwrite specifc field (as defined below)
@@ -305,6 +312,9 @@ args = parser.parse_args()
### MAIN CODE
##############################################################
if not args.verbose:
args.verbose = 0
if args.debug:
print("### ARGUMENT VARS: X: {}, L: {}, F: {}, G: {}, V: {}, D: {}, T: {}".format(args.xmp_sources, args.lightroom_folder, args.field_controls, args.google_api_key, args.verbose, args.debug, args.test))
@@ -364,7 +374,8 @@ count = {
'changed': 0,
'failed': 0,
'skipped': 0,
'not_found': 0
'not_found': 0,
'many_found': 0,
}
# do lightroom stuff only if we have the lightroom folder
@@ -380,6 +391,9 @@ if args.lightroom_folder:
query += 'LEFT JOIN AgInternedIptcIsoCountryCode ON AgHarvestedIptcMetadata.isoCountryCodeRef = AgInternedIptcIsoCountryCode.id_local '
query += 'WHERE Adobe_images.rootFile = AgLibraryFile.id_local AND Adobe_images.id_local = AgHarvestedExifMetadata.image AND AgLibraryFile.folder = AgLibraryFolder.id_local AND AgLibraryFolder.rootFolder = AgLibraryRootFolder.id_local '
query += 'AND AgLibraryFile.baseName = ?'
# absolutePath + pathFromRoot = path of XMP file - XMP file
if args.lightroom_strict:
query += 'AND AgLibraryRootFolder.absolutePath || AgLibraryFolder.pathFromRoot = ?'
# connect to LR database for reading
# open the folder and look for the first lrcat file in there
@@ -438,13 +452,23 @@ for xmp_file in work_files:
if use_lightroom:
# get the base file name, we need this for lightroom
xmp_file_basename = os.path.splitext(os.path.split(xmp_file)[1])[0]
# for strict check we need to get the full path, and add / as the LR stores the last folder with /
if args.lightroom_strict:
xmp_file_path = "{}/{}".format(os.path.split(xmp_file)[0], '/')
# try to get this file name from the DB
# NOTE: We should search here with folder too in case for double same name entries
cur.execute(query, [xmp_file_basename])
lr_query_params = [xmp_file_basename]
if args.lightroom_strict:
lr_query_params.append(xmp_file_path)
cur.execute(query, lr_query_params)
# get the row data
lrdb_row = cur.fetchone()
# abort the read because we found more than one row
if cur.fetchone() is not None:
print("(!) Lightroom DB returned one than more row")
lightroom_data_ok = False
count['many_found'] += 1
# Notify if we couldn't find one
if not lrdb_row:
elif not lrdb_row:
print("(!) Could not get data from Lightroom DB")
lightroom_data_ok = False
count['not_found'] += 1
@@ -512,6 +536,7 @@ for xmp_file in work_files:
for loc in data_set_loc:
# only write to XMP if overwrite check passes
if checkOverwrite(data_set[loc], loc, args.field_controls):
data_set[loc] = google_location[loc]
xmp.set_property(xmp_fields[loc], loc, google_location[loc])
write_file = True
if write_file:
@@ -527,7 +552,7 @@ for xmp_file in work_files:
if use_lightroom and lightroom_data_ok:
for key in data_set:
# if not the same (to original data) and passes overwrite check
if data_set[key] != data_set_original[key] and checkOverwrite(data_set[key], key, args.field_controls):
if data_set[key] != data_set_original[key] and checkOverwrite(data_set_original[key], key, args.field_controls):
xmp.set_property(xmp_fields[key], key, data_set[key])
write_file = True;
if write_file:
@@ -567,7 +592,8 @@ print("GeoLocation from Cache : {:7,}".format(count['cache']))
print("Failed reverse GeoLocate : {:7,}".format(count['failed']))
if use_lightroom:
print("GeoLocaction from Lightroom : {:7,}".format(count['lightroom']))
print("No Lightroom data : {:7,}".format(count['not_found']))
print("No Lightroom data found : {:7,}".format(count['not_found']))
print("More than one found in LR : {:7,}".format(count['many_found']))
# if we have failed data
if len(failed_files) > 0:
print("{}".format('-' * 37))