コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Import shows to database
        /// </summary>
        /// <param name="rawImports">Imports 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 show
                            var showJson =
                                JsonSerializer.Deserialize <ShowJson>(import);

                            if (showJson.Year == null)
                            {
                                continue;
                            }

                            var show = new Show
                            {
                                Rating = new Rating
                                {
                                    Hated      = showJson.Rating.Hated,
                                    Percentage = Convert.ToInt32(showJson.Rating.Percentage),
                                    Votes      = showJson.Rating.Votes,
                                    Loved      = showJson.Rating.Loved,
                                    Watching   = showJson.Rating.Watching
                                },
                                Images = new ImageShow
                                {
                                    Banner = showJson.Images?.Banner,
                                    Poster = showJson.Images?.Poster
                                },
                                ImdbId  = showJson.ImdbId,
                                Title   = WebUtility.HtmlDecode(showJson.Title),
                                Year    = int.Parse(showJson.Year),
                                Runtime = showJson.Runtime,
                                Genres  = showJson.Genres.Select(genre => new Genre
                                {
                                    Name = genre
                                }).ToList(),
                                GenreNames =
                                    string.Join(", ", showJson.Genres.Select(FirstCharToUpper)),
                                Slug        = showJson.Slug,
                                LastUpdated = showJson.LastUpdated,
                                TvdbId      = showJson.TvdbId,
                                NumSeasons  = showJson.NumSeasons,
                                Status      = showJson.Status,
                                Synopsis    = showJson.Synopsis,
                                Country     = showJson.Country,
                                Episodes    = showJson.Episodes.Select(episode => new EpisodeShow
                                {
                                    Title     = WebUtility.HtmlDecode(episode.Title),
                                    DateBased = episode.DateBased,
                                    TvdbId    = episode.TvdbId != null &&
                                                int.TryParse(episode.TvdbId.ToString(), out var tvdbId)
                                        ? tvdbId
                                        : 0,
                                    Torrents = new TorrentNode
                                    {
                                        Torrent0 = new Torrent
                                        {
                                            Url   = episode.Torrents.Torrent_0?.Url,
                                            Peers = episode.Torrents.Torrent_0?.Peers,
                                            Seeds = episode.Torrents.Torrent_0?.Seeds
                                        },
                                        Torrent1080p = new Torrent
                                        {
                                            Url   = episode.Torrents.Torrent_1080p?.Url,
                                            Peers = episode.Torrents.Torrent_1080p?.Peers,
                                            Seeds = episode.Torrents.Torrent_1080p?.Seeds
                                        },
                                        Torrent480p = new Torrent
                                        {
                                            Url   = episode.Torrents.Torrent_480p?.Url,
                                            Peers = episode.Torrents.Torrent_480p?.Peers,
                                            Seeds = episode.Torrents.Torrent_480p?.Seeds
                                        },
                                        Torrent720p = new Torrent
                                        {
                                            Url   = episode.Torrents.Torrent_720p?.Url,
                                            Peers = episode.Torrents.Torrent_720p?.Peers,
                                            Seeds = episode.Torrents.Torrent_720p?.Seeds
                                        }
                                    },
                                    EpisodeNumber =
                                        int.TryParse(episode.EpisodeNumber.ToString(), out var episodeNumber)
                                            ? episodeNumber
                                            : 0,
                                    Season     = int.TryParse(episode.Season.ToString(), out var season) ? season : 0,
                                    Overview   = episode.Overview,
                                    FirstAired = episode.FirstAired
                                }).ToList(),
                                AirDay  = showJson.AirDay,
                                AirTime = showJson.AirTime,
                                Network = showJson.Network
                            };

                            if (!context.ShowSet.Any(a => a.ImdbId == show.ImdbId))
                            {
                                await UpdateImagesAndSimilarShow(show);

                                context.ShowSet.Add(show);
                            }
                            else
                            {
                                var existingEntity = await context.ShowSet.Include(a => a.Rating)
                                                     .Include(a => a.Episodes)
                                                     .ThenInclude(episode => episode.Torrents)
                                                     .ThenInclude(torrent => torrent.Torrent0)
                                                     .Include(a => a.Episodes)
                                                     .ThenInclude(episode => episode.Torrents)
                                                     .ThenInclude(torrent => torrent.Torrent1080p)
                                                     .Include(a => a.Episodes)
                                                     .ThenInclude(episode => episode.Torrents)
                                                     .ThenInclude(torrent => torrent.Torrent480p)
                                                     .Include(a => a.Episodes)
                                                     .ThenInclude(episode => episode.Torrents)
                                                     .ThenInclude(torrent => torrent.Torrent720p)
                                                     .Include(a => a.Genres)
                                                     .Include(a => a.Images)
                                                     .Include(a => a.Similars).FirstOrDefaultAsync(a => a.ImdbId == show.ImdbId)
                                ;

                                existingEntity.Rating.Hated      = show.Rating.Hated;
                                existingEntity.Rating.Loved      = show.Rating.Loved;
                                existingEntity.Rating.Percentage = show.Rating.Percentage;
                                existingEntity.Rating.Votes      = show.Rating.Votes;
                                existingEntity.Rating.Watching   = show.Rating.Watching;
                                existingEntity.AirDay            = show.AirDay;
                                existingEntity.AirTime           = show.AirTime;
                                existingEntity.Status            = show.Status;
                                existingEntity.NumSeasons        = show.NumSeasons;
                                foreach (var episode in existingEntity.Episodes)
                                {
                                    var updatedEpisode = show.Episodes.FirstOrDefault(a => a.TvdbId == episode.TvdbId);
                                    if (updatedEpisode == null)
                                    {
                                        continue;
                                    }
                                    if (episode.Torrents?.Torrent0 != null && updatedEpisode.Torrents.Torrent0 != null)
                                    {
                                        episode.Torrents.Torrent0.Peers = updatedEpisode.Torrents.Torrent0.Peers;
                                        episode.Torrents.Torrent0.Seeds = updatedEpisode.Torrents.Torrent0.Seeds;
                                        if (!string.IsNullOrWhiteSpace(updatedEpisode.Torrents.Torrent0.Url))
                                        {
                                            episode.Torrents.Torrent0.Url = updatedEpisode.Torrents.Torrent0.Url;
                                        }
                                    }

                                    if (episode.Torrents?.Torrent1080p != null &&
                                        updatedEpisode.Torrents.Torrent1080p != null)
                                    {
                                        episode.Torrents.Torrent1080p.Peers =
                                            updatedEpisode.Torrents.Torrent1080p.Peers;
                                        episode.Torrents.Torrent1080p.Seeds =
                                            updatedEpisode.Torrents.Torrent1080p.Seeds;
                                        if (!string.IsNullOrWhiteSpace(updatedEpisode.Torrents.Torrent1080p.Url))
                                        {
                                            episode.Torrents.Torrent1080p.Url =
                                                updatedEpisode.Torrents.Torrent1080p.Url;
                                        }
                                    }

                                    if (episode.Torrents?.Torrent720p != null &&
                                        updatedEpisode.Torrents.Torrent720p != null)
                                    {
                                        episode.Torrents.Torrent720p.Peers = updatedEpisode.Torrents.Torrent720p.Peers;
                                        episode.Torrents.Torrent720p.Seeds = updatedEpisode.Torrents.Torrent720p.Seeds;
                                        if (!string.IsNullOrWhiteSpace(updatedEpisode.Torrents.Torrent720p.Url))
                                        {
                                            episode.Torrents.Torrent720p.Url = updatedEpisode.Torrents.Torrent720p.Url;
                                        }
                                    }

                                    if (episode.Torrents?.Torrent480p != null &&
                                        updatedEpisode.Torrents.Torrent480p != null)
                                    {
                                        episode.Torrents.Torrent480p.Peers = updatedEpisode.Torrents.Torrent480p.Peers;
                                        episode.Torrents.Torrent480p.Seeds = updatedEpisode.Torrents.Torrent480p.Seeds;
                                        if (!string.IsNullOrWhiteSpace(updatedEpisode.Torrents.Torrent480p.Url))
                                        {
                                            episode.Torrents.Torrent480p.Url = updatedEpisode.Torrents.Torrent480p.Url;
                                        }
                                    }
                                }

                                var newEpisodes =
                                    show.Episodes.Except(existingEntity.Episodes, new EpisodeComparer());
                                foreach (var newEpisode in newEpisodes.ToList())
                                {
                                    existingEntity.Episodes.Add(newEpisode);
                                }

                                if (existingEntity.Episodes.Any())
                                {
                                    var lastEpisode = existingEntity.Episodes.OrderBy(a => a.FirstAired).Last();
                                    existingEntity.LastUpdated = lastEpisode.FirstAired;
                                }
                            }

                            await context.SaveChangesAsync();

                            childProgress?.Tick();
                        }
コード例 #3
0
        /// <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();
            }
        }
コード例 #4
0
        /// <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;
            var tmdbClient    = new TMDbClient(Constants.TmdbClientApiKey);

            tmdbClient.GetConfig();
            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 Database.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 Database.Genre
                            {
                                Name = genre
                            }).ToList(),
                            GenreNames             = string.Join(", ", movieJson.Genres.Select(FirstCharToUpper)),
                            Language               = movieJson.Language,
                            Slug                   = movieJson.Slug,
                            Title                  = movieJson.Title,
                            BackdropImage          = movieJson.BackdropImage,
                            BackgroundImage        = movieJson.BackgroundImage,
                            LargeCoverImage        = movieJson.LargeCoverImage,
                            LargeScreenshotImage1  = movieJson.LargeScreenshotImage1,
                            LargeScreenshotImage2  = movieJson.LargeScreenshotImage2,
                            LargeScreenshotImage3  = movieJson.LargeScreenshotImage3,
                            MediumCoverImage       = movieJson.MediumCoverImage,
                            MediumScreenshotImage1 = movieJson.MediumScreenshotImage1,
                            MediumScreenshotImage2 = movieJson.MediumScreenshotImage2,
                            MediumScreenshotImage3 = movieJson.MediumScreenshotImage3,
                            SmallCoverImage        = movieJson.SmallCoverImage,
                            PosterImage            = movieJson.PosterImage
                        };

                        var existingEntity =
                            await context.MovieSet.Include(a => a.Torrents)
                            .Include(a => a.Cast)
                            .Include(a => a.Genres).Include(a => a.Similars).FirstOrDefaultAsync(a => a.ImdbCode == movie.ImdbCode);

                        if (existingEntity == null)
                        {
                            try
                            {
                                var tmdbMovie = await TmdbClient.GetMovieAsync(movie.ImdbCode, MovieMethods.Similar);

                                if (tmdbMovie.Similar.TotalResults != 0)
                                {
                                    movie.Similars = new List <Similar>();
                                    await tmdbMovie.Similar.Results.Select(a => a.Id)
                                    .ParallelForEachAsync(async id =>
                                    {
                                        var res = await TmdbClient.GetMovieAsync(id);
                                        if (!string.IsNullOrEmpty(res?.ImdbId))
                                        {
                                            movie.Similars.Add(new Similar
                                            {
                                                TmdbId = res.ImdbId
                                            });
                                        }
                                    });
                                }

                                await movie.Torrents.ParallelForEachAsync(async torrent =>
                                {
                                    torrent.Url = await _assetsService.UploadFile(
                                        $@"torrents/{movie.ImdbCode}/{torrent.Url.Split('/').Last()}.torrent",
                                        torrent.Url);
                                });
                            }
                            catch (Exception)
                            {
                            }

                            await RetrieveAssets(tmdbClient, movie);

                            context.MovieSet.Add(movie);
                        }
                        else
                        {
                            foreach (var torrent in existingEntity.Torrents)
                            {
                                var updatedTorrent = movie.Torrents.FirstOrDefault(a => a.Quality == torrent.Quality);
                                torrent.Peers = updatedTorrent.Peers;
                                torrent.Seeds = updatedTorrent.Seeds;
                                if (string.IsNullOrEmpty(torrent.Url))
                                {
                                    torrent.Url = updatedTorrent.Url;
                                }
                            }

                            existingEntity.GenreNames = string.Join(", ", movieJson.Genres.Select(FirstCharToUpper));
                        }

                        await context.SaveChangesAsync();

                        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);
        }
