示例#1
0
        public virtual async Task <IEnumerable <ReleaseInfo> > PerformQuery(TorznabQuery query)
        {
            IEnumerable <Task <IEnumerable <ReleaseInfo> > > tasks = Indexers.Where(i => i.CanHandleQuery(query)).Select(i => i.PerformQuery(query)).ToList(); // explicit conversion to List to execute LINQ query

            bool needFallback = query.IsImdbQuery;
            IEnumerable <string> fallbackTitles = null;

            if (needFallback)
            {
                var imdb = new ImdbResolver(webclient);
                fallbackTitles = await imdb.GetAllTitles(query.ImdbID);

                var fallbackQueries = fallbackTitles.Select(t => query.CreateFallback(t));
                var backupTasks     = fallbackQueries.SelectMany(q => Indexers.Where(i => !i.CanHandleQuery(query) && i.CanHandleQuery(q)).Select(i => i.PerformQuery(q.Clone())));
                tasks = tasks.Concat(backupTasks.ToList()); // explicit conversion to List to execute LINQ query
            }

            var aggregateTask = Task.WhenAll <IEnumerable <ReleaseInfo> >(tasks);

            try {
                await aggregateTask;
            } catch {
                logger.Error(aggregateTask.Exception, "Error during request in metaindexer " + ID);
            }

            var unorderedResult = tasks.Where(x => x.Status == TaskStatus.RanToCompletion).SelectMany(x => x.Result);
            var orderedResult   = unorderedResult.Where(r => {
                var normalizedTitles = fallbackTitles.Concat(fallbackTitles.Select(t => t.Replace(' ', '.').Replace(":", ""))).Select(t => t.ToLowerInvariant());
                foreach (var title in normalizedTitles)
                {
                    if (r.Title.ToLowerInvariant().Contains(title))
                    {
                        return(true);
                    }
                }
                return(false);
            }).OrderByDescending(r => r.Gain);

            var filteredResult = orderedResult.Where(r => {
                if (r.Imdb != null)
                {
                    try {
                        return(Int64.Parse(query.ImdbID.Select(c => char.IsDigit(c)).ToString()) == r.Imdb);
                    } catch {
                        // Cannot safely determine whether result is what we
                        // wanted, so let's just leave it alone...
                    }
                }
                return(true);
            });
            // Limiting the response size might be interesting for use-cases where there are
            // tons of trackers configured in Jackett. For now just use the limit param if
            // someone wants to do that.
            IEnumerable <ReleaseInfo> result = filteredResult;

            if (query.Limit > 0)
            {
                result = result.Take(query.Limit);
            }
            return(result);
        }