public BusinessObject <List <RottenTomatoesData> > GetByName(string name) { ValidationUtility.ThrowIfNullOrEmpty(name, "name"); // Example: http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Sideways&page_limit=10&apikey=dgknfyd952s8gkmrgj5rd9xm BusinessObject <List <RottenTomatoesData> > entities = new BusinessObject <List <RottenTomatoesData> >(); string url = string.Format("http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={0}&page_limit=10&apikey={1}", WebUtility.HtmlEncode(name), RottenTomatoesDAO.ApiKey); Dictionary <string, object> data = this.RequestData(url); if (data != null) { ArrayList moviesData = data["movies"] as ArrayList; if (moviesData.Count > 0) { entities.Data = new List <RottenTomatoesData>(); foreach (Dictionary <string, object> movieData in moviesData) { RottenTomatoesData movie = this.MapToMovie(movieData); entities.Data.Add(movie); } } } return(entities); }
private Dictionary <string, object> RequestData(string url) { ValidationUtility.ThrowIfNullOrEmpty(url, "url"); TraceManager.Trace(string.Format("Requesting url '{0}' ...", url.Replace(RottenTomatoesDAO.ApiKey, "[Key]")), TraceTypes.OperationStarted); WebRequest request = HttpWebRequest.Create(url); WebResponse response = null; Dictionary <string, object> data = null; try { response = request.GetResponse(); } catch (Exception ex) { TraceManager.Trace(string.Format("Unable to make request: Exception: {0}. URL: {1}", ex.Message, url), TraceTypes.Error); } if (response != null) { string responseValue; using (StreamReader reader = new StreamReader(response.GetResponseStream())) { responseValue = reader.ReadToEnd(); } if (!string.IsNullOrWhiteSpace(responseValue)) { JavaScriptSerializer serializer = new JavaScriptSerializer(); data = serializer.Deserialize <Dictionary <string, object> >(responseValue); } } return(data); }
protected BusinessObject <dynamic> GetData(string url, string searchTerm) { ValidationUtility.ThrowIfNullOrEmpty(url, "url"); ValidationUtility.ThrowIfNullOrEmpty(searchTerm, "searchTerm"); var returnValue = new BusinessObject <dynamic>(); string searchTermEncoded = WebUtility.HtmlEncode(searchTerm); string requestUrl = string.Format(url, "[Key]", searchTermEncoded); TraceManager.Trace(string.Format("Requesting url '{0}' ...", requestUrl), TraceTypes.OperationStarted); requestUrl = string.Format(url, TheMovieDBDAO.ApiKey, searchTermEncoded); try { var request = WebRequest.Create(requestUrl); using (var response = request.GetResponse()) { using (var stream = response.GetResponseStream()) { string json = new StreamReader(stream).ReadToEnd(); dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json); returnValue.Data = obj; } } } catch (Exception ex) { returnValue.Errors.Add(ex.Message); } return(returnValue); }
private Movie MapToMovie(dynamic data) { ValidationUtility.ThrowIfNullOrEmpty(data, "movieData"); Movie movie = new Movie(); movie.ID = data.id; movie.Name = data.title; movie.AlternateName = data.original_title; movie.IsAdult = data.adult; movie.Created = DateTime.ParseExact(data.release_date.ToString(), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); movie.Popularity = data.popularity; movie.Budget = data.budget ?? 0; movie.Revenue = data.revenue ?? 0; movie.Runtime = data.runtime ?? 0; movie.Tagline = data.tagline; movie.HomepageUrl = data.homepage; movie.IMDBIdValue = (!string.IsNullOrEmpty((string)data.imdb_id)) ? int.Parse(((string)data.imdb_id).Replace("tt", string.Empty)) : 0; movie.Description = data.overview; movie.Images.Add(new Image() { URL = data.poster_path }); if (data.genres != null) { ((IEnumerable <dynamic>)data.genres).ForEach(x => movie.Genres.Add(x.name.ToString())); } if (data.production_companies != null) { ((IEnumerable <dynamic>)data.production_companies).ForEach(x => movie.Studios.Add(x.name.ToString())); } return(movie); }
public BusinessObject <List <EntityBase> > FetchMetadataByName(string name) { ValidationUtility.ThrowIfNullOrEmpty(name, "name"); name = this.ParseName(name); var entities = new TheTVDBDAO().GetShowsByName(name); entities.Data.ForEach((e) => e.Updated = DateTime.Now); return(entities); }
private RottenTomatoesData MapToMovie(Dictionary <string, object> movieData) { ValidationUtility.ThrowIfNullOrEmpty(movieData, "movieData"); RottenTomatoesData data = new RottenTomatoesData(); data.ID = movieData.GetData <int>("id"); data.Name = movieData.GetData <string>("title"); data.MPAARating = movieData.GetData <string>("mpaa_rating"); data.Studio = movieData.GetData <string>("studio"); data.Runtime = movieData.GetData <int>("runtime"); data.Description = string.Concat(movieData.GetData <string>("synopsis"), movieData.GetData <string>("critics_consensus")); Dictionary <string, object> releaseDates = movieData.GetData <Dictionary <string, object> >("release_dates"); data.Created = releaseDates.GetData <DateTime>("theater"); Dictionary <string, object> alternateIDs = movieData.GetData <Dictionary <string, object> >("alternate_ids"); data.IMDBId = string.Concat("tt", alternateIDs.GetData <int>("imdb").ToString()); Dictionary <string, object> ratingData = movieData.GetData <Dictionary <string, object> >("ratings"); data.CriticsRating = new Rating(); data.CriticsRating.Description = ratingData.GetData <string>("critics_rating"); data.CriticsRating.Score = ratingData.GetData <int>("critics_score"); data.AudienceRating = new Rating(); data.AudienceRating.Description = ratingData.GetData <string>("audience_rating"); data.AudienceRating.Score = ratingData.GetData <int>("audience_score"); Dictionary <string, object> postersData = movieData.GetData <Dictionary <string, object> >("posters"); data.Images = new List <Image>(); data.Images.Add(new Image() { MappedType = ImageType.Poster, ThumbnailURL = postersData.GetData <string>("original") }); Dictionary <string, object> linksData = movieData.GetData <Dictionary <string, object> >("links"); data.Url = linksData.GetData <string>("alternate"); ArrayList genres = movieData.GetData <ArrayList>("genres"); if (genres != null) { data.Genres.AddRange(genres.ToArray().Cast <string>()); } return(data); }
protected Show GetShowFromXML(XmlNode seriesNode) { ValidationUtility.ThrowIfNullOrEmpty(seriesNode, "seriesNode"); Show show = new Show(); show.ID = int.Parse(seriesNode.SelectSingleNode("seriesid").InnerText); show.Name = seriesNode.GetNodeValue <string>("SeriesName"); show.Description = seriesNode.GetNodeValue <string>("Overview"); if (!seriesNode.SelectSingleNode("FirstAired").IsNullOrEmpty()) { show.Created = seriesNode.GetNodeValue <DateTime>("FirstAired"); } return(show); }
public BusinessObject <RottenTomatoesData> GetByIMDB(string id) { ValidationUtility.ThrowIfNullOrEmpty(id, "id"); BusinessObject <RottenTomatoesData> entity = new BusinessObject <RottenTomatoesData>(); string url = string.Format("http://api.rottentomatoes.com/api/public/v1.0/movie_alias.json?id={0}&type=imdb", id); Dictionary <string, object> data = this.RequestData(url); if (data != null) { RottenTomatoesData movie = this.MapToMovie(data); entity.Data = movie; } return(entity); }
public BusinessObject <RottenTomatoesData> GetByID(int id) { ValidationUtility.ThrowIfNullOrEmpty(id, "id"); BusinessObject <RottenTomatoesData> entity = new BusinessObject <RottenTomatoesData>(); string url = string.Format("http://api.rottentomatoes.com/api/public/v1.0/movies/{0}.json?apikey={1}", id.ToString(), RottenTomatoesDAO.ApiKey); Dictionary <string, object> data = this.RequestData(url); if (data != null) { RottenTomatoesData movie = this.MapToMovie(data); entity.Data = movie; } return(entity); }
public static void AddToImagesFile(string showDirectoryPath, Image image, string imageFilePath) { ValidationUtility.ThrowIfNullOrEmpty(showDirectoryPath, "showDirectoryPath"); ValidationUtility.ThrowIfNullOrEmpty(image, "image"); ValidationUtility.ThrowIfNullOrEmpty(imageFilePath, "imageFilePath"); DirectoryInfo showDirectory = new DirectoryInfo(showDirectoryPath); if (showDirectory.Exists) { XmlDocument doc = new XmlDocument(); XmlNode imagesNode = null; string imagesFilePath = string.Concat(showDirectory.FullName, "\\Images.xml"); FileInfo imagesFile = new FileInfo(imagesFilePath); if (!imagesFile.Exists) { XmlNode rootNode = doc.AddNode("Root"); imagesNode = rootNode.AddNode("Images"); } else { doc.Load(imagesFilePath); imagesNode = doc.SelectSingleNode("//Root/Images"); if (imagesNode == null) { imagesNode = doc.SelectSingleNode("//Root").AddNode("Images"); } } XmlNode imageNode = imagesNode.AddNode("Image"); imageNode.AddNode("ID", image.ID.ToString()); imageNode.AddNode("Path", imageFilePath); doc.Save(imagesFile.FullName); } else { throw new DirectoryNotFoundException(string.Format("Directory {0} was not found.", showDirectoryPath)); } }
public BusinessObject <List <EntityBase> > GetByName(string name) { ValidationUtility.ThrowIfNullOrEmpty(name, "name"); // Example: http://api.themoviedb.org/3/search/movie?api_key=c78b66672025d9b8a5fcb54a07c6ad84&query=transformers var entities = new BusinessObject <List <EntityBase> >(); var result = this.GetData("http://api.themoviedb.org/3/search/movie?api_key={0}&query={1}", name); if (result.Errors.IsNullOrEmpty()) { if (result.Data != null) { entities.Data = new List <EntityBase>(); ((IEnumerable <dynamic>)result.Data.results).ForEach(m => entities.Data.Add(this.MapToMovie(m))); } } else { entities.Errors.AddRange(result.Errors); } return(entities); }
public static IEntityService GetService(EntityBase entity) { ValidationUtility.ThrowIfNullOrEmpty(entity, "entity"); IEntityService service = null; if (entity is Show) { service = new ShowService(); } else if (entity is Episode) { service = new EpisodeService(); } else if (entity is Season) { service = new SeasonService(); } else if (entity is Movie) { service = new MovieService(); } return(service); }
public static bool DownloadImage(string url, string filename) { ValidationUtility.ThrowIfNullOrEmpty(url, "url"); ValidationUtility.ThrowIfNullOrEmpty(filename, "filename"); ImageFormat imageType = ImageFormat.Jpeg; WebResponse response = null; Stream remoteStream = null; StreamReader readStream = null; try { TraceManager.Trace(string.Format("Making image request for {0}.", url), TraceVerbosity.Minimal, TraceTypes.WebRequest); WebRequest request = WebRequest.Create(url); if (request != null) { response = request.GetResponse(); TraceManager.Trace("Request completed.", TraceTypes.Default); if (response != null) { remoteStream = response.GetResponseStream(); string content_type = response.Headers["Content-type"]; if (content_type == "image/jpeg" || content_type == "image/jpg") { imageType = ImageFormat.Jpeg; } else if (content_type == "image/png") { imageType = ImageFormat.Png; } else if (content_type == "image/gif") { imageType = ImageFormat.Gif; } else { TraceManager.TraceFormat("Unrecognized image type '{0}' at {1}, aborting image download.", content_type, url); return(false); } readStream = new StreamReader(remoteStream); System.Drawing.Image image = System.Drawing.Image.FromStream(remoteStream); if (image == null) { return(false); } TraceManager.Trace(string.Format("Saving file to {0}.", filename), TraceTypes.Default); FileInfo file = new FileInfo(filename); if (!file.Directory.Exists) { file.Directory.Create(); } if (file.Exists) { file.Delete(); } image.Save(filename, imageType); image.Dispose(); } else { TraceManager.Trace(string.Format("Response for {0} was null or empty.", url), TraceTypes.Error); } } } catch (Exception ex) { TraceManager.Trace(string.Format("Download image failed: {0}", ex.Message), TraceVerbosity.Minimal, TraceTypes.Exception, ex); } finally { if (response != null) { response.Close(); } if (remoteStream != null) { remoteStream.Close(); } if (readStream != null) { readStream.Close(); } } return(true); }