public List <ComicVineIssue> SearchIssue(string volumeName, int issueNumber) { List <ComicVineVolume> comicVineVolumeList = this.FindVolumeIdByName(volumeName); ConcurrentBag <ComicVineIssue> comicVineIssueBag = new ConcurrentBag <ComicVineIssue>(); int parallelThreads = SystemEnvironment.ProcessorCountOptimizedForEnvironment(); Parallel.ForEach(comicVineVolumeList, new ParallelOptions() { MaxDegreeOfParallelism = parallelThreads }, comicVineVolume => { try { if (comicVineVolume != null) { ComicVineIssue detailedComicVineIssue = this.GetComicVineIssue(comicVineVolume.id, issueNumber); if (detailedComicVineIssue.issue_number == issueNumber.ToString()) { comicVineIssueBag.Add(detailedComicVineIssue); } } } catch (AggregateException aggregateException) { foreach (var exception in aggregateException.InnerExceptions) { if (exception is ArgumentException) { // Don't act on this } else { throw exception; } } } }); return(comicVineIssueBag.ToList()); }
private List <ComicVineVolume> FindVolumeIdByName(string volumeName) { string query = null; if (this.SearchType == SearchType.Xml) { query = this.ComicVineAddress + "volumes/?api_key=" + this.ComicVineKey + "&format=xml&field_list=id,name,publisher&filter=name:" + volumeName; } else { query = this.ComicVineAddress + "volumes/?api_key=" + this.ComicVineKey + "&format=json&field_list=id,name,publisher&filter=name:" + volumeName; } Task <ComicVineResponse> comicVineResponse = ComicVineConnection.ConnectAndRequest(query); ConcurrentBag <List <ComicVineVolume> > comicVineVolumeLists = new ConcurrentBag <List <ComicVineVolume> >(); if (comicVineResponse.Result.Status == "OK") { ComicVineResponseData firstData = ComicVineReader.GetFirstVolumeQueryResponse(this.SearchType, comicVineResponse.Result.Response); if (firstData.number_of_total_results > 0) { int parallelThreads = SystemEnvironment.ProcessorCountOptimizedForEnvironment(); int numberOfIterations = (int)Math.Ceiling(((double)firstData.number_of_total_results / (double)firstData.limit)); Parallel.For(0, numberOfIterations, new ParallelOptions() { MaxDegreeOfParallelism = parallelThreads }, i => { int offset = i * firstData.limit; string secondQuery = query + "&offset=" + offset.ToString(); Task <ComicVineResponse> secondResponse = ComicVineConnection.ConnectAndRequest(secondQuery); var volumeList = ComicVineReader.GetVolumeQueryResponse(this.SearchType, secondResponse.Result.Response); comicVineVolumeLists.Add(volumeList); secondResponse = null; }); } } if (this.MatchType == MatchType.AbsoluteMatch) { ConcurrentBag <List <ComicVineVolume> > filteredComicVineVolumeLists = new ConcurrentBag <List <ComicVineVolume> >(); List <ComicVineVolume> filteredComicVineVolumeList = new List <ComicVineVolume>(); foreach (var volumeList in comicVineVolumeLists) { foreach (var volume in volumeList) { if (volume.name == volumeName) { filteredComicVineVolumeList.Add(volume); } } } filteredComicVineVolumeLists.Add(filteredComicVineVolumeList); comicVineVolumeLists = filteredComicVineVolumeLists; } List <ComicVineVolume> comicVineVolumeList = new List <ComicVineVolume>(); foreach (List <ComicVineVolume> comicVineVolume in comicVineVolumeLists) { comicVineVolumeList.AddRange(comicVineVolume); } return(comicVineVolumeList); }