Consuming YouTube using XLinq

I had to implement an integration with youtube for a client yesterday. Google provides API’s but they are for Java and PHP. I’m using C# for this project. So I decided to use XLinq to fetch the feeds and parse them into classes for what I needed.

I only need titles, movies and a thumbnail for each item. So I haven’t implemented all of the properties. And I am impressed. From having no exposure to XLinq whatsoever to having it parse feeds and being able to use those feeds in my monorail application took me about an hour. Since this is the very first time I use XLinq I imagine there is room for improvement, please tell me so when I’m wrong.

The classes I show here can be used as follows:

YouTubePlayList.LoadForUser("<>").ForEach(pl => Response.Write("" + pl.Id + ""));

var url = http://gdata.youtube.com/feeds/api/playlists/<
;
YouTubeCollection.LoadFrom(url).ForEach(yt => Response.Write(yt.Title + ", " + yt.MovieUrl + ""));

public class YouTubeCollection : List
    {
        public YouTubeCollection()
        {
        }

        public YouTubeCollection(IEnumerable collection)
            : base(collection)
        {
        }

        public static YouTubeCollection LoadFrom(string uri)
        {
            var feed = XElement.Load(uri);

            XNamespace ns = "http://www.w3.org/2005/Atom";
            XNamespace media = "http://search.yahoo.com/mrss/";

            var list = new YouTubeCollection(from item in feed.Elements(ns + "entry").Elements(media + "group")
                        select new YouTubeItem
                        {
                            Title = item.Element(media + "title").Value,
                            MovieUrl = (from el in item.Elements(media + "content")
                                     where el.Attribute("type").Value == "application/x-shockwave-flash"
                                     select el.Attribute("url").Value).First(),
                             ThumbnailUrl = (from el in item.Elements(media + "thumbnail")
                                             select el.Attribute("url").Value).First()
                        });

            return list;
        }

    }

    public class YouTubePlayList : List
    {
        public YouTubePlayList()
        {
        }

        public YouTubePlayList(IEnumerable collection) : base(collection)
        {
        }

        public static YouTubePlayList LoadForUser(string user)
        {
            var url = string.Format("http://gdata.youtube.com/feeds/api/users/{0}/playlists", user);

            var feed = XElement.Load(url);

            XNamespace ns = "http://www.w3.org/2005/Atom";
            XNamespace gd = "http://schemas.google.com/g/2005";

            var list = new YouTubePlayList(from item in feed.Elements(ns + "entry")
                                           select new YouTubePlayListItem
                                             {
                                                 Name = item.Element(ns + "title").Value,
                                                 Id = item.Element(gd + "feedLink").Attribute("href").Value
                                             });

            return list;
        }
    }

    [DataContract]
    public class YouTubePlayListItem
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Id { get; set; }
    }

    [DataContract]
    public class YouTubeItem
    {
        private string _id;
        [DataMember]
        public string Id
        {
            get
            {
                return MovieUrl.Split('/').Last();
            }
            set
            {
                _id = value;
            }
        }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string MovieUrl { get; set; }

        [DataMember]
        public string ThumbnailUrl { get; set; }
    }

 

Technorati Tags: ,

  1. Cool..Can you post the sample app? :)

  2. Hey Link is really powerful, an easy way to do

    var posts = from item in feed.Elements(ns + “entry”).Elements(media + “group”)
    select new
    {
    Title = item.Element(media + “title”).Value,
    MovieUrl = (from el in item.Elements(media + “content”)
    where el.Attribute(“type”).Value == “application/x-shockwave-flash”
    select el.Attribute(“url”).Value).First(),
    ThumbnailUrl = (from el in item.Elements(media + “thumbnail”)
    select el.Attribute(“url”).Value).First()
    };

    foreach (var post in posts)
    {
    Title = post.Title;
    MovieUrl = post.MovieUrl;
    ThumbnailUrl = post.ThumbnailUrl;
    }

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>