Blogging on programming and life in general.

Use YouTubes RSS Feed To Output A List of Videos

Ok! I admit it! I posted some incorrect information from one of my previous blog posts to “Dynamically Output A List of YouTube Video’s In ASP.NET”. I stupidly said: “The RSS feed is not structured in a nice enough format to output all the information you may need with ease.” I must have been drunk when I wrote that. As you can see from a sample of their RSS feed below I was wrong:

    <category scheme='' term='bike'/>
    <category scheme='' term='Podcast'/>
    <category scheme='' term='Pedrosa'/>
    <category scheme='' term='motorcycles'/>
    <category scheme='' term='Honda+RC212V'/>
    <category scheme='' term='speed'/>
    <category scheme='' term='Suzuki+GSV-R800'/>
    <category scheme='' term=''/>
    <category scheme='' term='Sports' label='Sport'/>
    <category scheme='' term='Rossi'/>
    <category scheme='' term='motorcycle+road+racing'/>
    <category scheme='' term='motograndprix'/>
    <category scheme='' term='Yamaha+YZR+M1'/>
    <category scheme='' term='Mugello'/>
    <category scheme='' term='Italy'/>
    <category scheme='' term='Stoner'/>
    <category scheme='' term='Ducati+Desmosedici+GP8'/>
    <category scheme='' term='MotoGP'/>
    <category scheme='' term='Lorenzo'/>
    <title type='text'>MotoGP action from Mugello 2009</title>
    <content type='text'>The best of the action from the Gran Premio D&amp;#180;Italia Alice, the fifth round of the 2009 motogp World Championship.</content>
    <link rel='alternate' type='text/html' href=''/>
    <link rel='' type='application/atom+xml' href=''/>
    <link rel='' type='application/atom+xml' href=''/>
    <link rel='self' type='application/atom+xml' href=''/>
      <gd:feedLink href='' countHint='24'/>
      <media:category label='Sport' scheme=''>Sports</media:category>
      <media:description type='plain'>The best of the action from the Gran Premio D&amp;#180;Italia Alice, the fifth round of the 2009 motogp World Championship.</media:description>
      <media:keywords>MotoGP, Italy, Mugello, Podcast, Stoner, Pedrosa, Rossi, Lorenzo, Yamaha+YZR+M1, Ducati+Desmosedici+GP8, Honda+RC212V, Suzuki+GSV-R800, motorcycle+road+racing, motograndprix, motorcycles, bike, speed</media:keywords>
      <media:player url=''/>
      <media:thumbnail url='' height='90' width='120' time='00:00:38.500'/>
      <media:thumbnail url='' height='90' width='120' time='00:00:19.250'/>
      <media:thumbnail url='' height='90' width='120' time='00:00:57.750'/>
      <media:thumbnail url='' height='240' width='320' time='00:00:38.500'/>
      <media:title type='plain'>MotoGP action from Mugello 2009</media:title>
      <yt:duration seconds='77'/>
    <gd:rating average='4.862069' max='5' min='1' numRaters='29' rel=''/>
    <yt:statistics favoriteCount='10' viewCount='1055'/>

Each “entry” element within the RSS feed represents a YouTube video. You are able to extrapolate all the important information about each movie such as Average Score, View Count, Thumbnail Images, Video Description, etc. Really useful stuff!

You may be thinking: Why should I use an RSS feed to retrieve the video information rather than using the YouTube API? Well, using a YouTube API is definitely the easier and most straight-forward method. But what you should be aware that the API only works from .NET 2.0 onwards. There isn’t a YouTube API for .NET 1.1. Unfortunately, I only found this out when I tried to implement the API into one of my .NET 1.1 client sites.

The code I have written below, reads the YouTube RSS feed and stores the information in a DataTable.

private void GetYouTubeData(string YouTubeUrl)
    //Create DataTable to store specific YouTube information
    DataTable dtYouTubeVideoData = new DataTable();

    DataRow drYouTubeVideoData;

    //Link to YouTube RSS feed
    XmlTextReader rssReader = new XmlTextReader(YouTubeUrl);
    XmlDocument xmlDoc = new XmlDocument();

    //Download the XML (via the XmlTextReader)

    //Select all nodes starting with "entry"
    XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName("entry");
    //For each "entry" element found
    foreach (XmlNode node in xmlNodeList)
        drYouTubeVideoData = dtYouTubeVideoData.NewRow();

        //Create a new document, to search through the inner contents
        XmlDocument innerXmlDocument = new XmlDocument();

        // Get movie ID
        drYouTubeVideoData["YouTubeID"] = innerXmlDocument.GetElementsByTagName("id")[0].InnerText.Replace("", "");

        // Get movie title
        drYouTubeVideoData["Title"] = innerXmlDocument.GetElementsByTagName("title")[0].InnerText;

        //Get movie description
        drYouTubeVideoData["Description"] = innerXmlDocument.GetElementsByTagName("content")[0].InnerText;
        //Get the thumbnails
        XmlNodeList mediaTumbnail = innerXmlDocument.GetElementsByTagName("media:thumbnail");

        //Iterate through each thumbnail and only get one thumbnail per <entry>.
        foreach (XmlNode thumbnailNode in mediaTumbnail)
            if (thumbnailNode.Attributes["height"].Value == "90" && thumbnailNode.Attributes["url"].Value.EndsWith("1.jpg"))
                drYouTubeVideoData["ImageUrl"] = thumbnailNode.Attributes["url"].Value;

        //Get movie rating
        XmlNodeList ratings = innerXmlDocument.GetElementsByTagName("gd:rating");

        foreach (XmlNode ratingsNode in ratings)
            drYouTubeVideoData["AverageRatings"] = ratingsNode.Attributes["average"].Value;

        //Get Statistics
        XmlNodeList statistics = innerXmlDocument.GetElementsByTagName("yt:statistics");

        foreach (XmlNode statisticsNode in statistics)
            drYouTubeVideoData["ViewCount"] = statisticsNode.Attributes["viewCount"].Value;



    //Bind YouTube data to repeater
    repVideoList.DataSource = dtYouTubeVideoData;

blog comments powered by Disqus