private static IEnumerable <Domain.Model.Movie> LoadMovies(int page) { var apiKey = ConfigurationManager.AppSettings["tmd-api-key"]; var request = (HttpWebRequest)WebRequest.Create(String.Format("http://api.themoviedb.org/3/movie/top_rated?api_key={0}&page={1}", apiKey, page)); request.KeepAlive = true; request.Method = "GET"; request.Accept = "application/json"; request.ContentLength = 0; string responseContent; using (var response = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream())) { responseContent = reader.ReadToEnd(); } } var results = JsonConvert.DeserializeObject <RootMovie>(responseContent).results; var movies = new List <Domain.Model.Movie>(); foreach (var result in results) { request = (HttpWebRequest)WebRequest.Create(String.Format("http://api.themoviedb.org/3/movie/{1}?api_key={0}", apiKey, result.id)); request.KeepAlive = true; request.Method = "GET"; request.Accept = "application/json"; request.ContentLength = 0; using (var response = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream())) { responseContent = reader.ReadToEnd(); } } var detail = JsonConvert.DeserializeObject <RootMovieDetail>(responseContent); if (detail.release_date == "") { continue; } var status = _statuses.SingleOrDefault(stat => stat.Status == detail.status); if (status == null) { status = new MovieStatus(detail.status); _statuses.Add(status); } var movie = new Domain.Model.Movie(detail.title, detail.id, DateTime.Parse(detail.release_date), status) { Overview = detail.overview, AverageRating = (decimal?)detail.vote_average }; foreach (var genre in detail.genres) { var selectedGenre = _genres.SingleOrDefault(gen => gen.Name == genre.name); if (selectedGenre == null) { selectedGenre = new Domain.Model.Genre(genre.name); _genres.Add(selectedGenre); } movie.MovieGenres.Add(new MovieGenre(movie, selectedGenre)); } foreach (var productionCompany in detail.production_companies) { var selectedCompany = _productionCompanies.SingleOrDefault(comp => comp.Name == productionCompany.name); if (selectedCompany == null) { selectedCompany = new Domain.Model.ProductionCompany(productionCompany.name); _productionCompanies.Add(selectedCompany); } movie.ProductionCompanyMovies.Add(new ProductionCompanyMovie(selectedCompany, movie)); } request = (HttpWebRequest)WebRequest.Create(String.Format("http://api.themoviedb.org/3/movie/{1}/credits?api_key={0}", apiKey, result.id)); request.KeepAlive = true; request.Method = "GET"; request.Accept = "application/json"; request.ContentLength = 0; using (var response = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream())) { responseContent = reader.ReadToEnd(); } } var credit = JsonConvert.DeserializeObject <RootCredits>(responseContent); foreach (var cast in credit.cast) { var selectedActor = _actors.SingleOrDefault(actor => actor.Name == cast.name); if (selectedActor == null) { selectedActor = new Actor(cast.name); _actors.Add(selectedActor); } movie.Characters.Add(new Character(cast.character, movie, selectedActor)); } movies.Add(movie); } return(movies); }
private static IEnumerable<Domain.Model.Movie> LoadMovies(int page) { var apiKey = ConfigurationManager.AppSettings["tmd-api-key"]; var request = (HttpWebRequest)WebRequest.Create(String.Format("http://api.themoviedb.org/3/movie/top_rated?api_key={0}&page={1}", apiKey, page)); request.KeepAlive = true; request.Method = "GET"; request.Accept = "application/json"; request.ContentLength = 0; string responseContent; using (var response = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream())) { responseContent = reader.ReadToEnd(); } } var results = JsonConvert.DeserializeObject<RootMovie>(responseContent).results; var movies = new List<Domain.Model.Movie>(); foreach (var result in results) { request = (HttpWebRequest)WebRequest.Create(String.Format("http://api.themoviedb.org/3/movie/{1}?api_key={0}", apiKey, result.id)); request.KeepAlive = true; request.Method = "GET"; request.Accept = "application/json"; request.ContentLength = 0; using (var response = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream())) { responseContent = reader.ReadToEnd(); } } var detail = JsonConvert.DeserializeObject<RootMovieDetail>(responseContent); if (detail.release_date == "") { continue; } var status = _statuses.SingleOrDefault(stat => stat.Status == detail.status); if (status == null) { status = new MovieStatus(detail.status); _statuses.Add(status); } var movie = new Domain.Model.Movie(detail.title, detail.id, DateTime.Parse(detail.release_date), status) { Overview = detail.overview, AverageRating = (decimal?)detail.vote_average }; foreach (var genre in detail.genres) { var selectedGenre = _genres.SingleOrDefault(gen => gen.Name == genre.name); if (selectedGenre == null) { selectedGenre = new Domain.Model.Genre(genre.name); _genres.Add(selectedGenre); } movie.MovieGenres.Add(new MovieGenre(movie, selectedGenre)); } foreach (var productionCompany in detail.production_companies) { var selectedCompany = _productionCompanies.SingleOrDefault(comp => comp.Name == productionCompany.name); if (selectedCompany == null) { selectedCompany = new Domain.Model.ProductionCompany(productionCompany.name); _productionCompanies.Add(selectedCompany); } movie.ProductionCompanyMovies.Add(new ProductionCompanyMovie(selectedCompany, movie)); } request = (HttpWebRequest)WebRequest.Create(String.Format("http://api.themoviedb.org/3/movie/{1}/credits?api_key={0}", apiKey, result.id)); request.KeepAlive = true; request.Method = "GET"; request.Accept = "application/json"; request.ContentLength = 0; using (var response = (HttpWebResponse)request.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream())) { responseContent = reader.ReadToEnd(); } } var credit = JsonConvert.DeserializeObject<RootCredits>(responseContent); foreach (var cast in credit.cast) { var selectedActor = _actors.SingleOrDefault(actor => actor.Name == cast.name); if (selectedActor == null) { selectedActor = new Actor(cast.name); _actors.Add(selectedActor); } movie.Characters.Add(new Character(cast.character, movie, selectedActor)); } movies.Add(movie); } return movies; }