Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/utils/categories/categories.go
akuma06 e54a2e59e2 Fixed alphabetical order for languages and categories
Also improved the name of exported functions for categories
2017-07-06 21:53:13 +02:00

90 lignes
2,1 Kio
Go

package categories
import (
"sort"
"github.com/NyaaPantsu/nyaa/config"
)
// Category is a struct defining a category
type Category struct {
ID string
Name string
}
// Cateogories is a struct defining an array of categories
type Categories []Category
var categories Categories
var Index map[string]int
func init() {
if len(categories) == 0 {
var cats map[string]string
if config.IsSukebei() {
cats = config.Conf.Torrents.SukebeiCategories
} else {
cats = config.Conf.Torrents.CleanCategories
}
// Sorting categories alphabetically
var index []string
ids := make(map[string]string)
Index = make(map[string]int, len(cats))
for id, name := range cats {
index = append(index, name)
ids[name] = id
}
sort.Strings(index)
// Creating index of categories
for k, name := range index {
categories = append(categories, Category{ids[name], name})
Index[ids[name]] = k
}
}
}
// All : function to get all categories depending on the actual website from config/categories.go
func All() Categories {
return categories
}
// Get : function to get a category by the key in the index array
func Get(key int) Category {
return categories[key]
}
// Get : function to get a category by the id of the category from the database
func GetByID(id string) (Category, bool) {
if key, ok := Index[id]; ok {
return categories[key], true
}
return Category{"", ""}, false
}
// Exists : Check if a category exist in config
func (cats Categories) Exists(category string) bool {
for _, cat := range cats {
if cat.Name == category {
return true
}
}
return false
}
// Exists : Check if a category exist in config
func Exists(category string) bool {
return categories.Exists(category)
}
// GetSelect : Format categories in map ordered alphabetically
func GetSelect(keepParent bool, keepChild bool) map[string]string {
catSelect := make(map[string]string, len(categories))
for _, v := range categories {
if (keepParent && keepChild) || (len(v.ID) > 2 && !keepParent) || (len(v.ID) <= 2 && !keepChild) {
catSelect[v.Name] = v.ID
}
}
return catSelect
}