66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package media
|
|
|
|
import (
|
|
"m3u8-downloader/pkg/constants"
|
|
"strings"
|
|
)
|
|
|
|
func ParseAttribute(attribute string) string {
|
|
parts := strings.Split(attribute, "=")
|
|
if len(parts) >= 2 {
|
|
return parts[1]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func StripQuotes(input string) string {
|
|
return strings.Trim(input, "\"")
|
|
}
|
|
|
|
func ParseMediaAttributes(mediaInfo string) map[string]string {
|
|
attributes := make(map[string]string)
|
|
|
|
// Remove the #EXT-X-MEDIA: prefix
|
|
content := strings.TrimPrefix(mediaInfo, constants.ExtXMedia+":")
|
|
|
|
// Split by comma, but respect quoted values
|
|
parts := splitRespectingQuotes(content, ',')
|
|
|
|
for _, part := range parts {
|
|
if kv := strings.SplitN(strings.TrimSpace(part), "=", 2); len(kv) == 2 {
|
|
attributes[kv[0]] = kv[1]
|
|
}
|
|
}
|
|
|
|
return attributes
|
|
}
|
|
|
|
func splitRespectingQuotes(input string, delimiter rune) []string {
|
|
var result []string
|
|
var current strings.Builder
|
|
inQuotes := false
|
|
|
|
for _, char := range input {
|
|
switch char {
|
|
case '"':
|
|
inQuotes = !inQuotes
|
|
current.WriteRune(char)
|
|
case delimiter:
|
|
if inQuotes {
|
|
current.WriteRune(char)
|
|
} else {
|
|
result = append(result, current.String())
|
|
current.Reset()
|
|
}
|
|
default:
|
|
current.WriteRune(char)
|
|
}
|
|
}
|
|
|
|
if current.Len() > 0 {
|
|
result = append(result, current.String())
|
|
}
|
|
|
|
return result
|
|
}
|