public static Track GetUpdatedInfo(Track track) { string videoId = string.Empty; string numResults = "10"; XElement document = GetResultsFromYoutubeSearch(track.artist, numResults); var entries = (from node in document.Elements() where node.Name.LocalName == "entry" && !node.Elements().Any(e => e.Name.LocalName == "control") select node).FirstOrDefault(); if (null != entries) { var url = RetrieveUrlFromYoutubeResults(entries); string videoUrl = string.IsNullOrWhiteSpace(url) ? string.Empty : url; string tmp = videoUrl.Substring(26); int index = tmp.IndexOf('?'); videoId = tmp.Substring(0, index); var title = entries.Elements().Where(n => n.Name.LocalName == "title").FirstOrDefault().Value.ToString(); track.source = "Youtube"; track.sourceid = videoId; track.name = title; } else { //Console.WriteLine("No videos found for artist: " + track.artist); } return track; }
/// <summary> /// Updates track information for a given event /// </summary> /// <param name="eventID">Event where the track belongs</param> /// <param name="currentTrack">The track information to be updated</param> private static void UpdateTrack(int eventID, Track currentTrack) { string jsonTrack = JsonConvert.SerializeObject(currentTrack); WebRequest trackPostRequest = WebRequest.Create( string.Format(@"http://tonightsplaylist.herokuapp.com/events/{0}/tracks/{1}.json", eventID, currentTrack.id)); trackPostRequest.Credentials = CredentialCache.DefaultCredentials; trackPostRequest.Method = "PUT"; trackPostRequest.ContentType = "application/json"; byte[] byteArray = Encoding.UTF8.GetBytes(jsonTrack); trackPostRequest.ContentLength = byteArray.Length; Stream dataStream = trackPostRequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); var response = trackPostRequest.GetResponse(); using (var responseReader = new StreamReader(response.GetResponseStream())) { var responseContent = responseReader.ReadToEnd(); //Console.WriteLine(responseContent); } }