This commit is contained in:
2026-05-30 09:24:42 +08:00
parent 6c3de4b2ef
commit e054f458bb
17 changed files with 533 additions and 78 deletions

View File

@@ -54,8 +54,10 @@ type Playlist struct {
CheckedAt int64 `json:"checkedAt"` // Время проверки в формате UNIX timestamp
}
// tmpChannel хранит временные данные о канале, который обрабатывается в Parse
var tmpChannel = Channel{}
var (
attrRegex = regexp.MustCompile(`(?U)([a-z-]+)="(.*)"`)
titleRegex = regexp.MustCompile(`['"]?\s*,\s*(.+)`)
)
// MakeFromFile создаёт экземпляр плейлиста из файла
func MakeFromFile(filepath string) (Playlist, error) {
@@ -98,8 +100,7 @@ func MakeFromUrl(url string) (Playlist, error) {
// parseAttributes парсит атрибуты тегов #EXT*
func parseAttributes(line string) map[string]string {
result := make(map[string]string)
regex := regexp.MustCompile(`(?U)([a-z-]+)="(.*)"`)
regexMatches := regex.FindAllStringSubmatch(line, -1)
regexMatches := attrRegex.FindAllStringSubmatch(line, -1)
for _, match := range regexMatches {
result[match[1]] = match[2]
}
@@ -110,8 +111,7 @@ func parseAttributes(line string) map[string]string {
func parseTitle(line string) string {
// сначала пытаемся по-доброму: в строке есть тег, могут быть атрибуты,
// есть запятая-разделитель, после неё -- название канала (с запятыми или без)
regex := regexp.MustCompile(`['"]?\s*,\s*(.+)`)
regexMatches := regex.FindAllStringSubmatch(line, -1)
regexMatches := titleRegex.FindAllStringSubmatch(line, -1)
if len(regexMatches) > 0 && len(regexMatches[0]) >= 2 {
return strings.TrimSpace(regexMatches[0][1])
}
@@ -159,6 +159,7 @@ func (pls *Playlist) ReadFromFs() error {
// Parse разбирает плейлист
func (pls *Playlist) Parse() Playlist {
isChannel := false
tmpChannel := Channel{}
pls.Attributes = make(map[string]string)
pls.Channels = make(map[string]Channel)
pls.Groups = make(map[string]Group)