set flickr_folder to ((path to home folder as text) & "scripts:flickr")
set the_xml_folder to flickr_folder & ":photoinfo"
set the_download_folder to flickr_folder & ":download"
set already_downloaded to list folder the_download_folder
set the_xml_files to get_xml_files(the_xml_folder)
repeat with i from 1 to the count of the_xml_files
set the_xml_file to (item i of the_xml_files as text)
set the_data to load_xml(the_xml_folder & ":" & the_xml_file)
set the_xml to parse_it(the_data)
-- generate the full URL to the original photo file
set photo to getAnElement(the_xml, "photo")
set photo_id to |id| of XML attributes of photo
set server to server of XML attributes of photo
set secret to secret of XML attributes of photo
set photo_file_name to photo_id & "_" & secret & "_o.jpg"
if already_downloaded contains photo_file_name then
log "skipping " & photo_file_name
else
-- do it
set photo_url to "http://static.flickr.com/" & server & "/" & photo_file_name
--set the_title to getElementValue(getElementFromPath(the_xml, {"photo", "title"}))
--set the_description to getElementValue(getElementFromPath(the_xml, {"photo", "description"}))
-- get a list of the tags associated with this photo
--set the_tags to getElements(getElementFromPath(the_xml, {"photo", "tags"}), "tag")
--set tags to {}
--repeat with j from 1 to count of the_tags
-- set tag to (item j of the_tags)
-- set end of tags to getElementValue(tag)
--end repeat
download_photo(photo_url, the_download_folder, photo_file_name)
--set imported_photo_ok to import_photo(photo_file_name, the_title, the_description)
--if imported_photo_ok then
-- assign_to_selection_keywords(tags)
--end if
end if
end repeat
on import_photo(photo_file_name, the_title, the_comment)
tell application "iPhoto"
--import from "~/scripts/flickr/download/" & photo_file_name with force copy
--return false
set found_photo to false
set the photo_list to photos of photo library album
repeat with i from 1 to count of photo_list
set the_photo to (item i of photo_list)
tell the_photo
set the_image_filename to the image filename
if the_image_filename is the photo_file_name then
set found_photo to true
select the_photo
set i to count of photo_list
end if
end tell
end repeat
if found_photo then
set sel to selection
set this_photo to last item of sel
tell this_photo
set the title to the_title
set the comment to the_comment
end tell
else
log "FAILED TO FIND PHOTO"
end if
end tell
return found_photo
end import_photo
on download_photo(photo_url, the_download_folder, photo_file_name)
tell application "URL Access Scripting"
download photo_url to (the_download_folder & ":" & photo_file_name)
end tell
end download_photo
on assign_to_selection_keywords(keywords_list)
tell application "iPhoto"
set keywords_count to the count of the keywords_list
repeat with i from 1 to keywords_count
set the_keyword to (item i of the keywords_list)
«event KAnewkey» given name:the_keyword
assign keyword string the_keyword
end repeat
end tell
end assign_to_selection_keywords
on getElementValue(theXML)
if theXML is missing value or theXML is {} then
return ""
else if class of theXML is string then
return theXML
else
try
return item 1 of XML contents of theXML
on error number -1728
return ""
end try
end if
end getElementValue
on getElementFromPath(theXML, theElementPath)
if theElementPath is {} then
return theXML
else
local foundElement
set foundElement to getAnElement(theXML, item 1 of theElementPath)
if foundElement is not missing value and ¬
class of foundElement is XML element then
return getElementFromPath(foundElement, rest of theElementPath)
else
return missing value
end if
end if
end getElementFromPath
on getAnElement(theXML, theElementName)
-- find and return a particular element (this presumes there is only one instance of the element)
repeat with anElement in XML contents of theXML
if class of anElement is XML element and ¬
XML tag of anElement is theElementName then
return contents of anElement
end if
end repeat
return missing value
end getAnElement
on getNamedElement(theXML, theElementName, theName)
-- find and return the first element with a particular name attribute
if class of theXML is XML element or class of theXML is XML document then
repeat with anElement in XML contents of theXML
try
if class of anElement is XML element and ¬
XML tag of anElement is theElementName and ¬
|name| of XML attributes of anElement is theName then
return contents of anElement
end if
on error number -1728
-- ignore this error
end try
end repeat
else if class of theXML is list then
repeat with anElement in theXML
try
if class of anElement is XML element and ¬
XML tag of anElement is theElementName and ¬
|name| of XML attributes of anElement is theName then
return contents of anElement
end if
on error number -1728
-- ignore this error
end try
end repeat
end if
return missing value
end getNamedElement
on getElements(theXML, theElementName)
local theResult
set theResult to {}
repeat with anElement in XML contents of theXML
if class of anElement is XML element and XML tag of anElement is theElementName then
set end of theResult to contents of anElement
end if
end repeat
return theResult as list
end getElements
on get_xml_files(the_xml_folder)
return list folder the_xml_folder
end get_xml_files
on load_xml(the_file)
set fRef to open for access file the_file without write permission
set the_contents to (read fRef)
close access fRef
return the_contents
end load_xml
on parse_it(the_xml_data)
set the_xml to parse XML the_xml_data
return the_xml
end parse_it