private NewznabCapabilities ParseCapabilities(HttpResponse response)
        {
            var capabilities = new NewznabCapabilities();

            var xDoc = XDocument.Parse(response.Content);

            if (xDoc == null)
            {
                throw new XmlException("Invalid XML").WithData(response);
            }

            NewznabRssParser.CheckError(xDoc, new IndexerResponse(new IndexerRequest(response.Request), response));

            var xmlRoot = xDoc.Element("caps");

            if (xmlRoot == null)
            {
                throw new XmlException("Unexpected XML").WithData(response);
            }

            var xmlLimits = xmlRoot.Element("limits");

            if (xmlLimits != null)
            {
                capabilities.DefaultPageSize = int.Parse(xmlLimits.Attribute("default").Value);
                capabilities.MaxPageSize     = int.Parse(xmlLimits.Attribute("max").Value);
            }

            var xmlSearching = xmlRoot.Element("searching");

            if (xmlSearching != null)
            {
                var xmlBasicSearch = xmlSearching.Element("search");
                if (xmlBasicSearch == null || xmlBasicSearch.Attribute("available").Value != "yes")
                {
                    capabilities.SupportedSearchParameters = null;
                }
                else if (xmlBasicSearch.Attribute("supportedParams") != null)
                {
                    capabilities.SupportedSearchParameters = xmlBasicSearch.Attribute("supportedParams").Value.Split(',');
                }

                var xmlTvSearch = xmlSearching.Element("tv-search");
                if (xmlTvSearch == null || xmlTvSearch.Attribute("available").Value != "yes")
                {
                    capabilities.SupportedTvSearchParameters = null;
                }
                else if (xmlTvSearch.Attribute("supportedParams") != null)
                {
                    capabilities.SupportedTvSearchParameters = xmlTvSearch.Attribute("supportedParams").Value.Split(',');
                    capabilities.SupportsAggregateIdSearch   = true;
                }
            }

            var xmlCategories = xmlRoot.Element("categories");

            if (xmlCategories != null)
            {
                foreach (var xmlCategory in xmlCategories.Elements("category"))
                {
                    var cat = new NewznabCategory
                    {
                        Id            = int.Parse(xmlCategory.Attribute("id").Value),
                        Name          = xmlCategory.Attribute("name").Value,
                        Description   = xmlCategory.Attribute("description") != null?xmlCategory.Attribute("description").Value : string.Empty,
                        Subcategories = new List <NewznabCategory>()
                    };

                    foreach (var xmlSubcat in xmlCategory.Elements("subcat"))
                    {
                        cat.Subcategories.Add(new NewznabCategory
                        {
                            Id          = int.Parse(xmlSubcat.Attribute("id").Value),
                            Name        = xmlSubcat.Attribute("name").Value,
                            Description = xmlSubcat.Attribute("description") != null ? xmlSubcat.Attribute("description").Value : string.Empty
                        });
                    }

                    capabilities.Categories.Add(cat);
                }
            }

            return(capabilities);
        }
