Thursday, December 30, 2010

Search Youtube Using C#

In this post I want to show you, how to search Youtube with C#, so searching for videos on that platform.
For that we use the Google Data API, especially the Youtube SDK. With these programming libraries  the access to Youtube & Co pretty easy.
The Google Data API can be downloaded on this page, for access on Youtube we only need this file, the Youtube SDK. We need to install it in order to be able to use it.
Searching through Youtube is done with so called feeds. Feeds are something like the content of a webpage in short form, mostly in plain text. In that way, readers can quickly inform themselves of the newest topics, furthermore many programs read these text files (e.g. search engines use them for indexing). This blog has a feed as well.
Using the Youtube SDK we can read feeds with a C# program, for searching on Youtube we use the feed
This feed is provided by Youtube and lists the to "Suchbegriff" matching search results in text form, the feed can also be viewed in a browser.

Now I want to explain the code, over which this feed can be analyzed in C#. First the downloaded libraries  Google.GData.Client.dll and Google.GData.Extensions.dll have to be included. They are contained in the Redist folder of the install folder (in my case, C:\Program Files (x86)\Google\Google YouTube SDK for .NET\Redist). Including can be done with Project - Add Reference - Browse. Then the following code includes the needed parts:

using Google.GData.Client;
using Google.GData.Extensions;

Furthermore it is important, to set the target frameproject in project properties correctly - to .Net Framework 4 and not .Net  Framework 4 Client Profile! (Since they dlls were compiled for the first one.)
I think the actual source code is self explaining.
The following console application searches for "VLC einbinden" on Youtube and then prints the first 10 results on the console:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Client;
using Google.GData.Extensions;

namespace YoutubeToolkit
    class YoutubeQuery
        static void Main(string[] args)
            FeedQuery Abfrage = new FeedQuery(); // object for handling feeds
            Abfrage.Uri = new Uri(""); // feed URL
            Abfrage.StartIndex = 0; // index, from which on search results should be displayed
            Abfrage.NumberToRetrieve = 10; // maximal number of search results

            Service Service = new Service(); // object to execute the feed query with the FeedQuery Objekt
            AtomFeed Feed = Service.Query(Abfrage); // result of the search query as feed

            // print the title of the video for every search result
            foreach (AtomEntry Ergebnis in Feed.Entries)

1 comment:

  1. Here, you can read great Beginner and Advance C# Programming tutorials...

    C# Programming Tutorials