/// <summary> /// Gets all possible data for a given movie reference (Filmreferenz) /// </summary> /// <param name="EngineId"></param> /// <returns></returns> private List <MovieMetaMovieModel> GetDataForMovieReference(string EngineId) { // prepare the result var result = new MovieMetaMovieModel(); result.MetaEngine = "ofdb"; result.Reference = EngineId; // Get all desired search objects var searchObjects = new XPathSelectionName[] { XPathSelectionName.DetailsMovieTitle, XPathSelectionName.DetailsProductionCounty, XPathSelectionName.DetailsYear, XPathSelectionName.DetailsGenres, XPathSelectionName.DetailsPlot, XPathSelectionName.DetailsActors, XPathSelectionName.DetailsCoverImage, XPathSelectionName.DetailsEditions, XPathSelectionName.DetatilsRating }; // Get the url and resolver data for the desired object var searchSelections = from s in OfdbXPathSelections.GetSelections() where searchObjects.Contains(s.Name) select s; // group by url to prevent multiple web requests to one and the same web page var urlGroups = from s in searchSelections group s by s.Url into g select g; // iterate through the url groups foreach (var urlGroup in urlGroups) { // Get the url and replace its placeholder string url = urlGroup.Key; url = SubstitutePlaceHolder(url, EngineId); // Create a web request with the url var request = new XPathAgilityPackSelector(url); foreach (var xObject in urlGroup) { try { // get the value for the xObjects XPaht var value = request.GetXPathValues(xObject.XPath); // switch the object name to assign the result value to the corresondig field within the classes return object switch (xObject.Name) { case XPathSelectionName.DetailsMovieTitle: result.Title = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; case XPathSelectionName.DetailsProductionCounty: result.ProductionCountry = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; case XPathSelectionName.DetailsYear: result.Year = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; case XPathSelectionName.DetailsGenres: result.Genres = xObject.Resolver.Resolve <List <string> >(value); break; case XPathSelectionName.DetailsPlot: result.Plot = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; case XPathSelectionName.DetailsActors: var actors = xObject.Resolver.Resolve <List <MovieMetaActorModel> >(value); result.Actors = actors; break; case XPathSelectionName.DetailsCoverImage: result.ImgUrl = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; case XPathSelectionName.DetailsEditions: result.Editions = xObject.Resolver.Resolve <List <MovieMetaEditionModel> >(value); break; case XPathSelectionName.DetatilsRating: result.Rating = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; } } catch (Exception ex) { if (ex is ArgumentNullException || ex is NullReferenceException) { // mainly occurs if xpath returns no values Log.Error().Write($"Error while parsing result for {xObject.Name} mainly because of emtpy data"); } else { throw ex; } } } } return(new List <MovieMetaMovieModel>() { result }); }
public List <MovieMetaMovieModel> SearchMovieByBarcode(string Barcode) { Log.Debug().Write($"Barcode: {Barcode}"); // prepare the result var result = new MovieMetaMovieModel(); result.MetaEngine = "ofdb"; // Get all desired search objects var searchObjects = new XPathSelectionName[] { XPathSelectionName.SearchBarcodeResultTitle, XPathSelectionName.SearchBarcodeResultEngineId, XPathSelectionName.SearchBarcodeResultYear, XPathSelectionName.SearchBarodeResultCoverImage }; // Get the url and resolver data for the desired object var searchSelections = from s in OfdbXPathSelections.GetSelections() where searchObjects.Contains(s.Name) select s; // group by url to prevent multiple web requests to one and the same web page var urlGroups = from s in searchSelections group s by s.Url into g select g; // iterate through the url groups foreach (var urlGroup in urlGroups) { // Get the url and replace its placeholder string url = urlGroup.Key; url = SubstitutePlaceHolder(url, Barcode); // Create a web request with the url var request = new XPathAgilityPackSelector(url); foreach (var xObject in urlGroup) { try { // get the value for the xObjects XPaht var value = request.GetXPathValues(xObject.XPath); // switch the object name to assign the result value to the corresondig field within the classes return object switch (xObject.Name) { case XPathSelectionName.SearchBarcodeResultTitle: result.Title = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; case XPathSelectionName.SearchBarcodeResultEngineId: result.Reference = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; case XPathSelectionName.SearchBarcodeResultYear: result.Year = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; case XPathSelectionName.SearchBarodeResultCoverImage: result.ImgUrl = xObject.Resolver.Resolve <List <string> >(value).FirstOrDefault(); break; } } catch (NullReferenceException ex) { // mainly occurs if xpath returns no values Log.Error().Write($"Error while fetching barcode {Barcode}"); Log.Debug().Write(ex.StackTrace); } } } return(SearchMovieByEngineId(result.Reference)); }