public async Task <SearchTitleBaseServiceModel> SearchTitleResultsAsync(string movieTitle) { string contentFilePath = GlobalConstants.SearchResultsBoxOfficeMojoLogFilePath; string searchTitleUrl = string.Format(GlobalConstants.BoxOfficeMojoSearchTitleUrl, movieTitle.EncodeToValidUrl()); HtmlNode docNode = await GetDocNode(contentFilePath, searchTitleUrl); var nodes = docNode .SelectNodes("//tr[.//a[starts-with(text(), 'Movie Title')]]/following-sibling::tr[count(td) > 2]"); if (nodes == null) { throw new InvalidOperationException("Box Office Mojo search result failed!"); } var model = new SearchTitleBaseServiceModel(); foreach (HtmlNode tr in nodes) { HtmlNode td1 = tr.SelectSingleNode(".//td[1]"); HtmlNode td2 = tr.SelectSingleNode(".//td[2]"); string href = td1.Element("a").Attributes["href"].Value; string id = Regex.Match(href, @"id=(.*)$").Groups[1].Value; string title = td1.Element("b").InnerText.Trim(); string studio = td2.InnerText.Trim(); var kvp = new KeyValuePair <string, string>($"{title} ({studio})", id); model.SearchTitleResults.Add(kvp); } return(model); }
public async Task <SearchTitleBaseServiceModel> SearchTitleResultsAsync(string movieTitle) { string contentFilePath = GlobalConstants.SearchResultsRottenTomatoesLogFilePath; string searchTitleUrl = string.Format(GlobalConstants.RottenTomatoesSearchTitleUrl, movieTitle.EncodeToValidUrl()); HtmlNode docNode = await GetDocNode(contentFilePath, searchTitleUrl); Match match = Regex.Match(docNode.InnerHtml, @"(\{""actorCount"".*?""tvCount"":\d+\})"); if (!match.Success) { throw new InvalidOperationException("Rotten Tomatoes search result failed!"); } string json = match.Value; var searchTitleResults = JsonConvert .DeserializeObject <RottenTomatoesSearchMovieTitleJsonDtoModel>(json); var model = new SearchTitleBaseServiceModel(); foreach (var movie in searchTitleResults.Movies) { string id = movie.Url.Trim().TrimStart('/'); var kvp = new KeyValuePair <string, string>($"{movie.Name} ({movie.Year})", id); model.SearchTitleResults.Add(kvp); } return(model); }
public async Task <SearchTitleBaseServiceModel> SearchTitleResultsAsync(string movieTitle) { string contentFilePath = GlobalConstants.SearchResultsBlurayDotComLogFilePath; string searchTitleUrl = string.Format(GlobalConstants.BlurayDotComSearchTitleUrl, movieTitle.EncodeToValidUrl()); HtmlNode docNode = await GetDocNode(contentFilePath, searchTitleUrl); var nodes = docNode .SelectNodes("//table[.//h2[contains(text(), 'Search movies')]]/following-sibling::table[.//a[@href and @title]]") .Descendants("a"); if (nodes == null) { throw new InvalidOperationException("Blu-ray.com search result failed!"); } var model = new SearchTitleBaseServiceModel(); foreach (HtmlNode a in nodes) { string href = a.Attributes["href"].Value.Trim(); string id = href .Replace(GlobalConstants.BlurayDotComDomain + "/movies/", string.Empty) .Trim('/'); string title = a.Attributes["title"].Value.Trim(); var kvp = new KeyValuePair <string, string>(title, id); model.SearchTitleResults.Add(kvp); } Console.WriteLine(model); return(model); }
public async Task <SearchTitleBaseServiceModel> SearchTitleResultsAsync(string movieTitle) { string contentFilePath = GlobalConstants.SearchResultsDvdEmpireLogIlePath; string searchTitleUrl = string.Format(GlobalConstants.DvdEmpireSearchTileUrl, movieTitle.EncodeToValidUrl()); HtmlNode docNode = await GetDocNode(contentFilePath, searchTitleUrl); // Search for <h3> with childnodes <a> AND <small> AND sibling <ul> var nodes = docNode .SelectNodes("//div[@id='ListView']/div/*/h3[a and small][../ul]"); if (nodes == null) { throw new InvalidOperationException("DVD Empire search result failed!"); } var model = new SearchTitleBaseServiceModel(); foreach (HtmlNode h3 in nodes) { string href = h3.Element("a").Attributes["href"].Value; string id = href.Trim().TrimStart('/'); string title = h3.Element("a").InnerText.Trim(); HtmlNode small = h3.SelectSingleNode("./small[2]/text()"); HtmlNode span = h3.SelectSingleNode("../ul/li[3]/span/text()"); string productionYear = string.Empty; if (small != null) { productionYear = $"{small.InnerText.Trim()}"; title = $"{title} {productionYear}"; } string releaseDateValue = string.Empty; if (span != null) { releaseDateValue = span.InnerText.Trim(); DateTime releaseDate = releaseDateValue.ParseToDateTime(); title = $"{title} (Home Video Release Date: {releaseDate.ToShortDateString()})"; } var kvp = new KeyValuePair <string, string>($"{title}", id); model.SearchTitleResults.Add(kvp); } return(model); }
public async Task <SearchTitleBaseServiceModel> SearchTitleResultsAsync(string movieTitle) { string contentFilePath = GlobalConstants.SearchResultsImdbLogFilePath; string searchTitleUrl = string.Format(GlobalConstants.ImdbSearchTitleUrl, movieTitle.EncodeToValidUrl()); HtmlNode docNode = await GetDocNode(contentFilePath, searchTitleUrl); var nodes = docNode .SelectNodes("//div[@class='findSection']/table[1]/tr"); if (nodes == null) { throw new InvalidOperationException("IMDb search result failed!"); } var model = new SearchTitleBaseServiceModel(); foreach (HtmlNode tr in nodes) { if (!tr.Descendants("td").Any() || tr.Descendants("td").Count() < 2) { continue; } HtmlNode td2 = tr.SelectSingleNode(".//td[2]"); string title = td2.InnerText.Trim(); title = title.RemoveExtraWhitespace(); string href = td2.Element("a").GetAttributeValue("href", string.Empty); string id = Regex.Match(href, @"(tt\d+)").Value; var kvp = new KeyValuePair <string, string>(title, id); model.SearchTitleResults.Add(kvp); } return(model); }
public async Task<IActionResult> Search(string title) { if (title == null) { return BadRequest(); } MovieSearchTitleFormModel formModel = new MovieSearchTitleFormModel(); formModel.SearchTitleText = title; try { SearchTitleBaseServiceModel blurayDotComServiceModel = await this.blurayDotComService.SearchTitleResultsAsync(title); formModel.BlurayDoComSearchTitleResults = blurayDotComServiceModel.SearchTitleResults.AsNotNull(); } catch (Exception ex) { formModel.BoxOfficeMojoExceptionMessage = ex.Message; await Task.Run(() => ex.Log(nameof(HomeController), nameof(Search))); } try { SearchTitleBaseServiceModel boxOfficeMojoServiceModel = await this.boxOfficeMojoService.SearchTitleResultsAsync(title); formModel.BoxOfficeMojoSearchTitleResults = boxOfficeMojoServiceModel.SearchTitleResults.AsNotNull(); } catch (Exception ex) { formModel.BoxOfficeMojoExceptionMessage = ex.Message; await Task.Run(() => ex.Log(nameof(HomeController), nameof(Search))); } try { SearchTitleBaseServiceModel dvdEmpireServiceModel = await this.dvdEmpireService.SearchTitleResultsAsync(title); formModel.DvdEmpireSearchTitleResults = dvdEmpireServiceModel.SearchTitleResults.AsNotNull(); } catch (Exception ex) { formModel.DvdEmpireExceptionMessage = ex.Message; await Task.Run(() => ex.Log(nameof(HomeController), nameof(Search))); } try { SearchTitleBaseServiceModel imdbServiceModel = await this.imdbService.SearchTitleResultsAsync(title); formModel.ImdbSearchTitleResults = imdbServiceModel.SearchTitleResults.AsNotNull(); } catch (Exception ex) { formModel.ImdbExceptionMessage = ex.Message; await Task.Run(() => ex.Log(nameof(HomeController), nameof(Search))); } try { SearchTitleBaseServiceModel rottenTomatoesServiceModel = await this.rottenTomatoesService.SearchTitleResultsAsync(title); formModel.RottenTomatoesSearchTitleResults = rottenTomatoesServiceModel.SearchTitleResults.AsNotNull(); } catch (Exception ex) { formModel.RottentTomattoesExceptionMessage = ex.Message; await Task.Run(() => ex.Log(nameof(HomeController), nameof(Search))); } return View(formModel); }