Skip to content

Commit

Permalink
Fixed name search to leverage google to find IMDB ID
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarpouzis committed Mar 21, 2024
1 parent dcd4a94 commit d8b245d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
22 changes: 17 additions & 5 deletions src/MovieTheater.Services/GoogleSearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand All @@ -25,11 +26,22 @@ public class GoogleSearchService

movieName = movieName.Trim();

////Try IMDB page lookup.
//Search using Google:
string GoogleRT = "http://www.google.com/search?num=1&q=" + HttpUtility.UrlEncode(movieName) + " IMDB";
var result = await new HtmlWeb().LoadFromWebAsync(GoogleRT);
var googleNodes = result.DocumentNode.SelectNodes("//html//body//div[@id='main']//a").ToList();
//Try IMDB page lookup.
HttpClient httpClient = new HttpClient();

//Search using Google:
string GoogleQuery = "http://www.google.com/search?num=1&q=" + HttpUtility.UrlEncode(movieName) + " IMDB";

//Set a legitimate client string
string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36";
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);

HttpResponseMessage response = await httpClient.GetAsync(GoogleQuery);
string htmlContent = await response.Content.ReadAsStringAsync();
HtmlDocument newDoc = new HtmlDocument();
newDoc.LoadHtml(htmlContent);

var googleNodes = newDoc.DocumentNode.SelectNodes("//html//body//div[@id='main']//a").ToList();
foreach(var link in googleNodes)
{
var href = link.GetAttributeValue("href", "");
Expand Down
6 changes: 3 additions & 3 deletions src/MovieTheater/Controllers/APIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ public async Task<List<Movie>> GetMoviesFromNames([FromBody]string[] movieNames)
List<Movie> movies = new List<Movie>();
foreach(var movieName in movieNames)
{
//var imdbID = await googleSearchService.FindImdbIdFromMovieName(movieName);
//var movie = await imdb.ImdbApiLookupImdbID(imdbID);
var movie = await omdb.GetMovieByName(movieName);
//OMDB lookup-by-title is very inconsistent. Better to Google for the IMDBID
var imdbID = await googleSearchService.FindImdbIdFromMovieName(movieName);
var movie = await omdb.GetMovie(imdbID);
movies.Add(movie);
}
return movies;
Expand Down

0 comments on commit d8b245d

Please sign in to comment.