Exemple #2
0
        private IndexerCapabilities ParseCapabilities(HttpResponse response)
        {
            var capabilities = new IndexerCapabilities();

            var xDoc = XDocument.Parse(response.Content);

            if (xDoc == null)
            {
                throw new XmlException("Invalid XML").WithData(response);
            }

            NewznabRssParser.CheckError(xDoc, new IndexerResponse(new IndexerRequest(response.Request), response));

            var xmlRoot = xDoc.Element("caps");

            if (xmlRoot == null)
            {
                throw new XmlException("Unexpected XML").WithData(response);
            }

            var xmlLimits = xmlRoot.Element("limits");

            if (xmlLimits != null)
            {
                capabilities.LimitsDefault = int.Parse(xmlLimits.Attribute("default").Value);
                capabilities.LimitsMax     = int.Parse(xmlLimits.Attribute("max").Value);
            }

            var xmlSearching = xmlRoot.Element("searching");

            if (xmlSearching != null)
            {
                var xmlBasicSearch = xmlSearching.Element("search");
                if (xmlBasicSearch == null || xmlBasicSearch.Attribute("available").Value != "yes")
                {
                    capabilities.SearchParams = new List <SearchParam>();
                }
                else if (xmlBasicSearch.Attribute("supportedParams") != null)
                {
                    foreach (var param in xmlBasicSearch.Attribute("supportedParams").Value.Split(','))
                    {
                        if (Enum.TryParse(param, true, out SearchParam searchParam))
                        {
                            capabilities.SearchParams.AddIfNotNull(searchParam);
                        }
                    }
                }
                else
                {
                    capabilities.SearchParams = new List <SearchParam> {
                        SearchParam.Q
                    };
                }

                var xmlMovieSearch = xmlSearching.Element("movie-search");
                if (xmlMovieSearch == null || xmlMovieSearch.Attribute("available").Value != "yes")
                {
                    capabilities.MovieSearchParams = new List <MovieSearchParam>();
                }
                else if (xmlMovieSearch.Attribute("supportedParams") != null)
                {
                    foreach (var param in xmlMovieSearch.Attribute("supportedParams").Value.Split(','))
                    {
                        if (Enum.TryParse(param, true, out MovieSearchParam searchParam))
                        {
                            capabilities.MovieSearchParams.AddIfNotNull(searchParam);
                        }
                    }
                }
                else
                {
                    capabilities.MovieSearchParams = new List <MovieSearchParam> {
                        MovieSearchParam.Q
                    };
                }

                var xmlTvSearch = xmlSearching.Element("tv-search");
                if (xmlTvSearch == null || xmlTvSearch.Attribute("available").Value != "yes")
                {
                    capabilities.TvSearchParams = new List <TvSearchParam>();
                }
                else if (xmlTvSearch.Attribute("supportedParams") != null)
                {
                    foreach (var param in xmlTvSearch.Attribute("supportedParams").Value.Split(','))
                    {
                        if (Enum.TryParse(param, true, out TvSearchParam searchParam))
                        {
                            capabilities.TvSearchParams.AddIfNotNull(searchParam);
                        }
                    }
                }
                else
                {
                    capabilities.TvSearchParams = new List <TvSearchParam> {
                        TvSearchParam.Q
                    };
                }

                var xmlAudioSearch = xmlSearching.Element("audio-search");
                if (xmlAudioSearch == null || xmlAudioSearch.Attribute("available").Value != "yes")
                {
                    capabilities.MusicSearchParams = new List <MusicSearchParam>();
                }
                else if (xmlAudioSearch.Attribute("supportedParams") != null)
                {
                    foreach (var param in xmlAudioSearch.Attribute("supportedParams").Value.Split(','))
                    {
                        if (Enum.TryParse(param, true, out MusicSearchParam searchParam))
                        {
                            capabilities.MusicSearchParams.AddIfNotNull(searchParam);
                        }
                    }
                }
                else
                {
                    capabilities.MusicSearchParams = new List <MusicSearchParam> {
                        MusicSearchParam.Q
                    };
                }

                var xmlBookSearch = xmlSearching.Element("book-search");
                if (xmlBookSearch == null || xmlBookSearch.Attribute("available").Value != "yes")
                {
                    capabilities.BookSearchParams = new List <BookSearchParam>();
                }
                else if (xmlBookSearch.Attribute("supportedParams") != null)
                {
                    foreach (var param in xmlBookSearch.Attribute("supportedParams").Value.Split(','))
                    {
                        if (Enum.TryParse(param, true, out BookSearchParam searchParam))
                        {
                            capabilities.BookSearchParams.AddIfNotNull(searchParam);
                        }
                    }
                }
                else
                {
                    capabilities.BookSearchParams = new List <BookSearchParam> {
                        BookSearchParam.Q
                    };
                }
            }

            var xmlCategories = xmlRoot.Element("categories");

            if (xmlCategories != null)
            {
                foreach (var xmlCategory in xmlCategories.Elements("category"))
                {
                    var cat = new IndexerCategory
                    {
                        Id          = int.Parse(xmlCategory.Attribute("id").Value),
                        Name        = xmlCategory.Attribute("name").Value,
                        Description = xmlCategory.Attribute("description") != null?xmlCategory.Attribute("description").Value : string.Empty
                    };

                    foreach (var xmlSubcat in xmlCategory.Elements("subcat"))
                    {
                        var subCat = new IndexerCategory
                        {
                            Id          = int.Parse(xmlSubcat.Attribute("id").Value),
                            Name        = xmlSubcat.Attribute("name").Value,
                            Description = xmlSubcat.Attribute("description") != null?xmlSubcat.Attribute("description").Value : string.Empty
                        };

                        cat.SubCategories.Add(subCat);
                        capabilities.Categories.AddCategoryMapping(subCat.Name, subCat);
                    }

                    capabilities.Categories.AddCategoryMapping(cat.Name, cat);
                }
            }

            return(capabilities);
        }