Saturday, August 28, 2010

Class to get Live Photo and Album Information in .NET

Thanks to the Windows Live Spaces Photo Album plugin project at CodePlex, I was able to abstract the code to get Album and Pictures information from a Live Spaces account. This class serves as a helper when generating javascript code for slideshows containing photos stored at Live Spaces.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using HtmlAgilityPack;
using System.Web;

namespace LivePhotoDataProvider
{
    public class AlbumInfo
    {
        public string URL { get; set; }
        public string Name { get; set; }
    }
    public class PhotoInfo
    {
        public string FullSizeURL { get; set; }
    }

    public class LivePhotoData
    {
        public List<AlbumInfo> GetAlbums(string live_spaces_link)
        {
            List<AlbumInfo> albums = new List<AlbumInfo>();
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(live_spaces_link);
            }
            catch (Exception e)
            {
                //TODO: handle the exception your way
	return albums;
            }

            // Need to add the MSN namespace that appears in the Live Space RSS feed
            XmlNamespaceManager nmgr = new XmlNamespaceManager(doc.NameTable);
            nmgr.AddNamespace("live", "http://schemas.microsoft.com/live/spaces/2006/rss/");
            nmgr.AddNamespace("cf", "http://www.microsoft.com/schemas/rss/core/2005");

            string xml = doc.InnerXml;
            XmlNodeList NL = doc.SelectNodes("//item[live:type='photoalbum']", nmgr);
            if (NL.Count == 0)
            {
                //MessageBox.Show("No photo albums were found on this blog, are you sure there are meant to be some?",
                //    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //Invoke(m_hideloading);
                return albums;
            }

            string url_list = "";

            // Adds the relevant details to the respective string List and also adds the element
            // to the photoalbum combobox

            foreach (XmlNode node in NL)
            {
                string name = node["title"].InnerText;
                string url = node["cf:itemRSS"].InnerText;
                string code = node["guid"].InnerText;
             //   Invoke(m_combo1addstring, new object[] { name });

                albums.Add(new AlbumInfo() { Name = name, URL = url});
                //AlbumCode.Add(code);
                //AlbumName.Add(name);
                //AlbumUrl.Add(url);
                //url_list += GetPicsURLs(url);
            }
            return albums;
        }

        public List<PhotoInfo> GetPics(string album_url)
        {
            List<PhotoInfo> photos = new List<PhotoInfo>();

            // Opens the photo album feed
            XmlDocument picdoc = new XmlDocument();
            try
            {
                picdoc.Load(album_url);
            }
            catch (Exception e)
            {
                //TODO: handle the exception your way
                return photos;
            }

            XmlNodeList piclist = picdoc.SelectNodes("//item");

            string url_list = "";
            // Gets all the picture information and puts them in the relative lists
            foreach (XmlNode node in piclist)
            {
                string link = node["link"].InnerText;

                photos.Add( GetPicFromURL(link));
            }


            return photos;
        }

         private PhotoInfo GetPicFromURL(string imageurl)
        {
            //get xhtml doc
             PhotoInfo photo = new PhotoInfo();
            HtmlAgilityPack.HtmlDocument picdoc = new HtmlAgilityPack.HtmlDocument();
            var response = System.Net.HttpWebRequest.Create(imageurl).GetResponse().GetResponseStream();
            try
            {
                picdoc.Load(response);
            }
            catch (Exception e)
            {
                //TODO: handle the exception your way
                return photo;
            }
            string full_imager_url = "";
            HtmlNode preview_link = picdoc.DocumentNode.SelectSingleNode("//a[@id='spPreviewLink']");
            full_imager_url = preview_link.Attributes["href"].Value;
            string decoded = HttpUtility.HtmlDecode(full_imager_url);

            photo.FullSizeURL = decoded;
            return photo;
        }
    }
}

You can use the class like this:

            LivePhotoData app = new LivePhotoData();
            var albums = app.GetAlbums("http://picturethat.spaces.live.com/feed.rss");
            foreach (var album in albums)
            {
                Console.Out.WriteLine(album.Name);
                Console.Out.WriteLine(album.URL);
                var pics = app.GetPics(album.URL);
                foreach (var pic in pics)
                {
                    Console.Out.WriteLine(pic.FullSizeURL);
                }
            }

0 comments: