private static Task CreateNYTimesTask(IMDbObj imdbObj, MovieInfo info) { var tcs = new TaskCompletionSource<MovieInfo>(); var year = Convert.ToInt32(imdbObj.Year); var nyTimesUrl = string.Format("http://api.nytimes.com/svc/movies/v2/reviews/search.json?query={0}&api-key={1}&opening-date={2}-01-01;{3}-12-31", imdbObj.Title, _nyTimesKey, year, year + 1); var httpClient = new HttpClient(); var requestTask = httpClient.GetAsync(nyTimesUrl); requestTask.ContinueWith(t1 => { if (t1.Result.IsSuccessStatusCode) { var taskReadObject = requestTask.Result.Content.ReadAsStreamAsync(); taskReadObject.ContinueWith(t2 => { JavaScriptSerializer jsonMaster = new JavaScriptSerializer(); var nyTimesObj = jsonMaster.Deserialize<NYTimesObj>(new StreamReader(t2.Result).ReadToEnd()); if (nyTimesObj.num_results > 0) { info.NYTimesReviews = new List<MovieReview>(); foreach (var critic in nyTimesObj.results) { info.NYTimesReviews.Add(new MovieReview() { Reviewer = critic.byline, Summary = critic.summary_short, Review = critic.capsule_review, Url = critic.link.url }); } tcs.SetResult(info); } }); } }); return tcs.Task; }
private static Task CreateFlickrTask(IMDbObj imdbObj, MovieInfo info) { var tcs = new TaskCompletionSource<MovieInfo>(); var httpClient = new HttpClient(); var flickrUrl = string.Format("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={0}&format=json&nojsoncallback=1&text={1}+{2}&sort=interestingness-desc", _flickrKey, imdbObj.Title, imdbObj.Director); var taskFlickr = httpClient.GetAsync(flickrUrl); taskFlickr.ContinueWith(t => { if (t.Result.IsSuccessStatusCode) { var taskReadFlickrObj = t.Result.Content.ReadAsStreamAsync(); taskReadFlickrObj.ContinueWith(t2 => { JavaScriptSerializer jsonMaster = new JavaScriptSerializer(); var flickrInfo = jsonMaster.Deserialize<FlickrObj>(new StreamReader(t2.Result).ReadToEnd()); info.FlickrPhotos = flickrInfo.Photos.Photo.Select(p => string.Format("http://farm{0}.static.flickr.com/{1}/{2}_{3}.jpg", p.Farm, p.Server, p.Id, p.Secret)).ToList(); tcs.SetResult(info); }); } }); return tcs.Task; }