Friday, March 29, 2013

YouTube API with .NET
How to pull region/country specific videos list from YouTube?

This article is next in series of our learning YouTube API with .NET. In the last post we learn about how to read list of top rated videos on YouTube. In this post we will learn to pull region or county specific video list from YouTube using .NET.

To start with we have designed following interface in .NET. We have a list box and a button control on the form. The list box is to show the list of videos pulled from YouTubeService. On the button control we have written .NET code logic to interact with YouTubeServices and pull country or region specific feeds.

In this example we will pull the list of most polular videos from Canada. For each of the country Google has provided a list of Region id that you can use as part of your YouTube service request. For a complete list of Region ID you can visit navigate to Google Developer reference guide.

We have added Google Data API reference in the project. We have used following code snippet on the form.

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 XXXXX";
        
            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("http://gdata.youtube.com/feeds/api/standardfeeds/CA/most_popular?v=2"); 
            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 we are done with the design and writing code on the .NET form, I connected to internet and clicked on button “Pull Videon from You Tube”. The result was as expected, the application connected with YouTubeServices and pulled the most popular video list specific to country Canada.

Popular Posts

Real Time Web Analytics