Dynamicweb PicasaWeb Integration

I was just playing around with the Google API and Dynamicweb the other day. I created a small Dynamicweb module allowing the user to access his/her Picasa photo album. The module allows the user to list albums and display photo details. It also allows the user to search the tags associated with a photo.

The code should be rather self-explanatory, but if you have any questions, then leave me a comment.

using System;
using System.Configuration;
using System.Web;
using Google.GData.Photos;
using Google.GData.Client;
using Dynamicweb;
using Dynamicweb.Extensibility;
using Template = Dynamicweb.Templatev2.Template;

namespace CustomModules.CustomModules.PicasaIntegration
{
[AddInName(“PicasaIntegration”)]
public class Frontend : ContentModule
{
private const string _username = “YOUR USERNAME HERE”;
private const string _password = “YOUR PASSWORD HERE”;
private Template _listAlbumTemplate = null;
private Template _listPhotoAlbumTemplate = null;
private Template _listSearchTemplate = null;
private readonly string _command = Base.ChkString(Base.Request(“command”));
private readonly string _albumId = Base.ChkString(Base.Request(“AlbumId”));
private readonly string _tags = Base.ChkString(Base.Request(“tags”));
private PicasaService _service = null;

public override string GetContent()
{
_service = new PicasaService(“exampleCo-exampleApp-1”);
_service.setUserCredentials(_username, _password);

string output = “”;
switch (_command)
{
case “renderPhotoAlbum”:
output = renderPhotoAlbum();
break;

case “search”:
output = renderSearch();
break;

default:
output = renderAlbums();
break;
}

return output;
}

public string renderAlbums()
{
const string cacheKey = “renderAlbums”;
var output = HttpRuntime.Cache.Get(cacheKey) as string;
if (string.IsNullOrEmpty(output))
{
_listAlbumTemplate = new Template(“PicasaIntegration/ListAlbumTemplate.xslt”);
Template albumTemplate = _listAlbumTemplate.GetLoop(“Albums”);

var query = new AlbumQuery(PicasaQuery.CreatePicasaUri(“default”));

PicasaFeed feed = _service.Query(query);
PhotoQuery photoQuery = null;
foreach (PicasaEntry entry in feed.Entries)
{
var ac = new AlbumAccessor(entry);
albumTemplate.SetTag(“NumPhotos”, ac.NumPhotos);
albumTemplate.SetTag(“AlbumTitle”, ac.AlbumTitle);
albumTemplate.SetTag(“AlbumAuthor”, ac.AlbumAuthor);
albumTemplate.SetTag(“AlbumSummary”, ac.AlbumSummary);
albumTemplate.SetTag(“AlbumID”, ac.Id);

photoQuery = new PhotoQuery(PicasaQuery.CreatePicasaUri(“default”, ac.Id)) { NumberToRetrieve = 1 };
renderPhotos(albumTemplate, _service.Query(photoQuery));
}

output = _listAlbumTemplate.Output();
HttpRuntime.Cache.Insert(cacheKey, output, null, DateTime.Now.AddMinutes(Convert.ToDouble(ConfigurationManager.AppSettings[“PicasaIntegrationExportCacheDuration”])), TimeSpan.Zero);
}

return output;
}

public string renderPhotoAlbum()
{
if (string.IsNullOrEmpty(_albumId)) return null;
string cacheKey = “renderPhotoAlbum” + _albumId;
var output = HttpRuntime.Cache.Get(cacheKey) as string;
if (string.IsNullOrEmpty(output))
{
_listPhotoAlbumTemplate = new Template(“PicasaIntegration/ListPhotoAlbumTemplate.xslt”);
Template photoTemplate = _listPhotoAlbumTemplate.GetLoop(“Photos”);
PhotoQuery photoQuery = new PhotoQuery(PicasaQuery.CreatePicasaUri(“default”, _albumId)) { NumberToRetrieve = 100 };
renderPhotos(photoTemplate, _service.Query(photoQuery));
output = _listPhotoAlbumTemplate.Output();
HttpRuntime.Cache.Insert(cacheKey, output, null, DateTime.Now.AddMinutes(Convert.ToDouble(ConfigurationManager.AppSettings[“PicasaIntegrationExportCacheDuration”])), TimeSpan.Zero);
}
return output;
}

public string renderSearch()
{
if (string.IsNullOrEmpty(_tags)) return null;
_listSearchTemplate = new Template(“PicasaIntegration/ListPhotoAlbumTemplate.xslt”);
Template photoTemplate = _listSearchTemplate.GetLoop(“Photos”);
var photoQuery = new PhotoQuery(PicasaQuery.CreatePicasaUri(“default”)) { NumberToRetrieve = 10, Tags = _tags };
renderPhotos(photoTemplate, _service.Query(photoQuery));
return _listSearchTemplate.Output();
}

private void renderPhotos(Template template, AtomFeed photoFeed)
{
foreach (PicasaEntry photoEntry in photoFeed.Entries)
{
renderPhoto(template, photoEntry);
template.CommitLoop();
}
}

private void renderPhoto(Template template, PicasaEntry photoEntry)
{
var accessor = new PhotoAccessor(photoEntry);

template.SetTag(“MediaURI”, photoEntry.MediaUri.ToString());
template.SetTag(“thumbURI”, photoEntry.Media.Thumbnails[0].Attributes[“url”].ToString());
template.SetTag(“thumbURI2”, photoEntry.Media.Thumbnails[1].Attributes[“url”].ToString());
template.SetTag(“thumbURI3”, photoEntry.Media.Thumbnails[2].Attributes[“url”].ToString());
template.SetTag(“Id”, accessor.Id);
//template.SetTag(“MediaSourceName”, photoEntry.MediaSource.Name);
template.SetTag(“Published”, photoEntry.Published);
template.SetTag(“Rights”, photoEntry.Rights.Text);
template.SetTag(“Summary”, photoEntry.Summary.Text);
template.SetTag(“Updated”, photoEntry.Updated);
template.SetTag(“Exif”, photoEntry.Exif.ToString());
template.SetTag(“Height”, accessor.Height);
template.SetTag(“Width”, accessor.Width);
template.SetTag(“Latitude”, accessor.Latitude);
template.SetTag(“Longitude”, accessor.Longitude);
template.SetTag(“PhotoTitle”, accessor.PhotoTitle);
template.SetTag(“PhotoSummary”, accessor.PhotoSummary);
template.SetTag(“Position”, accessor.Position);
template.SetTag(“Rotation”, accessor.Rotation);
template.SetTag(“Size”, accessor.Size);
template.SetTag(“Version”, accessor.Version);

if (photoEntry.Exif.Make != null && photoEntry.Exif.Model != null)
{
template.SetTag(“camera”, photoEntry.Exif.Make.Value + ” ” + photoEntry.Exif.Model.Value);
}
Template tagTemplate = template.GetLoop(“Tags”);
renderTags(tagTemplate, accessor.AlbumId, accessor.Id);
}

private void renderTags(Template template, string albumId, string photoId)
{
if (string.IsNullOrEmpty(albumId) || string.IsNullOrEmpty(photoId)) return;
var query = new TagQuery(PicasaQuery.CreatePicasaUri(_username, albumId, photoId));
PicasaFeed feed = _service.Query(query);

foreach (PicasaEntry entry in feed.Entries)
{
renderTag(template, entry);
template.CommitLoop();
}
}

private static void renderTag(Template template, AtomEntry entry)
{
if (entry == null) throw new ArgumentNullException(“entry”);
template.SetTag(“Text”, entry.Title.Text);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s