Beispiel #1
0
        public CardigannBase(IConfigService configService,
                             CardigannDefinition definition,
                             Logger logger)
        {
            _configService = configService;
            _definition    = definition;
            _encoding      = Encoding.GetEncoding(definition.Encoding);
            _logger        = logger;

            SiteLink = definition.Links.First();

            if (_definition.Caps.Categories != null)
            {
                foreach (var category in _definition.Caps.Categories)
                {
                    var cat = NewznabStandardCategory.GetCatByName(category.Value);
                    if (cat == null)
                    {
                        _logger.Error(string.Format("CardigannIndexer ({0}): invalid Torznab category for id {1}: {2}", _definition.Id, category.Key, category.Value));
                        continue;
                    }

                    AddCategoryMapping(category.Key, cat);
                }
            }

            if (_definition.Caps.Categorymappings != null)
            {
                foreach (var categorymapping in _definition.Caps.Categorymappings)
                {
                    IndexerCategory torznabCat = null;

                    if (categorymapping.cat != null)
                    {
                        torznabCat = NewznabStandardCategory.GetCatByName(categorymapping.cat);
                        if (torznabCat == null)
                        {
                            _logger.Error(string.Format("CardigannIndexer ({0}): invalid Torznab category for id {1}: {2}", _definition.Id, categorymapping.id, categorymapping.cat));
                            continue;
                        }
                    }

                    AddCategoryMapping(categorymapping.id, torznabCat, categorymapping.desc);

                    if (categorymapping.Default)
                    {
                        _defaultCategories.Add(categorymapping.id);
                    }
                }
            }
        }
Beispiel #2
0
        public void AddCategoryMapping(string trackerCategory, IndexerCategory torznabCategory, string trackerCategoryDesc = null)
        {
            _categoryMapping.Add(new CategoryMapping(trackerCategory, trackerCategoryDesc, torznabCategory.Id));

            if (trackerCategoryDesc == null)
            {
                return;
            }

            // create custom cats (1:1 categories) if trackerCategoryDesc is defined
            // - if trackerCategory is "integer" we use that number to generate custom category id
            // - if trackerCategory is "string" we compute a hash to generate fixed integer id for the custom category
            //   the hash is not perfect but it should work in most cases. we can't use sequential numbers because
            //   categories are updated frequently and the id must be fixed to work in 3rd party apps
            if (!int.TryParse(trackerCategory, out var trackerCategoryInt))
            {
                var hashed = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(trackerCategory));
                trackerCategoryInt = BitConverter.ToUInt16(hashed, 0); // id between 0 and 65535 < 100000
            }

            var customCat = new IndexerCategory(trackerCategoryInt + 100000, trackerCategoryDesc);

            _categoryMapping.Add(new CategoryMapping(trackerCategory, trackerCategoryDesc, customCat.Id));
        }
Beispiel #3
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);
        }