Friday, March 28, 2008

Uploading Images to Flickr using flickcurl and Groovy

For the latest Groosh release I created an more elaborate example of how to use Groovy and Groosh. This example shows how to upload Images to a Flickr account using Groovy, Groosh and flickcurl.

import static groosh.Groosh.groosh
import static org.codehaus.groovy.groosh.stream.DevNull.devnull

ids = [:]
shell = groosh()

//get all images in this folder and upload it to flickr
//remember the photo id we get from flickr
shell.ls().grep(~/.*jpg/).each {
println "Uploading file $it to flickr"
flickcurl = shell.flickcurl("upload",it,"friend","family")
.useError(true)
id = flickcurl.grep(~/.*Photo ID.*/)[0].split(":")[1].trim()
ids[it] = id
println "Photo ID is: $id"
}
//we need to know the first photo id
firstKey = ids.keySet().toList()[0]

//create a set with the name of the directory we are in right now
//use the id of the first photo as set cover
setName = shell.pwd().text.split("/")[-1]
println "Creating set: $setName"
flickcurl = shell.flickcurl("photosets.create",setName,setName,ids[firstKey])
.useError(true)
id = flickcurl.grep(~/.*Photoset.*/)[0].split(" ")[2].trim()
println "Photoset ID is: $id"

//make a backup of the ids in a file for later reference
println "Writing ids to a file"
file = new File(shell.pwd().text.trim() + "/.flickrset")
file << "Photoset:" << id << "\n"
ids.each {
file << it.key << ":" << it.value << "\n"
}

//the first photo is allready part of the photo set so lets remove it
ids.remove(firstKey)

//add the remaining photos to the photo set
ids.each {
println "Adding photo to set at flickr: $it"
shell.flickcurl("photosets.addPhoto",id,it.value) | devnull()
}

println "DONE"
Post a Comment