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

Powershell script for converting JPG to TIFF

The following Powershell script will convert a batch of JPEG files to TIFF format: #This Code is released under MIT license [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $files_folder = 'C:\path-where-your-jpg-files-are\' $pdfs = get-childitem $files_folder -recurse | where {$_.Extension -match "jpg"} foreach($pdf in $pdfs) { $picture = [System.Drawing.Bitmap]::FromFile( $pdf.FullName ) $tiff = $pdf.FullName.replace('.PDF','').replace('.pdf','').replace('.jpg','').replace('.JPG','') + '.tiff' $picture.Save($tiff) }

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

Alert if file missing using Powershell

The following Powershell script can be used to send an email alert when a file is missing from a folder or it is the same file from a previous check: $path_mask = "yourfile_*.txt" $previous_file_store = "lastfileread.txt" $script_name = "File Check" ###### Functions ########## Function EMailLog($subject, $message) {    $emailTo = "juanito@yourserver.com"    $emailFrom = "alert@yourserver.com"    $smtpserver="smtp.yourserver.com"       $smtp=new-object Net.Mail.SmtpClient($smtpServer)    $smtp.Send($emailFrom, $emailTo, $subject, $message) } Try {    #get files that match the mask    $curr_file = dir $path_mask |  select name    if ($curr_file.count -gt 0)    {        #file found        #check if the file is different from the previous file read        $previous_file = Get-Content $previous_file_store        $curr_file_name = $curr_file.Item(0).Name        if ($