Request information about movie from IMDB.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
namespace MovieNavigator.Core
{
public class MovieService
{
public MoviesResponse GetMoviesList(string movieTitle, int currentPageNumber)
{
// OMDB API returns 10 items per page by default.
// https://www.omdbapi.com/
var result = new MoviesResponse();
var moviesData = new Movie();
var omdbApiRequestUrl = $"https://www.omdbapi.com/?apikey={Properties.OmdbApiKey}&plot=short&r=json&s={movieTitle}";
if (currentPageNumber > 0)
omdbApiRequestUrl += $"&page={currentPageNumber}";
result.Items = new List<Movie>();
using (var webClient = new WebClient())
{
try
{
var json = webClient.DownloadString(omdbApiRequestUrl);
moviesData = JsonConvert.DeserializeObject<Movie>(json);
result.TotalResults = moviesData.TotalResults;
result.Response = moviesData.Response;
if (!string.IsNullOrEmpty(result.Response) && Convert.ToBoolean(moviesData.Response))
{
foreach (var movie in moviesData.Search.OrderByDescending(x => x.Year))
{
result.Items.Add(new Movie { Title = movie.Title, Year = movie.Year, imdbID = movie.imdbID });
}
}
}
catch (Exception ex)
{
result.Response = "Error";
result.ResponseMessage = ex.Message;
}
}
return result;
}
}
}