예제 #1
0
 /// <summary>
 /// Get movies as a list from wrapped movies
 /// </summary>
 /// <param name="wrapper">Wrapped movies</param>
 /// <returns>List of movies</returns>
 private static IEnumerable<MovieShort> GetMoviesListFromWrapper(WrapperMovieShort wrapper)
     => wrapper?.Data?.Movies?.Select(movie => new MovieShort
     {
         ApiVersion = movie.ApiVersion,
         DateUploaded = movie.DateUploaded,
         DateUploadedUnix = movie.DateUploadedUnix,
         ExecutionTime = movie.ExecutionTime,
         Genres = movie.Genres,
         Id = movie.Id,
         ImdbCode = movie.ImdbCode,
         IsFavorite = false,
         HasBeenSeen = false,
         Language = movie.Language,
         MediumCoverImage = movie.MediumCoverImage,
         CoverImagePath = string.Empty,
         MpaRating = movie.MpaRating,
         RatingValue = movie.Rating,
         Runtime = movie.Runtime,
         ServerTime = movie.ServerTime,
         ServerTimezone = movie.ServerTimezone,
         SmallCoverImage = movie.SmallCoverImage,
         State = movie.State,
         Title = movie.Title,
         TitleLong = movie.TitleLong,
         Torrents = movie.Torrents,
         Url = movie.Url,
         Year = movie.Year
     }).ToList();
예제 #2
0
        /// <summary>
        /// Search movies by criteria
        /// </summary>
        /// <param name="criteria">Criteria used for search</param>
        /// <param name="page">Page to return</param>
        /// <param name="limit">The maximum number of movies to return</param>
        /// <param name="genre">The genre to filter</param>
        /// <param name="ratingFilter">Used to filter by rating</param>
        /// <param name="ct">Cancellation token</param>
        /// <returns>Searched movies and the number of movies found</returns>
        public async Task<Tuple<IEnumerable<MovieShort>, int>> SearchMoviesAsync(string criteria,
            int page,
            int limit,
            MovieGenre genre,
            double ratingFilter,
            CancellationToken ct)
        {
            var watch = Stopwatch.StartNew();

            var wrapper = new WrapperMovieShort();

            if (limit < 1 || limit > 50)
                limit = 20;

            if (page < 1)
                page = 1;

            var restClient = new RestClient(Constants.YtsApiEndpoint);
            var request = new RestRequest("/{segment}", Method.GET);
            request.AddUrlSegment("segment", "list_movies.json");
            request.AddParameter("limit", limit);
            request.AddParameter("page", page);
            if (genre != null) request.AddParameter("genre", genre.EnglishName);
            request.AddParameter("minimum_rating", ratingFilter);
            request.AddParameter("query_term", criteria);

            try
            {
                var response = await restClient.ExecuteGetTaskAsync<WrapperMovieShort>(request, ct);
                if (response.ErrorException != null)
                    throw response.ErrorException;

                wrapper = response.Data;
            }
            catch (Exception exception) when (exception is TaskCanceledException)
            {
                Logger.Debug(
                    "SearchMoviesAsync cancelled.");
            }
            catch (Exception exception)
            {
                Logger.Error(
                    $"SearchMoviesAsync: {exception.Message}");
                throw;
            }
            finally
            {
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Logger.Debug(
                    $"SearchMoviesAsync ({criteria}, {page}, {limit}) in {elapsedMs} milliseconds.");
            }

            var movies = GetMoviesListFromWrapper(wrapper) ?? new List<MovieShort>();
            var nbMovies = wrapper?.Data?.MovieCount ?? 0;

            return new Tuple<IEnumerable<MovieShort>, int>(movies, nbMovies);
        }