Sunday, January 4, 2009

Sorting photo data from Google's Picasa API

In the earlier post Google Calendar Service hosts a Gig Guide, I showed how to to use Google Data APIs to use Google Calendar data.

SNAG-1075The client has posted photos of interest to Picasa and given them tags that determine which page of their WEB site they are displayed on.

This post describes

  • how to use the Picasa Service to host photos and use them in a ASP.NET WEB site using VB
  • how to sort the GData feed into "caption" sequence using a .Net IComparer
  • how to avoid possible problems when the application is hosted on a virtual WEB hosting site like GoDaddy.

The software uses the Picasa API which should be downloaded and installed.

  1. Install the DLLs in the Bin folder of your ASP.NET application
  2. If you are having the app hosted in a shared environment, make sure you use the DLL's in the ASP.NET folder
  3. You must also use the 1.2.2 (or later) version of the API. Earlier versions do not work.
  4. If you are in a shared environment, you will also need to set the MSBuild properties as follows of the VB.NET app as follows:
    image

In the code snippet below, the class:

  1. Connects to the service and sets user credentials
  2. genHTML(tag) returns a HTML table for that tag, by constructing the string from the entries returned by getPhotoFeed(tag, size) and getComment(entry)
  3. getPhotoFeed builds the query and retieves the results
    It also sorts these using, myEntryCompare - our specialist implementation of IComparer
Imports Microsoft.VisualBasic
Imports Google.GData.Photos
Imports Google.GData.Client
Imports Google.GData.Extensions
Imports Google.GData.Extensions.Location

Public Class FoodList
Dim password As String = "secret"
Dim username As String = "secret"
Dim albumName As String = "WWW"
Dim service As PicasaService
Public Sub New()
service = New PicasaService("more secrets")
service.setUserCredentials(username, password)
End Sub
Public Function getMenuHTML(ByVal tag As String) As String
Dim html As String = "<table>"
For Each entry As PicasaEntry In getPhotoFeed(tag, "320")
html += "<tr><td colspan=2><h4>" + entry.Summary.Text + "</h4><tr><td><img src='" + entry.Content.AbsoluteUri + "'/> <td>" + getComment(entry)
Next
html += "</table>"
Dim x = ""
Return html
End Function
Private Function getPhotoFeed(ByVal tag As String, ByVal size As String) As ArrayList
Dim query As New PhotoQuery(PicasaQuery.CreatePicasaUri(username, albumName))
query.Tags = tag
query.ExtraParameters = "imgmax=" + size + "u" ' set the maximum size of the returned images
Dim feed As PicasaFeed = service.Query(query)
Dim arr As New ArrayList
arr.AddRange(feed.Entries) ' add all the entries to an array
arr.Sort(New myEntryCompare) ' sort the entries by the caption (Summary.Text)
Return arr
End Function
Public Class myEntryCompare
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim myX As String = DirectCast(x, PicasaEntry).Summary.Text
Dim myY As String = DirectCast(y, PicasaEntry).Summary.Text
Return myX.CompareTo(myY)
End Function 'IComparer.Compare
End Class
Private Function getComment(ByVal photoEntry As PicasaEntry) As String
Dim query As CommentsQuery = New CommentsQuery(PicasaQuery.CreatePicasaUri(username, albumName, (New PhotoAccessor(photoEntry)).Id))
Dim feed As PicasaFeed = service.Query(query)
Try
Return feed.Entries(0).Content.Content
Catch ex As Exception
End Try
Return ""
End Function
End Class

No comments: