Ejemplo n.º 1
0
        public IEnumerable <int?> SearchMovieIds(MovieSearchParams search)
        {
            if (string.IsNullOrEmpty(search.Search))
            {
                return(Enumerable.Empty <int?>());
            }

            using (var sphinx = new DbSphinx())
            {
                var qb = new SphinxQueryBuilder("torronto_movies")
                         .SelectColumns("id")
                         .AddMatch(string.Format("@!(description) {0}", EscapeUserInput(search.Search)))
                         .AddLimits(0, 100);

                if (search.MovieStatus > MovieStatus.Unknown)
                {
                    qb.AddWhere("status", (int)search.MovieStatus);
                }

                var items = sphinx.Query <int?>(qb.Build())
                            .ToList();

                return(items);
            }
        }
Ejemplo n.º 2
0
        public async Task ShouldFindMovieByImdbId()
        {
            // Arrange
            var imdbId       = "tt0068646";
            var searchParams = new MovieSearchParams {
                ImdbId = imdbId
            };
            var sut = CreateDownloader();

            // Act
            var actual = await sut.FindByExternalId(searchParams, null);

            // Assert
            Assert.That(actual, Is.Not.Null);
            Assert.That(actual, Is.Not.Empty);

            Console.WriteLine(actual);
            FindResults response =
                JsonConvert.DeserializeObject <FindResults>(actual);

            Assert.That(response, Is.Not.Null);
            Assert.That(response.MovieResults, Is.Not.Null | Is.Not.Empty);
            Assert.That(response.MovieResults[0], Is.Not.Null);
            Assert.That(response.MovieResults[0].Id, Is.EqualTo((int)KnownMovieIds.TheGodfather));
        }
Ejemplo n.º 3
0
        public async Task ShouldProduceSearchResults()
        {
            // Arrange
            var searchParams = new MovieSearchParams {
                Name = "The Godfather"
            };
            var sut = CreateDownloader();

            // Act
            var actual = await sut.GetMovieSearchResultsAsync(searchParams, 1, null);

            // Assert
            Assert.That(actual, Is.Not.Null);
            Assert.That(actual, Is.Not.Empty);

            SearchResults <Models.Movie> response =
                JsonConvert.DeserializeObject <SearchResults <Models.Movie> >(actual);

            Assert.That(response, Is.Not.Null);
            Assert.That(response.Results, Is.Not.Null | Is.Not.Empty);
            Assert.That(response.Results[0], Is.Not.Null);
            Assert.That(response.Results[0].Title, Is.Not.Null);
            Assert.That(response.Results[0].Title, Is.Not.Empty);
            Assert.That(response.Results[0].Title, Is.EqualTo("The Godfather"));
            Assert.That(response.Results[0].Id, Is.EqualTo((int)KnownMovieIds.TheGodfather));
        }
        public virtual async Task <Movie[]> SearchAsync(MovieSearchParams movieSearchParams,
                                                        IWebProxy proxy)
        {
            // TODO: IMDB Id
            string json = await _apiDownloader.GetMovieSearchResultsAsync(movieSearchParams, proxy);

            var searchResults = Deserialize <SearchResults <Models.Movie> >(json);

            Models.Movie[] data = searchResults.Results?.Take(MaxSearchResults).ToArray();
            // TODO: Paging

            List <Movie> movieResults = data?
                                        .Select(x => Adapt(x))
                                        .ToList();

            return(movieResults?.ToArray() ?? new Movie[0]);
        }
        public async Task <string> GetMovieSearchResultsAsync(MovieSearchParams searchParams, int page, IWebProxy proxy)
        {
            string baseUrl = _configurationValues.ApiBaseUrl;
            string apiKey  = _configurationValues.ApiKey;

            string url = baseUrl + Endpoints.MovieSearch;
            Dictionary <string, string> queryParams = new Dictionary <string, string>
            {
                { "query", searchParams.Name },
                { "page", page.ToString() },
                { "include_adult", searchParams.IncludeAdult.ToString().ToLowerInvariant() },
                { "api_key", apiKey },
            };

            if (searchParams.Year.HasValue)
            {
                queryParams.Add("year", searchParams.Year.Value.ToString());
            }

            string queryString = "?";

            foreach (var param in queryParams)
            {
                if (queryString.Length > 1)
                {
                    queryString += "&";
                }
                queryString += $"{WebUtility.UrlEncode(param.Key)}={WebUtility.UrlEncode(param.Value)}";
            }
            url += queryString;
            string json = await _httpDownloader.GetStringAsync(url,
                                                               new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "Content-Type", "application/json" }
            },
                                                               proxy);

            return(json);
        }
        //public async Task<string> GetAuthTokenAsync(IWebProxy proxy)
        //{
        //    string baseUrl = _configurationValues.ApiBaseUrl;
        //    string apiKey = _configurationValues.ApiKey;

        //    string url = baseUrl + Endpoints.NewAuthenticationToken;
        //    Dictionary<string, string> queryParams = new Dictionary<string, string>
        //    {
        //        {"api_key", apiKey},
        //    };

        //    string queryString = "?";
        //    foreach (var param in queryParams)
        //    {
        //        if (queryString.Length > 1) queryString += "&";
        //        queryString += $"{WebUtility.UrlEncode(param.Key)}={WebUtility.UrlEncode(param.Value)}";
        //    }
        //    url += queryString;
        //    string json = await _httpDownloader.GetStringAsync(url,
        //        new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
        //        {
        //            {"Content-Type", "application/json"}
        //        },
        //        proxy);
        //    return json;
        //}

        public async Task <string> FindByExternalId(MovieSearchParams searchParams, IWebProxy proxy)
        {
            string baseUrl = _configurationValues.ApiBaseUrl;
            string apiKey  = _configurationValues.ApiKey;

            string url = baseUrl;
            Dictionary <string, string> queryParams = new Dictionary <string, string>
            {
                { "api_key", apiKey },
            };

            if (!string.IsNullOrWhiteSpace(searchParams.ImdbId))
            {
                url = baseUrl + string.Format(Endpoints.FindByExternalId, searchParams.ImdbId);
                queryParams.Add("external_source", "imdb_id");
            }

            string queryString = "?";

            foreach (var param in queryParams)
            {
                if (queryString.Length > 1)
                {
                    queryString += "&";
                }
                queryString += $"{WebUtility.UrlEncode(param.Key)}={WebUtility.UrlEncode(param.Value)}";
            }
            url += queryString;
            string json = await _httpDownloader.GetStringAsync(url,
                                                               new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "Content-Type", "application/json" }
            },
                                                               proxy);

            return(json);
        }
