Thursday, March 28, 2013

YouTube API with .NET
How to pull top rated videos list from YouTube?

This post is second in series of our learning YouTube API with .NET. In the first post we learn some basic facts about YouTube API and different kind of feeds available from YouTubeServices. In this post we will learn how we can retrieve list of top rated videos from YouTube using .NET

As discussed in the first post, there are Public feeds and Private feeds available on YouTube for developers to read from. We will read a public feed in this post.

For demonstration purpose, I have designed following interface in .NET. I have a list box and a button control on the form. The idea is to pull the list of top rated videos and who had uploaded that video from YouTube and show it in the list box. The button "Pull Video from YouTube" has code logic to connect with YouTubeServices and request the top rated video list and show it in the list box.

In the last post we downloaded the Google Data API. We will be adding the references of Google.GData and Google.GData.YouTube dlls in this example. We also need to provide our developer key as part of our request so that YouTubeServices can identify the requestor.

The code snippet on the form is following: For demonstration purpose, I have kept the code to simple and not added error handling and coding best practices

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.YouTube;
using Google.YouTube;
using Google.GData.Client;
using Google.GData.Extensions.MediaRss;

namespace YouTubeAPI
{
    public partial class Form1 : Form
    {
        string myDeveloperKey;
            
        public Form1()
        {
            myDeveloperKey = "my Long string developer key XXXXXX";
        
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            listView1.Columns.Add("Video Title");
            listView1.Columns.Add("Uploaded By");
            listView1.FullRowSelect = true;
            
            YouTubeService myservice = new YouTubeService("aa",myDeveloperKey);

            Uri videoEntryUrl = new Uri("https://gdata.youtube.com/feeds/api/standardfeeds/top_rated"); 
            FeedQuery fq=new FeedQuery();
            fq.Uri=videoEntryUrl;
            
            Feed<Video> videoFeed = new Feed<Video>(myservice,fq);

            foreach (Video entry in videoFeed.Entries)
            {
                listView1.Items.Add((new ListViewItem(new string[] { entry.Title.ToString(), entry.Uploader.ToString()})));
            }
        }
    }
}

Once I am done with design and coding I connected to the internet and clicked on the button. The result was as expected, the application code logic interacted with the YouTubeServices and pulled the top rated video list.

Please keep this in mind that we are reading a feed which is periodically updated. So assuming that it will match the current list of top rated videos on YouTube is a wrong assumption.

So we are done with our first example, we will have some more examples on this topic soon.

Popular Posts

Real Time Web Analytics