コード例 #5
0
        /// <summary>
        /// Import shows 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} shows started at {
                        DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff",
                            CultureInfo.InvariantCulture)
                    }";

            _loggingService.Telemetry.TrackTrace(loggingTraceBegin);

            var updatedShows = 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 show
                        var showJson =
                            BsonSerializer.Deserialize <ShowBson>(document);

                        if (showJson.Year == null)
                        {
                            continue;
                        }

                        var show = new Show
                        {
                            Rating = new Rating
                            {
                                Watching   = showJson.Rating.Watching,
                                Hated      = showJson.Rating.Hated,
                                Percentage = showJson.Rating.Percentage,
                                Votes      = showJson.Rating.Votes,
                                Loved      = showJson.Rating.Loved
                            },
                            Images = new ImageShow
                            {
                                Banner = showJson.Images.Banner,
                                Poster = showJson.Images.Poster
                            },
                            ImdbId  = showJson.ImdbId,
                            Title   = WebUtility.HtmlDecode(showJson.Title),
                            Year    = int.Parse(showJson.Year),
                            Runtime = showJson.Runtime,
                            Genres  = showJson.Genres.Select(genre => new Genre
                            {
                                Name = genre.AsString
                            }).ToList(),
                            GenreNames  = string.Join(", ", showJson.Genres.Select(a => FirstCharToUpper(a.AsString))),
                            Slug        = showJson.Slug,
                            LastUpdated = showJson.LastUpdated,
                            TvdbId      = showJson.TvdbId,
                            NumSeasons  = showJson.NumSeasons,
                            Status      = showJson.Status,
                            Synopsis    = showJson.Synopsis,
                            Country     = showJson.Country,
                            Episodes    = showJson.Episodes.Select(episode => new EpisodeShow
                            {
                                Title     = WebUtility.HtmlDecode(episode.Title),
                                DateBased = episode.DateBased,
                                TvdbId    = episode.TvdbId,
                                Torrents  = new TorrentNode
                                {
                                    Torrent0 = new Torrent
                                    {
                                        Url      = episode.Torrents.Torrent_0?.Url,
                                        Peers    = episode.Torrents.Torrent_0?.Peers,
                                        Seeds    = episode.Torrents.Torrent_0?.Seeds,
                                        Provider = episode.Torrents.Torrent_0?.Provider
                                    },
                                    Torrent1080p = new Torrent
                                    {
                                        Url      = episode.Torrents.Torrent_1080p?.Url,
                                        Peers    = episode.Torrents.Torrent_1080p?.Peers,
                                        Seeds    = episode.Torrents.Torrent_1080p?.Seeds,
                                        Provider = episode.Torrents.Torrent_1080p?.Provider
                                    },
                                    Torrent480p = new Torrent
                                    {
                                        Url      = episode.Torrents.Torrent_480p?.Url,
                                        Peers    = episode.Torrents.Torrent_480p?.Peers,
                                        Seeds    = episode.Torrents.Torrent_480p?.Seeds,
                                        Provider = episode.Torrents.Torrent_480p?.Provider
                                    },
                                    Torrent720p = new Torrent
                                    {
                                        Url      = episode.Torrents.Torrent_720p?.Url,
                                        Peers    = episode.Torrents.Torrent_720p?.Peers,
                                        Seeds    = episode.Torrents.Torrent_720p?.Seeds,
                                        Provider = episode.Torrents.Torrent_720p?.Provider
                                    }
                                },
                                EpisodeNumber = episode.EpisodeNumber,
                                Season        = episode.Season,
                                Overview      = episode.Overview,
                                FirstAired    = episode.FirstAired
                            }).ToList(),
                            AirDay  = showJson.AirDay,
                            AirTime = showJson.AirTime,
                            Network = showJson.Network
                        };

                        if (!context.ShowSet.Any(a => a.ImdbId == show.ImdbId))
                        {
                            await UpdateImagesAndSimilarShow(show).ConfigureAwait(false);

                            context.ShowSet.Add(show);
                        }
                        else
                        {
                            var existingEntity = await context.ShowSet.Include(a => a.Rating)
                                                 .Include(a => a.Episodes)
                                                 .ThenInclude(episode => episode.Torrents)
                                                 .ThenInclude(torrent => torrent.Torrent0)
                                                 .Include(a => a.Episodes)
                                                 .ThenInclude(episode => episode.Torrents)
                                                 .ThenInclude(torrent => torrent.Torrent1080p)
                                                 .Include(a => a.Episodes)
                                                 .ThenInclude(episode => episode.Torrents)
                                                 .ThenInclude(torrent => torrent.Torrent480p)
                                                 .Include(a => a.Episodes)
                                                 .ThenInclude(episode => episode.Torrents)
                                                 .ThenInclude(torrent => torrent.Torrent720p)
                                                 .Include(a => a.Genres)
                                                 .Include(a => a.Images)
                                                 .Include(a => a.Similars).FirstOrDefaultAsync(a => a.ImdbId == show.ImdbId)
                                                 .ConfigureAwait(false);

                            existingEntity.Rating.Hated      = show.Rating.Hated;
                            existingEntity.Rating.Loved      = show.Rating.Loved;
                            existingEntity.Rating.Percentage = show.Rating.Percentage;
                            existingEntity.Rating.Votes      = show.Rating.Votes;
                            existingEntity.Rating.Watching   = show.Rating.Watching;
                            existingEntity.AirDay            = show.AirDay;
                            existingEntity.AirTime           = show.AirTime;
                            existingEntity.Status            = show.Status;
                            existingEntity.NumSeasons        = show.NumSeasons;
                            foreach (var episode in existingEntity.Episodes)
                            {
                                var updatedEpisode = show.Episodes.FirstOrDefault(a => a.TvdbId == episode.TvdbId);
                                if (updatedEpisode == null)
                                {
                                    continue;
                                }
                                if (episode.Torrents?.Torrent0 != null && updatedEpisode.Torrents.Torrent0 != null)
                                {
                                    episode.Torrents.Torrent0.Peers = updatedEpisode.Torrents.Torrent0.Peers;
                                    episode.Torrents.Torrent0.Seeds = updatedEpisode.Torrents.Torrent0.Seeds;
                                }

                                if (episode.Torrents?.Torrent1080p != null &&
                                    updatedEpisode.Torrents.Torrent1080p != null)
                                {
                                    episode.Torrents.Torrent1080p.Peers = updatedEpisode.Torrents.Torrent1080p.Peers;
                                    episode.Torrents.Torrent1080p.Seeds = updatedEpisode.Torrents.Torrent1080p.Seeds;
                                }

                                if (episode.Torrents?.Torrent720p != null &&
                                    updatedEpisode.Torrents.Torrent720p != null)
                                {
                                    episode.Torrents.Torrent720p.Peers = updatedEpisode.Torrents.Torrent720p.Peers;
                                    episode.Torrents.Torrent720p.Seeds = updatedEpisode.Torrents.Torrent720p.Seeds;
                                }

                                if (episode.Torrents?.Torrent480p != null &&
                                    updatedEpisode.Torrents.Torrent480p != null)
                                {
                                    episode.Torrents.Torrent480p.Peers = updatedEpisode.Torrents.Torrent480p.Peers;
                                    episode.Torrents.Torrent480p.Seeds = updatedEpisode.Torrents.Torrent480p.Seeds;
                                }
                            }

                            var newEpisodes =
                                show.Episodes.Except(existingEntity.Episodes, new EpisodeComparer());
                            foreach (var newEpisode in newEpisodes.ToList())
                            {
                                existingEntity.Episodes.Add(newEpisode);
                            }

                            if (existingEntity.Episodes.Any())
                            {
                                var lastEpisode = existingEntity.Episodes.OrderBy(a => a.FirstAired).Last();
                                existingEntity.LastUpdated = lastEpisode.FirstAired;
                            }
                        }

                        await context.SaveChangesAsync().ConfigureAwait(false);

                        watch.Stop();
                        updatedShows++;
                        Console.WriteLine(Environment.NewLine);
                        Console.WriteLine(
                            $"{DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture)} UPDATED SHOW {show.Title} in {watch.ElapsedMilliseconds} ms. {updatedShows}/{documents.Count}");
                    }
                    catch (Exception ex)
                    {
                        _loggingService.Telemetry.TrackException(ex);
                    }
                }
            }

            // Finish
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Done processing shows.");

            var loggingTraceEnd =
                $@"Import shows ended at {
                        DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff",
                            CultureInfo.InvariantCulture)
                    }";

            _loggingService.Telemetry.TrackTrace(loggingTraceEnd);
        }