Ejemplo n.º 7
0
        public MoviePagination GetMovies(int?userId, MovieSearchParams search, PaginationParams pageParams)
        {
            using (var db = new DbTorronto())
            {
                var filter = db.Movie
                             .NoCopyrighted()
                             .SelectMany(m => db.MovieUser.Where(x => x.MovieID == m.ID && x.UserID == userId).DefaultIfEmpty(),
                                         (movie, movieUser) => new
                {
                    M             = movie,
                    MuWaitList    = movieUser.IsWaitlist,
                    MuUserWatched = movieUser.IsWatched,
                    MuDontWant    = movieUser.IsDontWant,
                    WlDate        = movieUser.Created,
                    MuMark        = movieUser.Mark,
                });

                if (!string.IsNullOrEmpty(search.Search))
                {
                    if (_isSphinxEnabled)
                    {
                        var movieIds = _searchService.SearchMovieIds(search);
                        filter = filter.Where(x => movieIds.Contains(x.M.ID));
                    }
                    else
                    {
                        filter = filter.Where(x => x.M.Title.Contains(search.Search) ||
                                              x.M.OriginalTitle.Contains(search.Search));
                    }
                }

                if (search.WaitList)
                {
                    filter = filter.Where(x => x.MuWaitList);
                }

                if (search.KinopoiskID > 0)
                {
                    filter = filter.Where(x => x.M.KinopoiskID == search.KinopoiskID);
                }

                if (search.SystemList)
                {
                    var candidates = from mu in db.MovieUser
                                     join mr in db.MovieRecommendation on mu.MovieID equals mr.MovieID
                                     from omu in db.MovieUser.Where(mm => mm.MovieID == mr.OtherMovieID && mm.UserID == userId).DefaultIfEmpty()
                                     where mu.UserID == userId && mu.Mark == 10 &&
                                     Sql2.IsNullOrFalse(omu.IsDontWant) &&
                                     Sql2.IsNullOrFalse(omu.IsWaitlist) &&
                                     Sql2.IsNullOrFalse(omu.IsWatched)
                                     orderby mr.Position
                                     select(int?) mr.OtherMovieID;

                    var rmovies = candidates
                                  .Take(50)
                                  .ToArray();

                    if (rmovies.Length > 0)
                    {
                        filter = filter.Where(x => rmovies.Contains(x.M.ID));
                    }
                    else
                    {
                        filter = filter.Where(x => (x.M.Status == MovieStatus.ComingSoon || x.M.Status == MovieStatus.RecentPremiere) &&
                                              Sql2.IsNullOrFalse(x.MuUserWatched) &&
                                              Sql2.IsNullOrFalse(x.MuWaitList) &&
                                              Sql2.IsNullOrFalse(x.MuDontWant)
                                              );
                    }
                }

                if (search.MovieStatus != MovieStatus.Unknown)
                {
                    filter = filter.Where(x => x.M.Status == search.MovieStatus);
                }

                List <Person> actors = null;

                if (!string.IsNullOrEmpty(search.Actors))
                {
                    var actorIds = search.Actors
                                   .Split(',')
                                   .Select(x => Convert.ToInt32(x))
                                   .Cast <int?>()
                                   .ToArray();

                    actors = db.Person
                             .Where(p => actorIds.Contains(p.ID))
                             .ToList();

                    var movieIds = db.MoviePerson
                                   .Where(mp => actorIds.Contains(mp.PersonID))
                                   .Select(mp => mp.MovieID)
                                   .Cast <int?>()
                                   .ToArray();

                    filter = filter.Where(x => movieIds.Contains(x.M.ID));
                }

                switch (pageParams.Order)
                {
                case "wldate":
                    filter = filter.OrderByDescending(x => x.WlDate);
                    break;

                case "rkp":
                    filter = filter.OrderByDescending(f => f.M.RatingKinopoisk);
                    break;

                case "rimdb":
                    filter = filter.OrderByDescending(f => f.M.RatingImdb);
                    break;

                case "ruser":
                    filter = filter
                             .Where(x => x.MuMark != null)
                             .OrderByDescending(f => f.MuMark)
                             .ThenByDescending(f => f.M.RatingKinopoisk);
                    break;

                case "quality":
                    filter = filter
                             .OrderByDescending(f => f.M.BestVideoQuality)
                             .ThenByDescending(f => f.M.RatingKinopoisk);
                    break;

                case "added":
                    filter = filter
                             .OrderByDescending(f => f.M.Created)
                             .ThenByDescending(f => f.M.ID);
                    break;

                case "topweek":
                    // ReSharper disable ConditionIsAlwaysTrueOrFalse
                    filter = filter
                             .Where(x => x.M.MovieTopWeek.TorrentCount != null)
                             .Where(x => Sql2.IsNullOrFalse(x.MuUserWatched) &&
                                    Sql2.IsNullOrFalse(x.MuWaitList) &&
                                    Sql2.IsNullOrFalse(x.MuDontWant))
                             .OrderByDescending(f => f.M.MovieTopWeek.TorrentCount);
                    // ReSharper restore ConditionIsAlwaysTrueOrFalse
                    break;

                default:
                    filter = filter.OrderBy(t => t.M.Title);
                    break;
                }

                var count  = pageParams.NoCount ? 0 : filter.Count();
                var movies = filter
                             .Skip(pageParams.SkipCount)
                             .Take(pageParams.PageSize)
                             .Select(t => new MovieItem
                {
                    Self = new Movie
                    {
                        ID               = t.M.ID,
                        Title            = t.M.Title,
                        OriginalTitle    = t.M.OriginalTitle,
                        ImdbID           = t.M.ImdbID,
                        KinopoiskID      = t.M.KinopoiskID,
                        RatingImdb       = t.M.RatingImdb,
                        RatingKinopoisk  = t.M.RatingKinopoisk,
                        ReleaseDate      = t.M.ReleaseDate,
                        Status           = t.M.Status,
                        BestVideoQuality = t.M.BestVideoQuality
                    },
                    InWaitList = t.MuWaitList,
                    Mark       = t.MuMark,
                    IsWatched  = t.MuUserWatched,
                    IsDontWant = t.MuDontWant
                })
                             .ToList();

                return(new MoviePagination(movies)
                {
                    PageSize = pageParams.PageSize,
                    TotalItems = count,
                    Actors = actors
                });
            }
        }
 public async Task <string> GetMovieSearchResultsAsync(MovieSearchParams searchParams, IWebProxy proxy)
 {
     return(await GetMovieSearchResultsAsync(searchParams, 1, proxy));
 }