Skip to content

Commit

Permalink
Merge pull request #26 from aviationphoto/v1.3
Browse files Browse the repository at this point in the history
V1.3.1
  • Loading branch information
aviationphoto authored Apr 13, 2017
2 parents 120bc61 + 3c572f0 commit 74113b6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 58 deletions.
66 changes: 37 additions & 29 deletions aircraft-metadata.lrplugin/AircraftMetadataLookup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,29 @@ along with LR Aircraft Metadata. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------------]]
require "Utilities"

local AircraftMetadataImport = {}

function AircraftMetadataImport()

loadPrefs()
startLogger('AircraftMetadataLookup')

-- initialize variables
local catalog = LrApplication.activeCatalog()
local selectedPhotos = catalog:getTargetPhotos()
local messageEnd = 'Aircraft Metadata Lookup finished'
local metadataCache = {}
local countSelected = 0
local countProcessed = 0
local countCacheHit = 0
local countLookup = 0
local countNoReg = 0
local countRegNotFound = 0
local flagRegFound = true
local flagRun = true
local progressScope, keywordRegNotFound, keywordWrongReg, dialogAction, photo, photoFilename, searchRegistration, lookupURL, content
local foundRegistration, foundAirline, foundAircraft, foundAircraftManufacturer, foundAircraftType

LrFunctionContext.callWithContext( "Aircraft Metadata Import", function(context)
-- define progress bar
progressScope = LrProgressScope({title = 'Aircraft Metadata Lookup'})
Expand All @@ -28,21 +50,6 @@ function AircraftMetadataImport()
progressScope:done()
end)

loadPrefs()
startLogger('AircraftMetadataLookup')

-- initialize variables
messageEnd = 'Aircraft Metadata Lookup finished'
metadataCache = {}
countSelected = 0
countProcessed = 0
countCacheHit = 0
countLookup = 0
countNoReg = 0
countRegNotFound = 0
flagRegFound = true
flagRun = true

