/// <summary> /// Import movies to database /// </summary> /// <param name="docs">Documents to import</param> /// <returns><see cref="Task"/></returns> public async Task Import(IEnumerable <BsonDocument> docs) { var documents = docs.ToList(); var loggingTraceBegin = $@"Import {documents.Count} movies started at { DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture) }"; _loggingService.Telemetry.TrackTrace(loggingTraceBegin); var updatedMovies = 0; using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { foreach (var document in documents) { try { var watch = new Stopwatch(); watch.Start(); // Deserialize a document to a movie var movieJson = BsonSerializer.Deserialize <MovieBson>(document); var movie = new Movie { ImdbCode = movieJson.ImdbCode, Url = movieJson.Url, Torrents = movieJson.Torrents.Select(torrent => new TorrentMovie { Url = torrent.Url, DateUploaded = torrent.DateUploaded, DateUploadedUnix = torrent.DateUploadedUnix, Quality = torrent.Quality, Hash = torrent.Hash, Peers = torrent.Peers, Seeds = torrent.Seeds, Size = torrent.Size, SizeBytes = torrent.SizeBytes }).ToList(), DateUploaded = movieJson.DateUploaded, DateUploadedUnix = movieJson.DateUploadedUnix, DownloadCount = movieJson.DownloadCount, MpaRating = movieJson.MpaRating, Runtime = movieJson.Runtime, YtTrailerCode = movieJson.YtTrailerCode, DescriptionIntro = movieJson.DescriptionIntro, TitleLong = movieJson.TitleLong, Rating = movieJson.Rating, Year = movieJson.Year, LikeCount = movieJson.LikeCount, DescriptionFull = movieJson.DescriptionFull, Cast = movieJson.Cast?.Select(cast => new Database.Cast { ImdbCode = cast?.ImdbCode, SmallImage = cast?.SmallImage, CharacterName = cast?.CharacterName, Name = cast?.Name }).ToList(), Genres = movieJson.Genres.Select(genre => new Genre { Name = genre }).ToList(), GenreNames = string.Join(", ", movieJson.Genres.Select(FirstCharToUpper)), Language = movieJson.Language, Slug = movieJson.Slug, Title = movieJson.Title, BackgroundImage = movieJson.BackgroundImage, PosterImage = movieJson.PosterImage }; if (!context.MovieSet.Any(a => a.ImdbCode == movie.ImdbCode)) { await RetrieveAssets(movie).ConfigureAwait(false); context.MovieSet.Add(movie); await context.SaveChangesAsync().ConfigureAwait(false); } else { var existingEntity = await context.MovieSet.Include(a => a.Torrents) .FirstOrDefaultAsync(a => a.ImdbCode == movie.ImdbCode).ConfigureAwait(false); existingEntity.DownloadCount = movie.DownloadCount; existingEntity.LikeCount = movie.LikeCount; existingEntity.Rating = movie.Rating; foreach (var torrent in existingEntity.Torrents) { var updatedTorrent = movie.Torrents.FirstOrDefault(a => a.Quality == torrent.Quality); if (updatedTorrent == null) { continue; } torrent.Peers = updatedTorrent.Peers; torrent.Seeds = updatedTorrent.Seeds; } await context.SaveChangesAsync().ConfigureAwait(false); } watch.Stop(); updatedMovies++; Console.WriteLine(Environment.NewLine); Console.WriteLine( $"{DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture)} UPDATED MOVIE {movie.Title} in {watch.ElapsedMilliseconds} ms. {updatedMovies}/{documents.Count}"); } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } } } // Finish Console.WriteLine(Environment.NewLine); Console.WriteLine("Done processing movies."); var loggingTraceEnd = $@"Import movies ended at { DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture) }"; _loggingService.Telemetry.TrackTrace(loggingTraceEnd); }
/// <summary> /// Retrieve assets for the provided movie /// </summary> /// <param name="tmdbClient"><see cref="TMDbClient"/></param> /// <param name="movie">Movie to update</param> /// <returns></returns> private async Task RetrieveAssets(TMDbClient tmdbClient, Movie movie) { var tmdbMovie = await tmdbClient.GetMovieAsync(movie.ImdbCode, MovieMethods.Images); var tasks = new List <Task> { Task.Run(async() => { if (tmdbMovie.Images?.Backdrops != null && tmdbMovie.Images.Backdrops.Any()) { var backdrop = GetImagePathFromTmdb(tmdbClient, tmdbMovie.Images.Backdrops.Aggregate( (image1, image2) => image1 != null && image2 != null && image1.Width < image2.Width ? image2 : image1)); movie.BackdropImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/backdrop/{backdrop.Split('/').Last()}", backdrop); } }), Task.Run(async() => { if (tmdbMovie.Images?.Posters != null && tmdbMovie.Images.Posters.Any()) { var poster = GetImagePathFromTmdb(tmdbClient, tmdbMovie.Images.Posters.Aggregate( (image1, image2) => image1 != null && image2 != null && image1.VoteAverage < image2.VoteAverage ? image2 : image1)); movie.PosterImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/poster/{poster.Split('/').Last()}", poster); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.BackgroundImage)) { movie.BackgroundImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/background/{movie.BackgroundImage.Split('/').Last()}", movie.BackgroundImage); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.SmallCoverImage)) { movie.SmallCoverImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/cover/small/{movie.SmallCoverImage.Split('/').Last()}", movie.SmallCoverImage); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.MediumCoverImage)) { movie.MediumCoverImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/cover/medium/{movie.MediumCoverImage.Split('/') .Last()}", movie.MediumCoverImage); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.LargeCoverImage)) { movie.LargeCoverImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/cover/large/{movie.LargeCoverImage.Split('/').Last()}", movie.LargeCoverImage); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.MediumScreenshotImage1)) { movie.MediumScreenshotImage1 = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/screenshot/medium/1/{movie.MediumScreenshotImage1 .Split('/') .Last()}", movie.MediumScreenshotImage1); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.MediumScreenshotImage2)) { movie.MediumScreenshotImage2 = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/screenshot/medium/2/{movie.MediumScreenshotImage2 .Split('/') .Last()}", movie.MediumScreenshotImage2); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.MediumScreenshotImage3)) { movie.MediumScreenshotImage3 = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/screenshot/medium/3/{movie.MediumScreenshotImage3 .Split('/') .Last()}", movie.MediumScreenshotImage3); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.LargeScreenshotImage1)) { movie.LargeScreenshotImage1 = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/screenshot/large/1/{movie.LargeScreenshotImage1.Split ('/') .Last()}", movie.LargeScreenshotImage1); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.LargeScreenshotImage2)) { movie.LargeScreenshotImage2 = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/screenshot/large/2/{movie.LargeScreenshotImage2.Split ('/') .Last()}", movie.LargeScreenshotImage2); } }), Task.Run(async() => { if (!string.IsNullOrWhiteSpace(movie.LargeScreenshotImage3)) { movie.LargeScreenshotImage3 = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/screenshot/large/3/{movie.LargeScreenshotImage3.Split ('/') .Last()}", movie.LargeScreenshotImage3); } }), Task.Run(async() => { if (movie.Torrents != null) { foreach (var torrent in movie.Torrents) { torrent.Url = await _assetsService.UploadFile( $@"torrents/{movie.ImdbCode}/{torrent.Quality}/{movie.ImdbCode}.torrent", torrent.Url); } } }), Task.Run(async() => { if (movie.Cast != null) { foreach (var cast in movie.Cast) { if (!string.IsNullOrWhiteSpace(cast.SmallImage)) { cast.SmallImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/cast/{cast.ImdbCode}/{cast.SmallImage.Split ('/') .Last()}", cast.SmallImage); } } } }) }; await Task.WhenAll(tasks); }
/// <summary> /// Retrieve assets for the provided movie /// </summary> /// <param name="movie">Movie to update</param> /// <returns></returns> private async Task RetrieveAssets(Movie movie) { var tmdbMovie = await TmdbClient.GetMovieAsync(movie.ImdbCode, MovieMethods.Images | MovieMethods.Similar) .ConfigureAwait(false); if (tmdbMovie.Images?.Backdrops != null && tmdbMovie.Images.Backdrops.Any()) { var backdrop = GetImagePathFromTmdb(TmdbClient, tmdbMovie.Images.Backdrops.Aggregate((image1, image2) => image1 != null && image2 != null && image1.VoteCount < image2.VoteCount ? image2 : image1).FilePath); movie.BackgroundImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/background/{backdrop.Split('/').Last()}", backdrop).ConfigureAwait(false); } if (tmdbMovie.Images?.Posters != null && tmdbMovie.Images.Posters.Any()) { var poster = GetImagePathFromTmdb(TmdbClient, tmdbMovie.Images.Posters.Aggregate((image1, image2) => image1 != null && image2 != null && image1.VoteCount < image2.VoteCount ? image2 : image1).FilePath); movie.PosterImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/poster/{poster.Split('/').Last()}", poster).ConfigureAwait(false); } if (movie.Torrents != null) { foreach (var torrent in movie.Torrents) { torrent.Url = await _assetsService.UploadFile( $@"torrents/{movie.ImdbCode}/{torrent.Quality}/{movie.ImdbCode}.torrent", torrent.Url).ConfigureAwait(false); } } if (movie.Cast != null) { foreach (var cast in movie.Cast) { if (!string.IsNullOrWhiteSpace(cast.SmallImage)) { cast.SmallImage = await _assetsService.UploadFile( $@"images/{movie.ImdbCode}/cast/{cast.ImdbCode}/{ cast.SmallImage.Split ('/') .Last() }", cast.SmallImage).ConfigureAwait(false); } } } if (!movie.Similars.Any() && tmdbMovie.Similar.TotalResults != 0) { movie.Similars = new List <Similar>(); foreach (var id in tmdbMovie.Similar.Results.Select(a => a.Id)) { try { var res = await TmdbClient.GetMovieAsync(id).ConfigureAwait(false); if (!string.IsNullOrEmpty(res?.ImdbId)) { movie.Similars.Add(new Similar { TmdbId = res.ImdbId }); } } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } } } }
/// <summary> /// Import movies to database /// </summary> /// <param name="rawImports">Documents to import</param> /// <param name="pbar"><see cref="IProgressBar"/></param> /// <returns><see cref="Task"/></returns> public async Task Import(IEnumerable <string> rawImports, IProgressBar pbar) { await TmdbClient.GetConfigAsync(); var imports = rawImports.ToList(); var workBarOptions = new ProgressBarOptions { ForegroundColor = ConsoleColor.Yellow, ProgressCharacter = '─', BackgroundColor = ConsoleColor.DarkGray, }; using (var childProgress = pbar?.Spawn(imports.Count, "step import progress", workBarOptions)) { using (var context = new PopcornContextFactory().CreateDbContext(new string[0])) { foreach (var import in imports) { try { // Deserialize a document to a movie var movieJson = JsonSerializer.Deserialize <MovieJson>(import); if (movieJson.Torrents == null || movieJson.Cast == null) { continue; } var movie = new Movie { ImdbCode = movieJson.ImdbCode, Url = movieJson.Url, Torrents = movieJson.Torrents.Select(torrent => new TorrentMovie { Url = torrent.Url, DateUploaded = torrent.DateUploaded, DateUploadedUnix = torrent.DateUploadedUnix, Quality = torrent.Quality, Hash = torrent.Hash, Peers = torrent.Peers, Seeds = torrent.Seeds, Size = torrent.Size, SizeBytes = torrent.SizeBytes }).ToList(), DateUploaded = movieJson.DateUploaded, DateUploadedUnix = movieJson.DateUploadedUnix, DownloadCount = movieJson.DownloadCount, MpaRating = movieJson.MpaRating, Runtime = movieJson.Runtime, YtTrailerCode = movieJson.YtTrailerCode, DescriptionIntro = movieJson.DescriptionIntro, TitleLong = movieJson.TitleLong, Rating = movieJson.Rating, Year = movieJson.Year, LikeCount = movieJson.LikeCount, DescriptionFull = movieJson.DescriptionFull, Cast = movieJson.Cast?.Select(cast => new Database.Cast { ImdbCode = cast?.ImdbCode, SmallImage = cast?.SmallImage, CharacterName = cast?.CharacterName, Name = cast?.Name }).ToList(), Genres = movieJson.Genres?.Select(genre => new Genre { Name = genre }).ToList() ?? new List <Genre>(), GenreNames = string.Join(", ", movieJson.Genres?.Select(FirstCharToUpper) ?? new List <string>()), Language = movieJson.Language, Slug = movieJson.Slug, Title = movieJson.Title, BackgroundImage = movieJson.BackgroundImage, PosterImage = movieJson.PosterImage }; if (!context.MovieSet.Any(a => a.ImdbCode == movie.ImdbCode)) { await RetrieveAssets(movie); context.MovieSet.Add(movie); await context.SaveChangesAsync(); } else { var existingEntity = await context.MovieSet.Include(a => a.Torrents) .FirstOrDefaultAsync(a => a.ImdbCode == movie.ImdbCode); existingEntity.DownloadCount = movie.DownloadCount; existingEntity.LikeCount = movie.LikeCount; existingEntity.Rating = movie.Rating; foreach (var torrent in existingEntity.Torrents) { var updatedTorrent = movie.Torrents.FirstOrDefault(a => a.Quality == torrent.Quality); if (updatedTorrent == null) { continue; } torrent.Peers = updatedTorrent.Peers; torrent.Seeds = updatedTorrent.Seeds; } await context.SaveChangesAsync(); } childProgress?.Tick(); } catch (Exception ex) { _loggingService.Telemetry.TrackException(ex); } } } // Finish pbar?.Tick(); } }