// get : api/Movies?type={current/all (default)}&resultlimit={default 100} protected override string ProcessRequest() { string type = "all"; int resultLimit = 15; // get query string parameters string queryParameters = this.Request.RequestUri.Query; if (queryParameters != null) { var qpParams = HttpUtility.ParseQueryString(queryParameters); if (!string.IsNullOrEmpty(qpParams["type"])) { type = qpParams["type"].ToString().ToLower(); } if (!string.IsNullOrEmpty(qpParams["resultlimit"])) { int.TryParse(qpParams["resultlimit"].ToString(), out resultLimit); } } try { var tableMgr = new TableManager(); var moviesByName = (type == "all") ? tableMgr.GetSortedMoviesByName() : (type == "current") ? tableMgr.GetCurrentMovies() : (type == "upcoming") ? tableMgr.GetUpcomingMovies() : Enumerable.Empty<MovieEntity>(); List<MovieEntity> movies = moviesByName.Take(resultLimit).ToList(); // serialize movieList object and return. return jsonSerializer.Value.Serialize(movies); } catch (Exception ex) { // if any error occured then return User friendly message with system error message return jsonSerializer.Value.Serialize( new { Status = "Error", UserMessage = "Unable to get " + type + " movies", ActualError = ex.Message }); } }
private List<TwitterEntity> GetUpcomingMovieTweets() { List<TwitterEntity> twitterList = new List<TwitterEntity>(); // Find the list of upcoming movies - Get their Twitter Handle if (CacheManager.TryGet(CacheConstants.TwitterJson + "Upcoming", out twitterList)) { return twitterList; } else { if (twitterList == null) twitterList = new List<TwitterEntity>(); TableManager tbl = new TableManager(); IEnumerable<MovieEntity> upcomingMovies = tbl.GetUpcomingMovies(); if (upcomingMovies != null) { foreach (MovieEntity movie in upcomingMovies) { if (!string.IsNullOrEmpty(movie.TwitterHandle)) { // Call GetMovieTweets method List<TwitterEntity> tweets = GetLatestTweets(movie.TwitterHandle); // Add this movie to cache if (tweets != null && tweets.Count > 0) { List<TwitterEntity> movieTweets = new List<TwitterEntity>(); if (CacheManager.TryGet(CacheConstants.TwitterJson + movie.UniqueName, out movieTweets)) { CacheManager.Remove(CacheConstants.TwitterJson + movie.UniqueName); } CacheManager.Add(CacheConstants.TwitterJson + movie.UniqueName, tweets); } if (tweets != null && tweets.Count > 5) { // Just keep top 5 tweets tweets.RemoveRange(5, tweets.Count - 5); twitterList.AddRange(tweets); } else if (tweets != null) twitterList.AddRange(tweets); // Merge the tweet lists into one } } } if (twitterList.Count > 0) CacheManager.Add(CacheConstants.TwitterJson + "Upcoming", twitterList); } return twitterList; }
/// <summary> /// /// </summary> /// <remarks> /// Prepare JSON object like: /// [ /// [ /// { "UniqueName": "taran-adarsh", "Name": "Taran Adarsh", "Role": "Reviewer", "Weight": "3" }, /// { "UniqueName": "anupama-chopra", "Name": "Anupama Chopra", "Role": "Reviewer", "Weight": "4" }, /// { "UniqueName": "rachit-gupta", "Name": "Rachit Gupta", "Role": "Reviewer", "Weight": "2" } /// ], /// [ /// { "UniqueName": "mickey-virus", "Name": "Mickey Virus", "Role": "Movie", "Weight": "1" }, /// { "UniqueName": "krrish-3", "Name": "Krrish 3", "Role": "Movie", "Weight": "4" } /// ], /// [ /// { "UniqueName": "Deepika-Padukone", "Name": "Deepika Padukone", "Role": "Artists", "Weight": "5" }, /// { "UniqueName": "ranveer-singh", "Name": "Ranveer Singh", "Role": "Artists", "Weight": "4" }, /// { "UniqueName": "aditya-roy-kapoor", "Name": "Aditya Roy Kapoor", "Role": "Artists", "Weight": "1" }, /// { "UniqueName": "sanjay-leela-bhansali", "Name": "Sanjay Leela Bhansali", "Role": "Artists", "Weight": "2" } /// ], /// [ /// { "UniqueName": "Romance", "Name": "Romance", "Role": "Genre", "Weight": "5" }, /// { "UniqueName": "Action", "Name": "Action", "Role": "Genre", "Weight": "3" }, /// { "UniqueName": "Drama", "Name": "Drama", "Role": "Genre", "Weight": "1" } /// ] /// ] /// </remarks> /// <returns></returns> protected override string ProcessRequest() { string popularTags; if (!CacheManager.TryGet<string>(CacheConstants.PopularTagsJson, out popularTags)) { try { var tableMgr = new TableManager(); List<MovieEntity> movieEntities = tableMgr.GetUpcomingMovies() .Concat(tableMgr.GetCurrentMovies()) .ToList(); //int count = movieEntities.Count; int count = 6; //set default 6 if we do not specify a value in web.config file int.TryParse(ConfigurationManager.AppSettings["PopulerMovieCount"], out count); var roleReviewer = new string[] { "Taran Adarsh", "Anupama Chopra", "Rajeev Masand" } .Select(r => new { UniqueName = r.ToLower().Replace(" ", "-"), Name = r, Role = "Reviewer", Weight = "3", }); var roleMovie = movieEntities .OrderByDescending(m => m.Name) .Select(m => new { UniqueName = m.UniqueName, Name = m.Name, Role = "Movie", Weight = "3", }) .Take(count); //count/3 // TODO Fix Actors #region Old code for getting actors in trending section /*var actor = movieEntities .SelectMany(m => m.GetActors()) .GroupBy(g => g) .OrderByDescending(g => g.Count()) .Take(3); var artist = movieEntities .SelectMany(m => m.GetDirectors() .Concat(m.GetMusicDirectors()) .Concat(m.GetProducers())) .GroupBy(g => g) .OrderByDescending(g => g.Count()) .Take(2); var roleArtist = artist .Concat(actor) .Select(a => new { UniqueName = a.Key.ToLower().Replace(" ", "-"), Name = a.Key, Role = "Artists", Weight = a.Count(), }); */ #endregion var actor = movieEntities .SelectMany(m => m.GetActors(jsonSerializer.Value.Deserialize<List<Cast>>(m.Cast))) .GroupBy(g => g) .OrderByDescending(g => g.Count()) .Take(3); var artist = movieEntities .SelectMany(m => m.GetDirectors() .Concat(m.GetMusicDirectors()) .Concat(m.GetProducers())) .GroupBy(g => g) .OrderByDescending(g => g.Count()) .Take(2); //var roleArtist = artist //.Concat(actor) var roleArtist = actor .Select(a => new { UniqueName = a.Key.ToLower().Replace(" ", "-"), Name = a.Key, Role = "Artists", Weight = a.Count(), }); var roleGenre = movieEntities .SelectMany(m => m.Genre.Split(new string[] { " | " }, StringSplitOptions.RemoveEmptyEntries)) .GroupBy(g => g) .OrderByDescending(g => g.Count()) .Take(3) .Select(g => new { UniqueName = g.Key.ToLower().Replace(" ", "-"), Name = g.Key, Role = "Genre", Weight = g.Count(), }); var popular = string.Format( "[{0},{1},{2},{3}]", ////"[{0},{1},{2},{3}]", jsonSerializer.Value.Serialize(roleMovie), jsonSerializer.Value.Serialize(roleArtist), jsonSerializer.Value.Serialize(roleReviewer), jsonSerializer.Value.Serialize(roleGenre)); popularTags = popular; CacheManager.Add<string>(CacheConstants.PopularTagsJson, popularTags); } catch (Exception) { // if any error occured then return User friendly message with system error message return jsonError.Value; } } return popularTags; }
private static void UpdateCache(MovieEntity movie) { // Remove movie from Cache var movieKey = CacheConstants.MovieInfoJson + movie.UniqueName; var isPreviouslyCached = CacheManager.Exists(movieKey); CacheManager.Remove(movieKey); var tableMgr = new TableManager(); // Cache if previously cached or movie is upcoming/current Task.Run(() => { if (isPreviouslyCached || movie.State == "upcoming" || movie.State == "now playing") { MovieInfoController.GetMovieInfo(movie.UniqueName); } }); // Update more Cache Task.Run(() => { // Update cache for AllMovies // Note: We are not updating CacheConstants.AllMovieEntitiesSortedByName here // because typically the name of the movie does not changes as often CacheManager.Remove(CacheConstants.AllMovieEntities); tableMgr.GetAllMovies(); // Update current movies Task.Run(() => { CacheManager.Remove(CacheConstants.UpcomingMovieEntities); var movies = tableMgr.GetCurrentMovies(); }); // Update upcoming movies Task.Run(() => { CacheManager.Remove(CacheConstants.NowPlayingMovieEntities); var movies = tableMgr.GetUpcomingMovies(); }); }); }