TRUVEO - C# Sample 1
I'm Mark Blomsma, a .NET Software Architect and one of the latest additions to the AOL blogging team. I'll be using this space to write about AOL technology and show you how to use it in a .NET environment.
So let's jump right in and look at the TRUVEO API (formerly known as AOL Video Search API).
TRUVEO, a.k.a. AOL Video Search has a number of API's, from a .NET point of view the XML, REST based API is the most easiest to use. In fact it only takes 3 lines of code to invoke and retrieve a response with search results.
Below is a sample application.
Notice how we use a constant for the base URL so we only need to provide the query (=keyword) that we're looking for, then create an XmlDocument to hold the response.
The Load(..) method performs an HTTP GET operation to load the content located at the specified URL, effectively loading the response straight into the document.
We could dump the xml straight to the console, but this actually looks kinda of messy, so instead I created a DumpTitleToConsole method which displays only the title.
private const string cBaseUrl = "http://xml.searchvideo.com/apiv3?appid=1x1jhj64466mi12ia&method=truveo.videos.getVideos&query={0}";
private static void Main(string[] args)
{
string query = args[0]; // retrieve query from command line, some error handling would be in order
// create url to include the query
string url = String.Format(cBaseUrl, query);
// use XmlDocument to directly load search results
XmlDocument doc = new XmlDocument();
// Load(..) will perform an HTTP GET operation to access the REST service
doc.Load(url); // line 3: we have performed the search!
Console.WriteLine("Result for query: {0}.", args[0]);
//Console.WriteLine(doc.OuterXml); //not so pretty
DumpTitleToConsole(doc);
// wait for enter
Console.ReadLine();
return;
}
private static void DumpToConsole(XmlDocument doc)
{
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("av", "http://xml.searchvideo.com");
XmlNodeList nodes = doc.SelectNodes("//av:Video/av:title", mgr);
foreach (XmlNode node in nodes)
{
// each title on new line
Console.WriteLine(node.InnerText);
}
}
This approach above is probably the most simple way of performing a REST/GET operation, yet not the most memory friendly way. So only use this in scenario's where memory usage and performance are not critical.
- Mark Blomsma
[2007-08-26] Updated: AOL Video Search is now called TRUVEO.
- markdeveloper's blog
- Login or register to post comments
- Subscribe