-- create / get keyword
catalog:withWriteAccessDo('set keyword',
function()
Expand All @@ -58,9 +65,9 @@ function AircraftMetadataImport()
flagRun = false
progressScope:done()
messageEnd = 'Aircraft Metadata Lookup canceled'
log_info('no active photo selection - user canceled run on entire filmstrip')
LrLogger:info('no active photo selection - user canceled run on entire filmstrip')
else
log_info('no active photo selection - running on entire filmstrip')
LrLogger:info('no active photo selection - running on entire filmstrip')
end
end

Expand All @@ -70,15 +77,15 @@ function AircraftMetadataImport()
for _ in pairs(selectedPhotos) do
countSelected = countSelected + 1
end
log_info('performing lookup for '..countSelected..' selected photos')
LrLogger:info('performing lookup for '..countSelected..' selected photos')
-- loop through each of the photos
for _, photo in ipairs (selectedPhotos) do
countProcessed = countProcessed + 1
flagRegFound = true
-- check if user hit cancel in progress bar
if progressScope:isCanceled() then
messageEnd = 'Aircraft Metadata Lookup canceled'
log_info('canceled by user')
LrLogger:info('canceled by user')
break
else
-- set photo name for logging
Expand All @@ -99,7 +106,7 @@ function AircraftMetadataImport()
-- no, we need to do a lookup
countLookup = countLookup + 1
lookupURL = LrStringUtils.trimWhitespace(LrPrefs.prefLookupUrl)..searchRegistration
log_info(photoFilename..' - looking up registration at '..lookupURL..' for: '..searchRegistration)
LrLogger:info(photoFilename..' - looking up registration at '..lookupURL..' for: '..searchRegistration)
-- do the lookup
content = LrHttp.get(lookupURL)
--LrDialogs.message(photoFilename, content, 'info')
Expand All @@ -108,7 +115,7 @@ function AircraftMetadataImport()
-- lookup returned nothing usefull
countRegNotFound = countRegNotFound + 1
flagRegFound = false
log_warn(photoFilename..' - REG NOT FOUND: no metadata found for registration '..searchRegistration)
LrLogger:warn(photoFilename..' - REG NOT FOUND: no metadata found for registration '..searchRegistration)
-- mark photo with keyword reg_not_found
catalog:withWriteAccessDo('set keyword',
function()
Expand All @@ -126,10 +133,10 @@ function AircraftMetadataImport()
foundAircraftType = LrStringUtils.trimWhitespace(string.sub(foundAircraft, string.len(foundAircraftManufacturer)+1, string.len(foundAircraft)))
-- cache found metadata
metadataCache[searchRegistration] = {foundRegistration = foundRegistration, foundAirline = foundAirline, foundAircraft = foundAircraft, foundAircraftManufacturer = foundAircraftManufacturer, foundAircraftType = foundAircraftType, lookupURL = lookupURL}
log_info(photoFilename..' - metadata found: Reg: '..foundRegistration..', Airline: '..foundAirline..', Manufacturer: '..foundAircraftManufacturer..', Type: '..foundAircraftType)
LrLogger:info(photoFilename..' - metadata found: Reg: '..foundRegistration..', Airline: '..foundAirline..', Manufacturer: '..foundAircraftManufacturer..', Type: '..foundAircraftType)
else
-- no, lookup returned wrong registration
log_warn(photoFilename..' - WRONG REG: lookup returned wrong registration: '..foundRegistration..' instead of '..searchRegistration)
LrLogger:warn(photoFilename..' - WRONG REG: lookup returned wrong registration: '..foundRegistration..' instead of '..searchRegistration)
countNoReg = countNoReg + 1
flagRegFound = false
-- mark photo with keyword wrong_reg
Expand All @@ -141,7 +148,7 @@ function AircraftMetadataImport()
end
else
-- yes, use cached metadata
log_info(photoFilename..' - using cached metadata for: '..metadataCache[searchRegistration].foundRegistration)
LrLogger:info(photoFilename..' - using cached metadata for: '..metadataCache[searchRegistration].foundRegistration)
countCacheHit = countCacheHit + 1
end
-- check if we have a reg and user did not hit cancel
Expand All @@ -162,18 +169,18 @@ function AircraftMetadataImport()
end
else
-- photo has no registration
log_info(photoFilename..' - no registration set')
LrLogger:info(photoFilename..' - no registration set')
countNoReg = countNoReg + 1
end
-- update progress bar
progressScope:setPortionComplete(countProcessed, countSelected)
end
end
log_info('processed '..countProcessed..' of '..countSelected..' selected photos ('..countLookup..' web lookups, '..countRegNotFound..' regs not found, '..countCacheHit..' cache hits, '..countNoReg..' without reg)')
LrLogger:info('processed '..countProcessed..' of '..countSelected..' selected photos ('..countLookup..' web lookups, '..countRegNotFound..' regs not found, '..countCacheHit..' cache hits, '..countNoReg..' without reg)')
progressScope:done()
end
LrDialogs.showBezel(messageEnd)
log_info('>>>> done')
LrLogger:info('>>>> done')
end)
end

Expand All @@ -196,16 +203,17 @@ end
------- extractMetadata() -----------------------------------------------------
-- isolate metadata - sorry, creepy html parsing, no fancy things like JSON available
function extractMetadata(payload, Token1, Token2)
local posStart, posEnd, line
posStart, posEnd = string.find(payload, Token1)
if posEnd == nil then
log_error('Token '..Token1..' not found.')
LrLogger:error('Token '..Token1..' not found.')
LrErrors.throwUserError('Token "'..Token1..'" not found.')
else
line = string.sub(payload, posEnd + 1)
--LrDialogs.message('Lookup Airline - after Token 1', line, 'info')
posStart, posEnd = string.find(line, Token2)
if posStart == nil then
log_error('Token '..Token2..' not found.')
LrLogger:error('Token '..Token2..' not found.')
LrErrors.throwUserError('Token "'..Token2..'" not found.')
else
line = LrStringUtils.trimWhitespace(string.sub(line, 1, posStart - 1))
Expand Down
46 changes: 26 additions & 20 deletions aircraft-metadata.lrplugin/AircraftUrlUpdate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,23 @@ along with LR Aircraft Metadata. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------------]]
require "Utilities"

local AircraftUrlUpdate = {}

function AircraftUrlUpdate()

loadPrefs()
startLogger('AircraftUrlUpdate')

-- initialize variables
local catalog = LrApplication.activeCatalog()
local selectedPhotos = catalog:getTargetPhotos()
local messageEnd = 'Aircraft URL Update finished'
local countSelected = 0
local countProcessed = 0
local countSkipped = 0
local flagRun = true
local progressScope, dialogAction, photo, photoFilename, oldURL, newURL

LrFunctionContext.callWithContext( "Aircraft Metadata Import", function(context)
-- define progress bar
progressScope = LrProgressScope({title = 'Aircraft URL Update'})
Expand All @@ -28,16 +44,6 @@ function AircraftUrlUpdate()
progressScope:done()
end)

loadPrefs()
startLogger('AircraftUrlUpdate')

-- initialize variables
messageEnd = 'Aircraft URL Update finished'
countSelected = 0
countProcessed = 0
countSkipped = 0
flagRun = true

-- check if user selected at least one photos
if catalog:getTargetPhoto() == nil then
dialogAction = LrDialogs.confirm('Aircraft URL Update', 'No photo selected - run update on all photos in filmstrip?', 'Yes', 'No')
Expand All @@ -46,9 +52,9 @@ function AircraftUrlUpdate()
flagRun = false
progressScope:done()
messageEnd = 'Aircraft URL Update canceled'
log_info('no active photo selection - user canceled run on entire filmstrip')
LrLogger:info('no active photo selection - user canceled run on entire filmstrip')
else
log_info('no active photo selection - running on entire filmstrip')
LrLogger:info('no active photo selection - running on entire filmstrip')
end
end

