private SearchResults DetailsToSearchResults(string category, Details details) { SearchResults results = null; switch (category) { case "films": SearchResultsFilm film_results = new SearchResultsFilm (); film_results.Name = details.ProductName; film_results.OriginalTitle = ""; film_results.Rating = 1; film_results.Image = details.ImageUrlMedium; film_results.Directors = details.Directors; film_results.Starring = details.Starring; film_results.Date = details.TheatricalReleaseDate; film_results.Genre = ""; film_results.RunningTime = details.RunningTime; film_results.Country = ""; film_results.Language = ""; film_results.Manufacturer = details.Manufacturer; film_results.Medium = "DVD"; film_results.Comments = details.ProductDescription; results = film_results; break; case "albums": SearchResultsAlbum album_results = new SearchResultsAlbum (); album_results.Name = details.ProductName; album_results.Rating = 1; album_results.Image = details.ImageUrlMedium; album_results.Artists = details.Artists; album_results.Label = details.Manufacturer; album_results.Date = details.ReleaseDate; album_results.Style = ""; album_results.ASIN = details.Asin; if (details.Tracks != null) { for (int i = 0; i < details.Tracks.Length; i++) { album_results.Tracks[i] = details.Tracks[i].TrackName; } } album_results.Medium = "CDRom"; album_results.Runtime = details.RunningTime; album_results.Comments = details.ProductDescription; results = album_results; break; case "books": SearchResultsBook books_results = new SearchResultsBook (); books_results.Name = details.ProductName; books_results.Rating = 1; books_results.Image = details.ImageUrlMedium; books_results.Authors = details.Authors; books_results.Date = details.ReleaseDate; books_results.OriginalTitle = ""; books_results.Genre = ""; books_results.Pages = details.NumberOfPages; books_results.Publisher = details.Manufacturer; books_results.ISBN = details.Isbn; books_results.Country = ""; books_results.Language = ""; books_results.Comments = details.ProductDescription; results = books_results; break; } return results; }
public SearchResults GetInfo(string filmCode) { WebRequest req; WebResponse res; StreamReader sr; Regex regex; MatchCollection matchCollection; string html; SearchResultsFilm searchResults = new SearchResultsFilm (); if (ValidCommand(filmCode)) { req = WebRequest.Create("http://imdb.com/title/tt" + filmCode + "/"); res = req.GetResponse(); sr = new StreamReader(res.GetResponseStream()); html = sr.ReadToEnd(); html = html.Replace ("\n", ""); sr.Close(); res.Close(); // **** IMAGE **** regex = new Regex ("<img border=\"0\" alt=\"cover\" src=\"(?<image>.*?)\""); matchCollection = regex.Matches(html); foreach(Match m in matchCollection) { searchResults.Image = m.Result("${image}"); } // **** TITLE AND YEAR **** regex = new Regex(@"<title>(?<title>.*?) \((?<year>[^\)]*)\)</title>"); matchCollection = regex.Matches(html); foreach(Match m in matchCollection) { searchResults.Name = m.Result("${title}"); searchResults.Date = m.Result("${year}"); } // **** DIRECTOR **** regex = new Regex (@"Directed by</b><br>(?<directors>.*?)<br><br>", RegexOptions.ExplicitCapture); matchCollection = regex.Matches(html); foreach(Match m in matchCollection) { string aux = m.Result("${directors}"); regex = new Regex ("<a href=(.*?)>(?<director>.*?)</a>"); MatchCollection mc = regex.Matches (aux); searchResults.Directors = new string [mc.Count]; int i = 0; foreach (Match m1 in mc) { string director = m1.Result("${director}"); searchResults.Directors[i++] = director; } } // **** COMMENTS **** regex = new Regex(@"Plot Outline:</b> (?<comment>.*?)<a"); matchCollection = regex.Matches(html); foreach(Match m in matchCollection) { searchResults.Comments = m.Result("${comment}"); } // **** RATING **** regex = new Regex(@"User Rating:(.*)<b>(?<rating>.*)/10</b> ((.*) votes)"); matchCollection = regex.Matches(html); foreach(Match m in matchCollection) { string rat = m.Result ("${rating}"); double rating = Double.Parse (rat, System.Globalization.CultureInfo.InvariantCulture.NumberFormat); int rating1 = (int)Math.Round (rating/2); searchResults.Rating = rating1; } // **** CAST **** regex = new Regex (@"Complete credited cast:(?<cast>.*?)(more)"); matchCollection = regex.Matches(html); if (matchCollection.Count == 0) { regex = new Regex (@"Cast overview, first billed only:(?<cast>.*?)(more)"); matchCollection = regex.Matches(html); } foreach(Match m in matchCollection) { string aux = m.Result("${cast}"); regex = new Regex ("<a href=(.*?)>(?<actor>.*?)</a>"); MatchCollection mc = regex.Matches (aux); searchResults.Starring = new string [mc.Count]; int i = 0; foreach (Match m1 in mc) { string actor = m1.Result("${actor}"); searchResults.Starring [i++] = actor; } } // **** RUNTIME **** regex = new Regex (@"Runtime:</b>(?<runtime>.*?) min"); matchCollection = regex.Matches (html); foreach(Match m in matchCollection) { searchResults.RunningTime = m.Result("${runtime}"); } // **** COUNTRY **** regex = new Regex (@"Country:</b>(?<countries>.*?)<br>"); matchCollection = regex.Matches(html); foreach(Match m in matchCollection) { string aux = m.Result("${countries}"); regex = new Regex ("<a href=(.*?)>(?<country>.*?)</a>"); MatchCollection mc = regex.Matches (aux); int i = 0; string country = ""; foreach (Match m1 in mc) { country += m1.Result("${country}"); i++; if (i != mc.Count) { country +=" / "; } } searchResults.Country = country; } // **** LANGUAGE **** regex = new Regex (@"Language:</b>(?<languages>.*?)<br>"); matchCollection = regex.Matches(html); foreach(Match m in matchCollection) { string aux = m.Result("${languages}"); regex = new Regex ("<a href=(.*?)>(?<language>.*?)</a>"); MatchCollection mc = regex.Matches (aux); int i = 0; string language =""; foreach (Match m1 in mc) { language += m1.Result("${language}"); i++; if (i != mc.Count) { language +=" / "; } } searchResults.Language = language; } } return searchResults; }