forked from zikwall/gom3u-content-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m3u-content-parser.go
98 lines (73 loc) · 1.97 KB
/
m3u-content-parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gom3u_content_parser
import (
"regexp"
)
type M3UContentParser struct {
m3uFileContent string
dirtyItems []string
Items []M3UItem
CountItems int
TvgUrl string
Cache int
Refresh int
Offsets int
Limits int
}
func NewM3UContentParser() *M3UContentParser {
return &M3UContentParser{}
}
func (parser *M3UContentParser) LoadSource(source string, fromFile bool) *M3UContentParser {
if fromFile {
parser.m3uFileContent = ReadStringContentFromFile(source)
} else {
parser.m3uFileContent = ReadStringContentFromRemote(source)
}
return parser
}
func (parser *M3UContentParser) Parse() *M3UContentParser {
a := regexp.MustCompile(`(#EXTINF:0|#EXTINF:-1|#EXTINF:-1,)`)
parser.dirtyItems = a.Split(parser.m3uFileContent, -1)
// first is #EXTM3U tag
parser.parseAndSetTvgUrl(parser.dirtyItems[0])
parser.dirtyItems = parser.dirtyItems[1:]
for _, item := range parser.dirtyItems {
parser.Items = append(parser.Items, *NewM3UItem(item))
parser.CountItems++
}
return parser
}
func (parser *M3UContentParser) parseAndSetTvgUrl(url string) *M3UContentParser {
re := regexp.MustCompile(`"([^"]+)"`)
matches := re.FindAllString(url, -1)
// if tv program exist
if len(matches) > 0 {
parser.TvgUrl = matches[0]
}
return parser
}
func (parser *M3UContentParser) GetTvgUrl() string {
return parser.TvgUrl
}
func (parser *M3UContentParser) GetM3UContent() string {
return parser.m3uFileContent
}
func (parser *M3UContentParser) GetDirtyItems() []string {
return parser.dirtyItems
}
func (parser *M3UContentParser) GetItems() []M3UItem {
return parser.Items
}
func (parser *M3UContentParser) Offset(offset int) *M3UContentParser {
parser.Offsets = offset
return parser
}
func (parser *M3UContentParser) Limit(limit int) *M3UContentParser {
parser.Limits = limit
return parser
}
func (parser *M3UContentParser) All() []M3UItem {
if parser.Limits <= 0 {
parser.Limits = parser.CountItems
}
return parser.Items[parser.Offsets:parser.Limits]
}