Expand All @@ -58,13 +64,13 @@ function AircraftUrlUpdate()
for _ in pairs(selectedPhotos) do
countSelected = countSelected + 1
end
log_info('performing update on '..countSelected..' selected photos')
LrLogger:info('performing update on '..countSelected..' selected photos')
for _, photo in ipairs (selectedPhotos) do
countProcessed = countProcessed + 1
-- check if user hit cancel in progress bar
if progressScope:isCanceled() then
messageEnd = 'Aircraft URL Update canceled'
log_info('canceled by user')
LrLogger:info('canceled by user')
break
else
-- set photo name for logging
Expand All @@ -77,13 +83,13 @@ function AircraftUrlUpdate()
-- check if a registration is set
if photo:getPropertyForPlugin(_PLUGIN, 'registration') == nil then
-- photo has no url, maybee metadata update failed
log_info(photoFilename..' - skipped: no registration set')
LrLogger:info(photoFilename..' - skipped: no registration set')
countSkipped = countSkipped + 1
else
-- check if url is set
if photo:getPropertyForPlugin(_PLUGIN, 'aircraft_url') == nil then
-- photo has no registration
log_info(photoFilename..' - skipped: no url set')
LrLogger:info(photoFilename..' - skipped: no url set')
countSkipped = countSkipped + 1
else
-- looks good, go on
Expand All @@ -92,14 +98,14 @@ function AircraftUrlUpdate()
-- check if we need a update
if oldURL == newURL then
-- no
log_info(photoFilename..' - '..oldURL..' is fine, no update necessary')
LrLogger:info(photoFilename..' - '..oldURL..' is fine, no update necessary')
else
-- yes
catalog:withWriteAccessDo('set aircraft metadata',
function()
photo:setPropertyForPlugin(_PLUGIN, 'aircraft_url', newURL)
end)
log_info(photoFilename..' - url updated: '..oldURL..' --> '..newURL)
LrLogger:info(photoFilename..' - url updated: '..oldURL..' --> '..newURL)
end
end
end
Expand All @@ -108,9 +114,9 @@ function AircraftUrlUpdate()
end
end
progressScope:done()
log_info('processed '..countProcessed..' of '..countSelected..' selected photos ('..countSkipped..' skipped)')
LrLogger:info('processed '..countProcessed..' of '..countSelected..' selected photos ('..countSkipped..' skipped)')
LrDialogs.showBezel(messageEnd)
log_info('>>>> done')
LrLogger:info('>>>> done')
end)
end

Expand Down
2 changes: 1 addition & 1 deletion aircraft-metadata.lrplugin/Info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with LR Aircraft Metadata. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------------]]
return {
LrSdkVersion = 6.0,
VERSION = {major=1, minor=3, revision=0,},
VERSION = {major=1, minor=3, revision=1,},

LrPluginName = 'Aircraft Metadata',
LrToolkitIdentifier = 'ch.aviationphoto.aircraftmetadata',
Expand Down
14 changes: 6 additions & 8 deletions aircraft-metadata.lrplugin/Utilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,27 @@ LrSystemInfo = import 'LrSystemInfo'
LrTasks = import 'LrTasks'
LrPrefs = import 'LrPrefs'.prefsForPlugin()
LrLogger = import 'LrLogger'('AircraftMetadata')
log_error, log_warn, log_info = LrLogger:quick('error', 'warn', 'info')
catalog = LrApplication.activeCatalog()
selectedPhotos = catalog:getTargetPhotos()

------- startLogger() --------------------------------------------------------
-- start logger
function startLogger(functionName)
local logPath, success, reason
-- check if logging enabled
if LrPrefs.prefFlagLogging then
LrLogger:enable('logfile')
-- clear old logfile
logPath = LrPathUtils.child(LrPathUtils.getStandardFilePath('documents'), 'AircraftMetadata.log')
if LrFileUtils.exists( logPath ) then
success, reason = LrFileUtils.delete( logPath )
if LrFileUtils.exists(logPath) then
success, reason = LrFileUtils.delete(logPath)
if not success then
log_error('error deleting existing logfile!'..reason)
LrLogger:error('error deleting existing logfile!'..reason)
end
end
else
LrLogger:disable()
end
log_info('>>>> running '..functionName)
log_info('Lightroom version: '..LrApplication.versionString()..' on '..LrSystemInfo.summaryString())
LrLogger:info('>>>> running '..functionName)
LrLogger:info('Lightroom version: '..LrApplication.versionString()..' on '..LrSystemInfo.summaryString())
end

------- loadPrefs() -----------------------------------------------------------
Expand Down

0 comments on commit 74113b6

Please sign in to comment.