Files
iptvc/app/tagfile/tagfile.go
AnthonyAxenov d15d4f47b6
All checks were successful
release / release (push) Successful in 5m47s
Initial commit
2025-05-06 10:45:37 +08:00

88 lines
2.2 KiB
Go
Raw 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"`
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
}