Wednesday, April 3, 2013

YouTube API with .NET
How to search YouTube videos list Programmatically?

To continue our learning towards YouTube API with .NET this article will provide details how we can search for YouTube video list programmatically. In the last post we learn about pulling list of video uploaded by a specific user.

To demonstrate the example we have following interface designed in Ms-Visual Studio 2010. We have a text box where user can to enter search term. We have a button called "Search Video List" which contains the application logic to connect to YouTubeServices and pull list of videos that matches the search term. In the list view box we will show the video titles, uploader name and uploaded date.

We have added Google Data API references in our project code. The complete code snippet on this form is following. I have kept the application code simple for demonstration purpose. We are reading the YouTube feed and fetching the list of videos in viewcount order.

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";
        
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            listView1.Columns.Add("Video Title");
            listView1.Columns.Add("Uploaded By");
            listView1.Columns.Add("Uploaded Date");
            
            listView1.FullRowSelect = true;

            string youTubeSearchTerm;

            youTubeSearchTerm = textBox1.Text;
            
            YouTubeService myservice = new YouTubeService("aa",myDeveloperKey);

            Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos?q=" + youTubeSearchTerm + "&safeSearch=none&orderby=viewCount");
        
        
            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.Author.ToString(), entry.Updated.ToLongDateString() })));
            }
        }

    }
}

After setting up the design and code in Visual Studio, I connected with the internet as I am pulling the information from an online source (YouTube) and run the application.

I tried to pull the video list for search Term Lady Gaga first and results were as expected.

On second instance I search for list of videos related with Barack Obama.

Popular Posts

Real Time Web Analytics