public override void OnResultExecuting(ResultExecutingContext context) { base.OnResultExecuting(context); //Trace.WriteLine("Starting filter"); //save url, userId from session, etc... //create a Userlog for this request var request = context.HttpContext.Request; UserLog userLog = TrackingCreate.CreateUserLog(request); //TrackingDbContext db = new TrackingDbContext(); MovieDbContext db = new MovieDbContext(); db.UserLogs.Add(userLog); db.SaveChanges(); //Trace.WriteLine("raw > url > filepath"); //Trace.WriteLine(context.HttpContext.Request.RawUrl); //Trace.WriteLine(context.HttpContext.Request.Url); //Trace.WriteLine(context.HttpContext.Request.FilePath); context.Controller.ViewBag.filtered = true; //write out all headers //var headers = context.HttpContext.Request.Headers; //string[] keys = // context.HttpContext.Request.Headers.AllKeys; //foreach (string key in keys) { // string msg = string.Format("{0} : {1}", key, headers[key]); // Trace.WriteLine(msg); //} //write out the user agent //string userAgent = context.HttpContext.Request.UserAgent; //Trace.WriteLine(userAgent); //var model = context.Controller.ViewData.Model; //if (model.GetType() == typeof(List<NextFlicksMVC4.Controllers.MoviesController.MovieWithGenreViewModel>)) //{ // Trace.WriteLine("List of models"); //} //test for the custom cookie //HttpCookie cookie = context.HttpContext.Request.Cookies.Get("TestCookie"); //if (cookie != null) { // Trace.WriteLine(cookie.Value); //} //else { // Trace.WriteLine("Cookie does not exist"); //} //Trace.WriteLine("Ending filter"); }
public static void AddGenres(int movieID, Title title) { MovieDbContext db = new MovieDbContext(); db.Configuration.AutoDetectChangesEnabled = false; //genres to database foreach (Genre genre in title.ListGenres) { MovieToGenre movieToGenre = CreateMovieMovieToGenre(movieID, genre); db.MovieToGenres.Add(movieToGenre); } db.SaveChanges(); }
public ActionResult Edit(Movie movie) { MovieDbContext db = new MovieDbContext(); if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("oldIndex"); } return View(movie); }
public ActionResult Details(int movie_ID, List<String> tags, bool anon) { //if (true) { Tools.TraceLine("Looking to add the tags {0}, to movie id {1}", tags[0], movie_ID); MovieDbContext db = new MovieDbContext(); Movie taggedMovie = db.Movies.Find(movie_ID); MovieTag newTag = new MovieTag(); //break the tags down by comma delimeter List<String> seperatedtags = UserInput.DeliminateStrings(tags); seperatedtags = UserInput.StripWhiteSpace(seperatedtags); seperatedtags = UserInput.SanitizeSpecialCharacters(seperatedtags); foreach (string tag in seperatedtags) { //JB note: not sure if FoD() here return null if there's nothing, // I think it return a new Tag() empty which'll mean that // it's never gonna hit null. Not sure though. //JT Note: I'm pretty damn sure I tested this and it works, however you //are probably right and we shoudl switch it to somethign a little less error prone. var tagExists = db.MovieTags.FirstOrDefault(t => t.Name == tag); if (tagExists == null) { //tag doesn't exist, so create it newTag.Name = tag; db.MovieTags.Add(newTag); db.SaveChanges(); } else { //otherwise select the MovieTag where the names match and use that. newTag = db.MovieTags.First(t => t.Name == tag); } UserToMovieToTags UtMtT = new UserToMovieToTags(); UtMtT.TagId = newTag.TagId; UtMtT.UserID = WebSecurity.CurrentUserId; UtMtT.movie_ID = movie_ID; db.UserToMovieToTags.Add(UtMtT); db.SaveChanges(); UtMtTisAnon anonTags = new UtMtTisAnon(); anonTags.UtMtT_ID = UtMtT.UtMtY_ID; anonTags.IsAnon = anon; db.UtMtTisAnon.Add(anonTags); db.SaveChanges(); Tools.TraceLine("added tag {0} to movie_id {1}", UtMtT.TagId, movie_ID); } int id = movie_ID; return RedirectToAction("Details", new { movie_ID = id }); }
public ActionResult DeleteConfirmed(int id) { MovieDbContext db = new MovieDbContext(); Movie movie = db.Movies.Find(id); db.Movies.Remove(movie); db.SaveChanges(); return RedirectToAction("oldIndex"); }
public ActionResult Create(Movie movie) { MovieDbContext db = new MovieDbContext(); if (ModelState.IsValid) { db.Movies.Add(movie); db.SaveChanges(); return RedirectToAction("oldIndex"); } return View(movie); }
public static void PopulateGenresTable() { Tools.TraceLine("In PopulateGenresTable"); var db = new MovieDbContext(); //if genre table is not empty, its probably full and don't do anything //var db = NextFlicksMVC4.Controllers.MoviesController.db; //if (db.Genres.Count() != 0) //{ // Trace.WriteLine("Genre table already is not empty, assuming it's full, so no action was taken"); // return; //} //returns a dict of id to genres Dictionary<string, string> dict = NetFlixAPI.PopulateGenres.CreateDictofGenres(System.Web.HttpContext.Current.Server.MapPath("~/dbfiles/genres.NFPOX")); //create all the genre models List<Genre> genres = new List<Genre>(); foreach (KeyValuePair<string, string> keyValuePair in dict) { //create genres var id = keyValuePair.Key; var genre_string = keyValuePair.Value; Genre genre = NetFlixAPI.PopulateGenres.CreateGenreModel(id, genre_string); //add to list var genreExists = from gen in db.Genres where gen.genre_ID == genre.genre_ID select gen; if(!genreExists.Any()) genres.Add(genre); } //add to and save table Trace.WriteLine(" starting to add genres to the database"); foreach (Genre genre in genres) { db.Genres.Add(genre); } Trace.WriteLine(" starting to savechanges() "); try { db.SaveChanges(); } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) { Tools.TraceLine(" caught error while saving Genres Table. It probably already exists:\n***{0}", ex.Message); } Tools.TraceLine("Out PopulateGenresTable"); }
/// <summary> /// Parse the Netflix Catalog (fixedAPI.nfpox) to get a list of all current genres /// </summary> /// <param name="filepath"></param> public static void BuildMoviesBoxartGenresTables(string filepath) { /* * 1: get a list of hashes form all movies in db to check for duplicates * 2: open file to read line by line * 3: check each line and build a movie out of it * 4: check to see if new movie hash is in list of all hashes from DB( if so discard and move on, else add it to the db) * 5: keep track of movies added so we can then move intoa new loop and tie it to box art and genre data * 5a: possibly just keep trakc of the movie IDs instead of the entire object to pull the movie back up in the next loop * * * */ Tools.TraceLine("In BuildMoviesBoxartGenresTables"); Tools.TraceLine(" starting Full Action"); MovieDbContext db = new MovieDbContext(); db.Configuration.AutoDetectChangesEnabled = false; var start_time = Tools.WriteTimeStamp("starting data read"); //get alist of hashes for all movies in the db so we can make sure not to add a duplicate; List<int> dbHashes = new List<int>(); //keep a list of all movie ids so we can reference them later from the db in another loop and build the genres and box arts later List<int> newMovies = new List<int>(); foreach (Movie dbMovie in db.Movies) { dbHashes.Add(dbMovie.GetHashCode()); } // Go line by line, and parse it for Movie files write movies when we hit 1000 or so. int count = 0; using (StreamReader reader = new StreamReader(filepath)) { Tools.TraceLine(" Starting to read"); string line = reader.ReadLine(); line = line.Trim(); //try { while (line != null) { if (!line.StartsWith("<catalog_title>")) { Tools.TraceLine(" Invalid line of XML, probably CDATA or something, make sure all the lines start with '<cata' \n{0}", line); } else { //parse line for a title, which is what NF returns List<Title> titles = Create.ParseXmlForCatalogTitles(line); //if there was a title to parse add it to the db if (titles != null) { Movie movie = Create.CreateMovie(titles[0]); if (!dbHashes.Contains(movie.GetHashCode())) { //add to DB and dict count++; db.Movies.Add(movie); db.SaveChanges(); //save changes so we can get the movie ID, I hope this doesnt slow me down too bad! //create and add boxarts to db var boxArt = CreateMovieBoxart(movie.movie_ID, titles[0]); db.BoxArts.Add(boxArt); //create and save movies to genres AddGenres(movie.movie_ID, titles[0]); } } else { Tools.TraceLine(" Failed on line {0}\n{1}", count, line); } } if (count % 1000 == 0) { db.SaveChanges(); } line = reader.ReadLine(); } } //any unsaved changes save them db.SaveChanges(); Tools.TraceLine(" Done Saving! Check out Movies/index for a table of the stuff"); var end_time = Tools.WriteTimeStamp(" Done everything"); TimeSpan span = end_time - start_time; Tools. TraceLine(" It took this long:"); Tools.TraceLine(span.ToString()); Tools.TraceLine("Out BuildMoviesBoxartGenresTables"); }
/// <summary> /// Parses the TSV with the IMDB movie data and gets saved to the db /// </summary> /// <param name="imdb_filepath">the path to the omdb.txt with the IMDB movie data</param> /// <param name="numOfMoviesPerLoop">5000 is about 180MB, 10000 was around 240MB 15000 can be as high as 300MB</param> private static void OptimizedImdbTsvParse(string imdb_filepath, int numOfMoviesPerLoop = 5000) { //build a list of hashes MovieDbContext tempDb = new MovieDbContext(); //List<int> omdbHashes = // tempDb.Omdb.Select(item => item.GetHashCode()).ToList(); //create a list of hashes for the omdbentrys in the db List<int> omdbHashes = new List<int>(); foreach (OmdbEntry omdb in tempDb.Omdb) { omdbHashes.Add(omdb.GetHashCode()); } var omdbDistinctHashes = omdbHashes .Distinct() .ToList(); //read numOfMoviesPerLoop movies until all the IMDB movies are parsed //5000 is about 180MB, 10000 was around 240MB 15000 can be as high as 300MB " using ( CsvReader imdb_csvReader = new CsvReader(new StreamReader(imdb_filepath), true, '\t', '~', '`', '~', ValueTrimmingOptions.None)) { while (imdb_csvReader.ReadNextRecord()) { //loop through the imdbtsv, creating a omdbentry for the first 500 items List<OmdbEntry> small_omdbEntry_list = new List<OmdbEntry>(); for (int i = 0; i < numOfMoviesPerLoop; i++) { //read the row and create an omdb from it var entry = Omdb.CreateOmdbEntryFromTsvRecord( imdbReader: imdb_csvReader); small_omdbEntry_list.Add(entry); //if nothing left to read, break out of the loop if (imdb_csvReader.ReadNextRecord() == false) { Tools.TraceLine( "ReadNextRecord was false, breaking out of loop to save it"); break; } } //save the small_omdbEntry_list to db, but check for dupes first MovieDbContext db = new MovieDbContext(); db.Configuration.AutoDetectChangesEnabled = false; //for each omdbentry hash not in the list of hashes, add to db foreach ( OmdbEntry omdbEntry in small_omdbEntry_list.Where( omdbEntry => !omdbHashes.Contains(omdbEntry.GetHashCode()))) { db.Omdb.Add(omdbEntry); } Tools.TraceLine( "saving omdbs. # of omdbs in table before save: {0}", db.Omdb.Count()); db.SaveChanges(); db.Configuration.AutoDetectChangesEnabled = true; } Tools.TraceLine("Done saving IMDB OmdbEntrys"); } }
/// <summary> /// Parsed a TSV for RT data then adds the new data to existing IMDB OmdbEntrys in the db /// </summary> /// <param name="tom_filepath"></param> public static void OptimizedRtTsvParse(string tom_filepath) { //i don't think this is necessary, because you always want to update the RT data ////build a list of hashes //MovieDbContext tempDb = new MovieDbContext(); //List<int> omdbHashes = // tempDb.Omdb.Select(item => item.GetHashCode()) // .Distinct() // .ToList(); int num_of_RT_movies_per_loop = 5000; using ( CsvReader tom_csvReader = new CsvReader(new StreamReader(tom_filepath), true, '\t', '~', '`', '~', ValueTrimmingOptions.None)) { while (tom_csvReader.ReadNextRecord()) { //loop through the imdbtsv, creating a omdbentry for the first 5000 items List<OmdbEntry> new_tom_omdb_entries = new List<OmdbEntry>(); for (int i = 0; i < num_of_RT_movies_per_loop; i++) { //read the row and create an omdb from it parse the current TSV row var entry = Omdb.CreateOmdbEntryFromTsvRecord(tomReader: tom_csvReader); // add entry to a list new_tom_omdb_entries.Add(entry); //if nothing left to read, break out of the loop if (tom_csvReader.ReadNextRecord() == false) { Tools.TraceLine( "ReadNextRecord was false, breaking out of loop to save it"); break; } } //find all existing IMDB entries in db MovieDbContext db = new MovieDbContext(); db.Configuration.AutoDetectChangesEnabled = true; //find all existing OE that match the omdb_ids of the listed ones Tools.TraceLine("items in db.Omdb {0}", db.Omdb.Count()); //get the omdb_ids of the new RT omdbentrys List<int> tom_omdb_ids_to_match = new_tom_omdb_entries.Select(omdb => omdb.ombd_ID) .ToList(); //match the ids to the exist IMDB omdbentries var res = (from imdb in db.Omdb where tom_omdb_ids_to_match.Contains(imdb.ombd_ID) select imdb); //Tools.TraceLine("items in res {0}", res.Count()); List<OmdbEntry> matched_existing_imdb_omdbentrys = res.ToList(); //alter the existing IMDB entries and save the changes... foreach ( OmdbEntry matchedExistingImdbOmdbentry in matched_existing_imdb_omdbentrys) { //the RT omdb tha matches the imdb entry for the omdb_id var matching_RT_data = new_tom_omdb_entries.First( item => item.ombd_ID == matchedExistingImdbOmdbentry.ombd_ID); //updates the IMDB OmdbEntry with the RT OmdbEntry's information UpdateImdbEntryWithRtEntry(matchedExistingImdbOmdbentry, matching_RT_data); } //save the updated OmdbEntry information, dispose of the context db.SaveChanges(); db.Dispose(); Tools.TraceLine("Updated existing IMDB OmdbEntrys, done saving"); } } }