/*
Created by James Skemp - http://jamesrskemp.com/
Version 1.0
More information at http://strivinglife.com/words/post/Parsing-Yahoo!-Musics-Artist-Web-Services-with-C-and-LINQ-to-XML-Search-for-artists.aspx
Shared under a Creative Commons Attribution 3.0 United States License - http://creativecommons.org/licenses/by/3.0/us/
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Linq;
using System.Data;
namespace JamesRSkemp.WebServices {
class YahooMusic {
///
/// Key used to access Yahoo! Music Web services.
///
private string AppId = "";
///
/// Create a new YahooMusic object.
///
/// Application ID from Yahoo! Developer Network.
public YahooMusic(string appId) {
if (appId.Trim() != "") {
AppId = appId;
} else {
throw new Exception("You must pass a valid API identifier.");
}
}
///
/// Return artists similar to the one passed, with a match percentage.
///
/// The name of the artist to use for the request.
/// DataTable with artist names.
public DataTable GetSimilarArtists(string artistName) {
string requestUrl = "http://us.music.yahooapis.com/artist/v1/list/search/artist/"
+ System.Web.HttpUtility.UrlEncode(artistName.Trim())
+ "?appid=" + AppId + "&response=topsimilar";
string serviceResponse = GetServiceResponse(requestUrl);
var xmlResponse = XElement.Parse(serviceResponse);
var artistsCount = from Artists in xmlResponse.Descendants("TopSimilarArtists").Descendants("Artist")
select new {
name = Artists.Attribute("name").Value
};
DataTable similarArtists = new DataTable();
similarArtists.Columns.Add("Artist");
if (artistsCount.Count() > 0) {
DataRow artistsRow;
foreach (var artist in artistsCount) {
artistsRow = similarArtists.NewRow();
artistsRow["Artist"] = artist.name;
similarArtists.Rows.Add(artistsRow);
}
}
return similarArtists;
}
///
/// Gets the data from an HTTP request.
///
/// The full Url of the request to make.
/// Returns a string with the text returned from the request.
private string GetServiceResponse(string requestUrl) {
string httpResponse = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Timeout = 15000;
HttpWebResponse response = null;
StreamReader reader = null;
try {
response = (HttpWebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
httpResponse = reader.ReadToEnd();
} finally {
if (reader != null) {
reader.Close();
}
if (response != null) {
response.Close();
}
}
return httpResponse;
}
}
}