Ejemplo n.º 1
0
        public async Task SyncMovieDetailsAsync(IStudioClient studioClient)
        {
            var stopwatch = Stopwatch.StartNew();

            Console.Write($"'{studioClient.StudioName}': retrieving studio details. ");
            var studio = await _studioRepository.FindAsync(studioClient.StudioName);

            Console.WriteLine($"Studio Id: {studio.StudioId}");

            Console.WriteLine("Retrieving movies...");
            var movies = (await _movieRepository.FindMoviesWithoutDetailsAsync(studio.StudioId)).ToList();

            Console.WriteLine($"Retrieved {movies.Count} movies");
            Console.WriteLine("Getting movie details\n");

            var buffer = new ConcurrentBag <Movie>();

            total += movies.Count;

            var errorsCount = 0;
            await _concurrentActionHandler.ForeachAsync(movies, async movie =>
            {
                try
                {
                    Interlocked.Increment(ref counter);

                    var movieDetails = await GetMovieAsync(studioClient, movie);

                    if (movieDetails != null)
                    {
                        buffer.Add(movieDetails);

                        if (errorsCount > 0)
                        {
                            Interlocked.Decrement(ref errorsCount);
                        }
                    }
                }
                catch (Exception e)
                {
                    Interlocked.Increment(ref errorsCount);

                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("SCRAPPING ERROR:");
                    Console.WriteLine(e.Message);
                    Console.ForegroundColor = ConsoleColor.White;

                    if (errorsCount == 5)
                    {
                        throw;
                    }
                }
                finally
                {
                    stopwatch.Stop();
                }
            }, 4, () => IterationCompleted(buffer));
        }
Ejemplo n.º 2
0
        public async Task <Studio> GetStudioByIdAsync(int studioId)
        {
            var studio = await _studios.FindAsync(studioId);

            if (studio == null)
            {
                throw new ResourceNotFoundException($"Studio {studioId} not found");
            }

            return(_mapper.Map <Studio>(studio));
        }
Ejemplo n.º 3
0
        private async Task <Domain.Entities.Studio> GetStudioAsync(IStudioClient studioClient)
        {
            var studio = await _studioRepository.FindAsync(studioClient.StudioName);

            if (studio == null)
            {
                studio = new Domain.Entities.Studio
                {
                    Name = studioClient.StudioName
                };

                studio = await _studioRepository.AddAsync(studio);
            }

            return(studio);
        }