This commit is contained in:
2026-07-13 12:28:59 +08:00
parent 6c3de4b2ef
commit cd2ab11c44
65 changed files with 9901 additions and 413 deletions
+20 -21
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2025, Антон Аксенов
* Copyright (c) 2025-2026, Антон Аксенов
* This file is part of iptvc project
* MIT License: https://git.axenov.dev/IPTV/iptvc/src/branch/master/LICENSE
*/
@@ -54,9 +54,6 @@ type Playlist struct {
CheckedAt int64 `json:"checkedAt"` // Время проверки в формате UNIX timestamp
}
// tmpChannel хранит временные данные о канале, который обрабатывается в Parse
var tmpChannel = Channel{}
// MakeFromFile создаёт экземпляр плейлиста из файла
func MakeFromFile(filepath string) (Playlist, error) {
expandedPath, err := utils.ExpandPath(filepath)
@@ -131,8 +128,10 @@ func parseTitle(line string) string {
}
// Download загружает плейлист по URL-адресу
func (pls *Playlist) Download() error {
content, err := utils.Fetch(pls.Url)
// Download скачивает плейлист по URL.
// userAgent и timeout передаются в HTTP-клиент.
func (pls *Playlist) Download(userAgent string, timeout time.Duration) error {
content, err := utils.Fetch(pls.Url, userAgent, timeout)
if err != nil {
pls.Content = err.Error()
pls.CheckedAt = time.Now().Unix()
@@ -159,6 +158,7 @@ func (pls *Playlist) ReadFromFs() error {
// Parse разбирает плейлист
func (pls *Playlist) Parse() Playlist {
isChannel := false
var ch Channel
pls.Attributes = make(map[string]string)
pls.Channels = make(map[string]Channel)
pls.Groups = make(map[string]Group)
@@ -180,20 +180,20 @@ func (pls *Playlist) Parse() Playlist {
if strings.HasPrefix(line, "#EXTINF") {
isChannel = true
tmpChannel.Attributes = parseAttributes(line)
tmpChannel.Title = parseTitle(line)
ch = Channel{Attributes: parseAttributes(line)}
ch.Title = parseTitle(line)
if tmpChannel.Title == "" {
if tvgid, ok := tmpChannel.Attributes["tvg-id"]; ok {
tmpChannel.Title = "(канал без названия, tvg-id=" + tvgid + ")"
if ch.Title == "" {
if tvgid, ok := ch.Attributes["tvg-id"]; ok {
ch.Title = "(канал без названия, tvg-id=" + tvgid + ")"
} else {
tmpChannel.Title = "(канал без названия, tvg-id неизвестен)"
ch.Title = "(канал без названия, tvg-id неизвестен)"
}
}
if groupName, ok := tmpChannel.Attributes["group-title"]; ok {
if groupName, ok := ch.Attributes["group-title"]; ok {
id := utils.Md5str(groupName)
tmpChannel.GroupId = id
ch.GroupId = id
pls.Groups[id] = Group{
Id: id,
Name: groupName,
@@ -207,7 +207,7 @@ func (pls *Playlist) Parse() Playlist {
parts := strings.Split(line, ":")
groupName := strings.Trim(parts[1], " ")
id := utils.Md5str(groupName)
tmpChannel.GroupId = id
ch.GroupId = id
pls.Groups[id] = Group{
Id: id,
Name: groupName,
@@ -217,13 +217,12 @@ func (pls *Playlist) Parse() Playlist {
}
if isChannel && strings.HasPrefix(line, "http") {
tmpChannel.URL = strings.Trim(line, " ")
tmpChannel.Id = utils.Md5str(tmpChannel.URL)
if tmpChannel.Id != "" {
pls.Channels[tmpChannel.Id] = tmpChannel
ch.URL = strings.Trim(line, " ")
ch.Id = utils.Md5str(ch.URL)
if ch.Id != "" {
pls.Channels[ch.Id] = ch
isChannel = false
tmpChannel = Channel{}
tmpChannel.Attributes = make(map[string]string)
ch = Channel{}
}
}
}