Beispiel #1
0
        public IEnumerable <int?> SearchTorrentIds(TorrentSearchParams search)
        {
            if (string.IsNullOrEmpty(search.Search))
            {
                return(Enumerable.Empty <int?>());
            }

            using (var sphinx = new DbSphinx())
            {
                var qb = new SphinxQueryBuilder("torronto_torrents")
                         .SelectColumns("id")
                         .SelectLiteral(string.Format("((video_quality & {0}) > 0) vq", (int)search.VideoQuality))
                         .SelectLiteral(string.Format("((sound_quality & {0}) > 0) aq", (int)search.AudioQuality))
                         .SelectLiteral(string.Format("((translation & {0}) > 0) tq", (int)search.TranslationQuality))
                         .AddMatch(EscapeUserInput(search.Search))
                         .AddLimits(0, 100);

                if (search.VideoQuality > VideoQuality.Unknown)
                {
                    qb.AddWhere("vq", 1);
                }
                if (search.AudioQuality > AudioQuality.Unknown)
                {
                    qb.AddWhere("aq", 1);
                }
                if (search.TranslationQuality > Translation.Unknown)
                {
                    qb.AddWhere("tq", 1);
                }

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

                return(items);
            }
        }
        public Pagination <TorrentItem> GetTorrents(int?userId, TorrentSearchParams search, PaginationParams pageParams)
        {
            using (var db = new DbTorronto())
            {
                var filter = from t in db.Torrent
                             from muWaitList in db.MovieUser.Where(mu => mu.MovieID == t.MovieID && mu.UserID == userId).DefaultIfEmpty()
                             from subscription in db.TorrentUser.Where(tu => tu.TorrentID == t.ID && tu.UserID == userId).DefaultIfEmpty()
                             from movie in db.Movie.Where(mv => mv.ID == t.MovieID).DefaultIfEmpty()
                             select new TorrentJoin
                {
                    Torrent       = t,
                    MuWaitlist    = muWaitList.IsWaitlist,
                    IsSubscribed  = subscription.IsSubscribed,
                    AddedRss      = subscription.AddedRss,
                    IsCopyrighted = movie.IsCopyrighted
                };

                if (search.MovieID > 0)
                {
                    filter = filter.Where(x => x.Torrent.MovieID == search.MovieID);
                }

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

                if (search.Subscription)
                {
                    filter = filter.Where(x => x.IsSubscribed || x.AddedRss != null);
                }

                if (!string.IsNullOrEmpty(search.Search))
                {
                    if (_isSphinxEnabled)
                    {
                        var torrentIds = _searchService.SearchTorrentIds(search);
                        filter = filter.Where(x => torrentIds.Contains(x.Torrent.ID));
                    }
                    else
                    {
                        filter = filter.Where(x => x.Torrent.Title.Contains(search.Search));
                    }
                }

                if (search.VideoQuality != VideoQuality.Unknown)
                {
                    var predicate = PredicateBuilder.False <TorrentJoin>();

                    predicate = Enum.GetValues(typeof(VideoQuality))
                                .Cast <VideoQuality>()
                                .Where(vq => vq != VideoQuality.Unknown)
                                .Where(vq => search.VideoQuality.HasFlag(vq))
                                .Aggregate(predicate, (current, vq) => current.Or(x => x.Torrent.VideoQuality == vq));

                    filter = filter.Where(predicate);
                }

                if (search.AudioQuality != AudioQuality.Unknown)
                {
                    var predicate = PredicateBuilder.False <TorrentJoin>();

                    predicate = Enum.GetValues(typeof(AudioQuality))
                                .Cast <AudioQuality>()
                                .Where(aq => aq != AudioQuality.Unknown)
                                .Where(aq => search.AudioQuality.HasFlag(aq))
                                .Aggregate(predicate, (current, aq) => current.Or(x => x.Torrent.AudioQuality == aq));

                    filter = filter.Where(predicate);
                }

                if (search.TranslationQuality != Translation.Unknown)
                {
                    var predicate = PredicateBuilder.False <TorrentJoin>();

                    predicate = Enum.GetValues(typeof(Translation))
                                .Cast <Translation>()
                                .Where(translation => translation != Translation.Unknown)
                                .Where(translation => search.TranslationQuality.HasFlag(translation))
                                .Aggregate(predicate, (current, translation) => current.Or(x => x.Torrent.Translation == translation));

                    filter = filter.Where(predicate);
                }

                if (search.TorrentCategory != TorrentCategory.Unknown)
                {
                    var predicate = PredicateBuilder.False <TorrentJoin>();

                    predicate = Enum.GetValues(typeof(TorrentCategory))
                                .Cast <TorrentCategory>()
                                .Where(category => category != TorrentCategory.Unknown)
                                .Where(category => search.TorrentCategory.HasFlag(category))
                                .Aggregate(predicate, (current, category) => current.Or(x => x.Torrent.Category == category));

                    filter = filter.Where(predicate);
                }

                if (!string.IsNullOrEmpty(search.Sizes))
                {
                    var index = 0;
                    var nums  = search.Sizes
                                .Split(',')
                                .Select(x => Convert.ToInt32(x))
                                .ToArray();

                    var predicate = PredicateBuilder.False <TorrentJoin>();

                    foreach (var tuple in TorrentSizes)
                    {
                        if (nums.Contains(index))
                        {
                            predicate = predicate.Or(x => x.Torrent.Size >= tuple.Item1 && x.Torrent.Size <= tuple.Item2);
                        }
                        index++;
                    }

                    filter = filter.Where(predicate);
                }

                switch (search.Order)
                {
                case "size":
                    filter = filter
                             .OrderByDescending(x => x.Torrent.VideoQuality)
                             .ThenByDescending(x => x.Torrent.Size);
                    break;

                default:
                    filter = filter
                             .OrderByDescending(x => x.Torrent.Created)
                             .ThenByDescending(x => x.Torrent.ID);
                    break;
                }

                var count = pageParams.NoCount ? 0 : filter.Count();
                var items = filter
                            .Skip(pageParams.SkipCount)
                            .Take(pageParams.PageSize)
                            .Select(x => new TorrentItem
                {
                    Self          = x.Torrent,
                    InWaitList    = x.MuWaitlist,
                    IsSubscribed  = x.IsSubscribed,
                    IsRss         = x.AddedRss != null,
                    IsCopyrighted = x.IsCopyrighted
                })
                            .ToList();

                items.RemoveAll(t => t.IsCopyrighted);

                return(new Pagination <TorrentItem>(items)
                {
                    TotalItems = count,
                    PageSize = pageParams.PageSize
                });
            }
        }