Skip to main content

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);
                }
            }

Comments

Popular posts from this blog

Power Automate: SFTP action "Test connection failed"

When I added an SFTP create file action to my Power Automate flow ( https://flow.microsoft.com ) , I got the following error in the action step, within the designer: "Test connection failed" To troubleshoot the Power Automate connection, I had to: go the Power Automate portal then "Data"->"Connections"  the sftp connection was there, I clicked on the ellipsis, and entered the connection info It turns out, that screen provides more details about the connection error. In my case, it was complaining that "SSH host key finger-print xxx format is not supported. It must be in 'MD5' format". I had provided the sha fingerprint that WinScp shows. Instead, I needed to use the MD5 version of the fingerprint. To get that, I had to run in command line (I was in a folder that had openssh in it): ssh -o FingerprintHash=md5 mysftpsite.com To get the fingerprint in MD5 format. I took the string (without the "MD5:" part of the string) and put ...

How to create online multiplayer HTML5 games in Contruct2

  Construct2 can use websockets to send and receive messages between games. By using socket-io , we can use a Node.js script as the server and my modification to the socket-io plugin for Construct2 to allow the games to synchronize data between them in real-time. There are two parts to this design: the Node.js server and the Construct2 clients (the games playing). The main part of building an online multiplayer HTML5 game is to plan: how the clients will communicate how often and what to communicate how much of the logic will go into the server and how much to the client. In my sample game, I chose to have each client own a player and have the server just relay messages: Use string messages in the form TypeOfMessage, Parameter1, Paremeter2, Parater3, etc to communicate. Have the clients send their player position about 16 times a second. Whenever their player shoots, the client needs to send a message immediately. Almost all of the game logic will...

How to use Windows SSO with OpenXava

One of the nice things about the .NET web environment is the dead easy way to implement Single Sign On in your web apps through Active Directory authentication. In the Java world there are multiple alternatives to use Windows’ Single Sign On with Java based web apps. One of those alternatives is Waffle . Waffle allows your Java web app to authenticate against Active Directory groups (and users). The only caveat is that your web server needs to be running in Windows, which kind of makes sense. In this article, you will learn the steps required to have your OpenXava web application use Waffle to authenticate your Windows users. The first step is to download Waffle from their site and then copy the JAR files outlined in https://github.com/dblock/waffle/blob/master/Docs/tomcat/TomcatSingleSignOnValve.md to the OpenXava’s tomcat server. In your OpenXava project, create servlets.xml in the Web-inf, containing the following: <!-- the role name (the domain gorup) must be e...