This commit is contained in:
87
app/tagfile/tagfile.go
Normal file
87
app/tagfile/tagfile.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2025, Антон Аксенов
|
||||
* This file is part of iptvc project
|
||||
* MIT License: https://git.axenov.dev/IPTV/iptvc/src/branch/master/LICENSE
|
||||
*/
|
||||
|
||||
package tagfile
|
||||
|
||||
import (
|
||||
"axenov/iptv-checker/app/playlist"
|
||||
"axenov/iptv-checker/app/utils"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TagBlock описывает объект с набором тегов, который подходит для каналов по регулярному выражению
|
||||
type TagBlock struct {
|
||||
TvgId string `json:"tvg-id"`
|
||||
Title string `json:"title"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
// GetTags возвращает теги, соответствующие каналу
|
||||
func (block *TagBlock) GetTags(ch playlist.Channel) []string {
|
||||
var regex *regexp.Regexp
|
||||
var checkString string
|
||||
var err error
|
||||
result := make([]string, 0)
|
||||
|
||||
if block.TvgId != "" {
|
||||
regex, err = regexp.Compile(block.TvgId)
|
||||
if err != nil {
|
||||
return result
|
||||
}
|
||||
if _, ok := ch.Attributes["tvg-id"]; !ok {
|
||||
return result
|
||||
}
|
||||
checkString = ch.Attributes["tvg-id"]
|
||||
if checkString == "" {
|
||||
return result
|
||||
}
|
||||
} else if block.Title != "" {
|
||||
regex, err = regexp.Compile(block.Title)
|
||||
if err != nil {
|
||||
return result
|
||||
}
|
||||
checkString = ch.Title
|
||||
} else {
|
||||
return result
|
||||
}
|
||||
|
||||
checkString = strings.ToLower(checkString)
|
||||
check := regex.MatchString(checkString)
|
||||
if !check {
|
||||
return result
|
||||
}
|
||||
return block.Tags
|
||||
}
|
||||
|
||||
// Init инициализирует объекты тегов из тегфайла
|
||||
func Init(path string) []TagBlock {
|
||||
pathNormalized, _ := utils.ExpandPath(path)
|
||||
_, err := os.Stat(pathNormalized)
|
||||
if err != nil {
|
||||
log.Println("Warning: tagfile load error (", err, "), all channels will be untagged")
|
||||
return nil
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(pathNormalized)
|
||||
if err != nil {
|
||||
log.Println("Warning: tagfile load error (", err, "), all channels will be untagged")
|
||||
return nil
|
||||
}
|
||||
|
||||
var blocks []TagBlock
|
||||
err = json.Unmarshal(content, &blocks)
|
||||
if err != nil {
|
||||
log.Println("Warning: tagfile load error (", err, "), all channels will be untagged")
|
||||
return nil
|
||||
}
|
||||
|
||||
//TODO валидация полей: обязательны tvg-id или title, tags может быть пустым
|
||||
return blocks
|
||||
}
|
||||
Reference in New Issue
Block a user