Image-handling (Part 2): Exploring Picasa API
In my first post on image handling, I outlined a way of creating a short local reference to an image within my blogging workflow. This post, I explore how some of the manual work in hosting the images might be automated.
In my first post on image handling, I outlined a way of creating a short local reference to an image within my blogging workflow. This post, I explore how some of the manual work in hosting the images might be automated.
Picking a web service
My current blogging service, Scriptogram, does not allow for the hosting of images. Until last post, I was forced to hotlink to images I wanted to include on my blog, meaning that I didn’t use any. This has been very restrictive because some of the draft posts I’ve written require an element of visualisation.
Although I devised a way of referring to images that sit locally and then translating those references to a hosted location on publish, I did not have anywhere to host those images.
So I looked to three services I have used in the past or more recently:
The reason I picked these three is because they all have APIs which should make it possible to post images to programmatically. While I can just open an account and upload photos manually and store the addresses in my imagemap.csv
file, the whole point is that I want to remove as much friction as possible for the writing of content, while staying channel-neutral.
Exploring the APIs
Of the three options, I ruled out Pinterest pretty quickly. It seems Pinterest’s API is read-only and does not allow posting.
So I experimented with Picasa and Flickr. Both proved to be something a nightmare. There’s a lot of documentation out there but much of it is not very clear, assumes a lot about the developer, and contains a lot of deprecated information.
For example, searching for documentation on posting to Picasa with cURL brings up an old page which features a banner at the top declaring that the information is out of date. And a lot of the Flickr API documentation does not even declare that its deprecated, referring to “Frobs” which have also since been superceded by OAuth. I did a lot of reading and experimenting until I managed to make something work. In the end, I got there first with Picasa, as you can see in my last post on Picasa and OAuth 2.0.
Automating image-handling
As seen in my last post, once authenticated, adding an image to a Picasa album proved straightforward enough, so long as I didn’t also want metadata! (For various reasons, I have been unable to do that so far. Mostly, hardware issues that prevented me from spending more time exploring the rich variety of error messages I’ve been getting.)
Picasa returns some XML when an image has been uploaded. Within that, there’s a tag that looks like this:
<content type='image/jpeg' src='https://lh4.googleusercontent.com/-znwKAEKshK0/VNeyOsq2bCI/AAAAAAAAFTk/hvRBVblhcCw/mongolian-flower-1.jpg'/>
With some Bash regex it’s easy to extract the URL in that tag to get a link to the image.
Suppose I set what the slug is to be as a variable, given this is something I can set on Picasa via the post – the regex is then easy to write. Assuming $NEWIMG
is the XML return from Picasa:
FILESLUG="mongolian-flower-1.jpg"
re="<content type='.*' src='(.*/$FILESLUG)'/>"
if [[ $NEWIMG =~ $re ]]; then
IMGURL=${BASH_REMATCH[1]}
fi
echo "$FILESLUG, $IMGURL" >> ../imagemap.csv
You’ll notice that last line takes the file slug and the URL as a comma-delimited line and appends to my imagemap.csv
file. This is the same file I previously described as being parsed prior to a Scriptogram publish, so that any image addresses which begin /!/
will be treated as keys and substituted with values from my CSV.
So /!/mongolian-flower-1.jpg
now becomes a Picasa URL allowing me to display the picture below:
In this way, I’ve started to automate image-handling within my blogging workflow.
What next?
There are a number of actions I need to follow-up on if I’m going to reduce the friction around dealing with images in my workflow, namely:
- Work out how to get metadata into Picasa with the image.
- Come up with a general script for adding images from anywhere on the system to the workflow.
- Come up with appropriate checks in case images, slugs, etc. are missing.
- Tidy up the authentication script.