public void AddCategoryMapping(string trackerCategory, IndexerCategory torznabCategory, string trackerCategoryDesc = null) { _categoryMapping.Add(new CategoryMapping(trackerCategory, trackerCategoryDesc, torznabCategory.Id)); AddTorznabCategoryTree(torznabCategory); 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)); AddTorznabCategoryTree(customCat); }
public List <IndexerCategory> GetTorznabCategoryTree(bool sorted = false) { if (!sorted) { return(_torznabCategoryTree); } // we build a new tree, original is unsorted // first torznab categories ordered by id and then custom cats ordered by name var sortedTree = _torznabCategoryTree .Select(c => { var sortedSubCats = c.SubCategories.OrderBy(x => x.Id); var newCat = new IndexerCategory(c.Id, c.Name); newCat.SubCategories.AddRange(sortedSubCats); return(newCat); }).OrderBy(x => x.Id >= 100000 ? "zzz" + x.Name : x.Id.ToString()).ToList(); return(sortedTree); }
private void MapCardigannCategories(IndexerDefinition def, CardigannDefinition defFile) { if (defFile.Caps.Categories != null) { foreach (var category in defFile.Caps.Categories) { var cat = NewznabStandardCategory.GetCatByName(category.Value); if (cat == null) { continue; } def.Capabilities.Categories.AddCategoryMapping(category.Key, cat); } } if (defFile.Caps.Categorymappings != null) { foreach (var categorymapping in defFile.Caps.Categorymappings) { IndexerCategory torznabCat = null; if (categorymapping.cat != null) { torznabCat = NewznabStandardCategory.GetCatByName(categorymapping.cat); if (torznabCat == null) { continue; } } def.Capabilities.Categories.AddCategoryMapping(categorymapping.id, torznabCat, categorymapping.desc); //if (categorymapping.Default) //{ // DefaultCategories.Add(categorymapping.id); //} } } }
public List <int> ExpandTorznabQueryCategories(int[] queryCategories, bool mapChildrenCatsToParent = false) { var expandedQueryCats = new List <int>(); if (queryCategories == null) { return(expandedQueryCats); } foreach (var queryCategory in queryCategories) { expandedQueryCats.Add(queryCategory); if (queryCategory >= 100000) { continue; } var parentCat = _torznabCategoryTree.FirstOrDefault(c => c.Id == queryCategory); if (parentCat != null) { // if it's parent cat we add all the children expandedQueryCats.AddRange(parentCat.SubCategories.Select(c => c.Id)); } else if (mapChildrenCatsToParent) { // if it's child cat and mapChildrenCatsToParent is enabled we add the parent var queryCategoryTorznab = new IndexerCategory(queryCategory, ""); parentCat = _torznabCategoryTree.FirstOrDefault(c => c.Contains(queryCategoryTorznab)); if (parentCat != null) { expandedQueryCats.Add(parentCat.Id); } } } return(expandedQueryCats.Distinct().ToList()); }
public void AddCategoryMapping(int trackerCategory, IndexerCategory newznabCategory, string trackerCategoryDesc = null) => AddCategoryMapping(trackerCategory.ToString(), newznabCategory, trackerCategoryDesc);
private void AddTorznabCategoryTree(IndexerCategory torznabCategory) { // build the category tree if (NewznabStandardCategory.ParentCats.Contains(torznabCategory)) { // parent cat if (!_torznabCategoryTree.Contains(torznabCategory)) { _torznabCategoryTree.Add(torznabCategory.CopyWithoutSubCategories()); } } else { // child or custom cat var parentCat = NewznabStandardCategory.ParentCats.FirstOrDefault(c => c.Contains(torznabCategory)); if (parentCat != null) { // child cat var nodeCat = _torznabCategoryTree.FirstOrDefault(c => c.Equals(parentCat)); if (nodeCat != null) { // parent cat already exists if (!nodeCat.Contains(torznabCategory)) { nodeCat.SubCategories.Add(torznabCategory); } } else { // create parent cat and add child nodeCat = parentCat.CopyWithoutSubCategories(); nodeCat.SubCategories.Add(torznabCategory); _torznabCategoryTree.Add(nodeCat); } } else { // custom cat if (torznabCategory.Id > 1000 && torznabCategory.Id < 10000) { var potentialParent = NewznabStandardCategory.ParentCats.FirstOrDefault(c => (c.Id / 1000) == (torznabCategory.Id / 1000)); if (potentialParent != null) { var nodeCat = _torznabCategoryTree.FirstOrDefault(c => c.Equals(potentialParent)); if (nodeCat != null) { // parent cat already exists if (!nodeCat.Contains(torznabCategory)) { nodeCat.SubCategories.Add(torznabCategory); } } else { // create parent cat and add child nodeCat = potentialParent.CopyWithoutSubCategories(); nodeCat.SubCategories.Add(torznabCategory); _torznabCategoryTree.Add(nodeCat); } } else { _torznabCategoryTree.Add(torznabCategory); } } else { _torznabCategoryTree.Add(torznabCategory); } } } }