Files
iptvc/app/tagfile/tagfile.go

101 lines
2.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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"`
TvgName string `json:"tvg-name"`
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.TvgName != "" {
regex, err = regexp.Compile(block.TvgName)
if err != nil {
return result
}
if _, ok := ch.Attributes["tvg-name"]; !ok {
return result
}
checkString = ch.Attributes["tvg-name"]
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: all channels will be untagged due to error:", err)
return nil
}
content, err := os.ReadFile(pathNormalized)
if err != nil {
log.Println("Warning: all channels will be untagged due to error:", err)
return nil
}
var blocks []TagBlock
err = json.Unmarshal(content, &blocks)
if err != nil {
log.Println("Warning: all channels will be untagged due to error:", err)
return nil
}
//TODO валидация полей: обязательны tvg-id или title, tags может быть пустым
return blocks
}