public static Nullable <int> SearchTrack(ref List <string> genres, string track, string artist = "") { Nullable <int> year = null; string url = lfmRootUrl + "?method=track.search&track=" + HttpUtility.UrlEncode(track); if (artist != "") { url += "&artist=" + HttpUtility.UrlEncode(artist); } url += "&api_key=" + lfmAPIkey + "&format=json"; HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url); myReq.UserAgent = "MyGenres/1.0 +http://martinharran.com"; WebResponse wr = myReq.GetResponse(); Stream receiveStream = wr.GetResponseStream(); StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8); string content = reader.ReadToEnd(); LastFMResult result = LastFMResult.FromJson(content); if (result.Results != null) { for (int c = 0; c < result.Results.Trackmatches.Track.Count(); c++) { //LastFm Track Info LastFM.Track t = result.Results.Trackmatches.Track[c]; string infoUrl = lfmRootUrl + "?method=track.getinfo&api_key=" + lfmAPIkey + "&mbid=" + t.Mbid + "&format=json";; HttpWebRequest infoReq = (HttpWebRequest)WebRequest.Create(infoUrl); infoReq.UserAgent = "MyGenres/1.0 +http://martinharran.com"; WebResponse infoWr = infoReq.GetResponse(); Stream infoReceiveStream = infoWr.GetResponseStream(); StreamReader infoReader = new StreamReader(infoReceiveStream, Encoding.UTF8); String infoContent = infoReader.ReadToEnd(); LastFMInfo.GetInfo infoResult = LastFMInfo.GetInfo.FromJson(infoContent); if (infoResult.Track != null && infoResult.Track.Toptags != null) { for (int tagNo = 0; tagNo < infoResult.Track.Toptags.Tag.Count(); tagNo++) { LastFMInfo.Tag style = infoResult.Track.Toptags.Tag[tagNo]; if (genres == null) { genres = new List <string>(); } string lfmStyle = style.Name; var match = genres .FirstOrDefault(stringToCheck => stringToCheck.Contains(lfmStyle)); if (match == null) { genres.Add(lfmStyle); } } } } } return(year); }
public static string ToJson(this LastFMResult self) => JsonConvert.SerializeObject(self, MusicSaver.MyClasses.Discogs.Converter.Settings);