static void Main(string[] args) { using (var db = new MoviesDbContext()) { //db.Database.ExecuteSqlCommand("DELETE FROM Movies;"); //db.Database.ExecuteSqlCommand("DELETE FROM Actors;"); } using (var client = new System.Net.TMDb.ServiceClient(ConfigurationManager.AppSettings["TMDB-Api"])) { for (int i = 1, count = 1000; i <= count; i++) { using (var db = new MoviesDbContext()) { var movies = client.Movies.GetTopRatedAsync(null, i, CancellationToken.None).Result; count = movies.PageCount; // keep track of the actual page count foreach (ApiMovie m in movies.Results) { Thread.Sleep(250); var movieApi = client.Movies.GetAsync(m.Id, null, true, CancellationToken.None).Result; var movieDb = db.Movies.FirstOrDefault(p => p.ApiId == movieApi.Id); if (movieDb == null) { movieDb = new Movie(); movieDb.ApiId = movieApi.Id; movieDb.Cast = new List <Actor>(); movieDb.Title = movieApi.Title; movieDb.Summary = movieApi.Overview; movieDb.AirDate = movieApi.ReleaseDate; movieDb.Rating = (double)movieApi.VoteAverage; foreach (var x in movieApi.Credits.Cast.Take(10)) { Thread.Sleep(250); movieDb.Cast.Add(GetOrCreateActor(db, client, x.Id).Result); } db.Movies.Add(movieDb); db.SaveChanges(); System.Console.WriteLine("[Added] " + movieDb.Title); } else { System.Console.WriteLine("[Exists] " + movieDb.Title); } } } } } }
private static async Task <Actor> GetOrCreateActor(MoviesDbContext db, System.Net.TMDb.ServiceClient client, int id) { var person = await client.People.GetAsync(id, true, CancellationToken.None); var actor = db.Actors.FirstOrDefault(p => p.ApiId == id); if (actor == null) { actor = new Actor(); actor.ApiId = person.Id; actor.FullName = person.Name; actor.Bio = person.Biography; db.Actors.Add(actor); } return(actor); }
static void Main(string[] args) { using (var client = new System.Net.TMDb.ServiceClient(ConfigurationManager.AppSettings["TMDB-Api"])) { for (int i = 1, count = 1000; i <= count; i++) { using (IDbConnection dataConnection = new SQLiteConnection(LoadConnectionString())) { var movies = client.Movies.GetTopRatedAsync(null, i, CancellationToken.None).Result; count = movies.PageCount; // keep track of the actual page count int movieCount = 1; foreach (API_Movie m in movies.Results) { Thread.Sleep(250); var movieApi = client.Movies.GetAsync(m.Id, null, true, CancellationToken.None).Result; var movieEntry = dataConnection.Query <Movie>($"select * from Movies where Id = {movieApi.Id}", new DynamicParameters()).FirstOrDefault(); if (movieEntry == null) { movieEntry = new Movie(); movieEntry.Id = movieApi.Id; movieEntry.Cast = new List <Actor>(); movieEntry.Title = movieApi.Title; movieEntry.Summary = movieApi.Overview; movieEntry.AirDate = movieApi.ReleaseDate.HasValue ? movieApi.ReleaseDate.Value.ToShortDateString() : DateTime.Now.ToShortDateString(); movieEntry.Rating = (double)movieApi.VoteAverage; movieEntry.Genres = String.Join(", ", movieApi.Genres.Select(genre => genre.Name).ToArray()); dataConnection.Execute("insert into Movies (Id, Title, Summary, AirDate, Rating, Genres) values (@Id, @Title, @Summary, @AirDate, @Rating, @Genres)", movieEntry); dataConnection.Open(); using (var transaction = dataConnection.BeginTransaction()) { try { foreach (var x in movieApi.Credits.Cast.Take(10)) { Thread.Sleep(250); var MovieActor = new MovieActor(); MovieActor.Movie_Id = movieEntry.Id; MovieActor.Actor_Id = GetOrCreateActor(dataConnection, client, x.Id).Id; dataConnection.Execute("insert into MovieActors (Movie_Id, Actor_Id) values (@Movie_Id, @Actor_Id)", MovieActor); } transaction.Commit(); } catch { transaction.Rollback(); } finally { dataConnection.Close(); } } System.Console.WriteLine("[Added] " + movieEntry.Title); } else { System.Console.WriteLine("[Exists] " + movieEntry.Title); } movieCount++; if (movieCount == 5000) { break; } } } } } }
private static async Task <Actor> GetOrCreateActor(IDbConnection dataConnection, System.Net.TMDb.ServiceClient client, int id) { var person = await client.People.GetAsync(id, true, CancellationToken.None); var actor = dataConnection.Query <Actor>($"select * from Actors where Id = {id}", new DynamicParameters()).FirstOrDefault(); if (actor == null) { actor = new Actor(); actor.Id = person.Id; actor.FullName = person.Name; actor.Bio = person.Biography; dataConnection.Execute("insert into Actors (Id, FullName, Bio) values (@Id, @FullName, @Bio)", actor); } return(actor); }
private void GetAdditional(string pathAndFile, string title, string year, out string thumb, out string plot, out string[] genres, out Actor[] actors, out string[] director, out string[] writer, out string[] producer, out string rating) { // Set out values. thumb = string.Empty; plot = string.Empty; rating = string.Empty; genres = new string[0]; actors = new Actor[0]; director = new string[0]; producer = new string[0]; writer = new string[0]; int retryCount = 5; bool bRetry = false; retryPoint: try { string apiKey = "a8051abec55b1993cc89184181b3d6a3"; System.Net.TMDb.ServiceClient client = new System.Net.TMDb.ServiceClient(apiKey); var result = client.Movies.SearchAsync(title, "en", true, int.Parse(year), 1, System.Threading.CancellationToken.None); result.Wait(); var searchResult = result.Result; if (searchResult.TotalCount >= 1) { App.CountTmdbHits++; int id = searchResult.Results.First().Id; var result2 = client.Movies.GetAsync(id, "en", true, System.Threading.CancellationToken.None); result2.Wait(); var searchResult2 = result2.Result; // Get plot plot = searchResult2.Overview; // Get Thumb thumb = "http://image.tmdb.org/t/p/w500" + searchResult2.Poster; // Get Rating rating = searchResult2.Popularity.ToString(new CultureInfo("en-US")); // Get genres if (searchResult2.Genres != null) { genres = searchResult2.Genres.Select(g => g.Name).ToArray(); } // Get actors if (searchResult2.Credits != null) { if (searchResult2.Credits.Cast != null) { actors = searchResult2.Credits.Cast.Select(c => new Actor() { Name = c.Name, Role = c.Character }).ToArray(); } } // Get director if (searchResult2.Credits != null) { if (searchResult2.Credits.Crew != null) { director = searchResult2.Credits.Crew.Where(c => c.Job == "Director").Select(c => c.Name).ToArray(); } } // Get Writer if (searchResult2.Credits != null) { if (searchResult2.Credits.Crew != null) { writer = searchResult2.Credits.Crew.Where(c => c.Department == "Writing").Select(c => c.Name).ToArray(); } } // Get Producer if (searchResult2.Credits != null) { if (searchResult2.Credits.Crew != null) { producer = searchResult2.Credits.Crew.Where(c => c.Job == "Producer").Select(c => c.Name).ToArray(); } } Log.WriteInformation(string.Format("METADATA for file {0} FOUND", pathAndFile)); } else { App.CountTmdbMiss++; Log.WriteWarning(string.Format("METADATA for file {0} NOT FOUND", pathAndFile)); } } catch (System.AggregateException) { if (--retryCount >= 1) { bRetry = true; Log.WriteInformation(string.Format("The request limit was exceeded for '{0}', taking a pause and retrying...", pathAndFile)); } else { bRetry = false; App.CountTooManyTmdbHits++; Log.WriteWarning(string.Format("The request limit was exceeded for '{0}'", pathAndFile)); } } if (bRetry) { bRetry = false; System.Threading.Thread.Sleep(1000); goto retryPoint; } }