-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tv.go
176 lines (149 loc) · 3.17 KB
/
tv.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package tv
import (
"errors"
"fmt"
"net/url"
"strings"
"golang.org/x/net/publicsuffix"
)
const (
EmptyRoomName = ""
EmptyStreamerName = ""
)
var (
_ ITv = (*Tv)(nil)
ErrNotSupported = errors.New("streamer not supported")
ErrSiteInvalid = errors.New("site invalid")
)
type ITv interface {
Snap() error
StreamUrl() (string, bool)
RoomName() (string, bool)
StreamerName() (string, bool)
SiteName() string
}
type Tv struct {
SiteID string
RoomID string
cookie string
*Info
}
func New(siteID, roomID string, opts ...Option) (*Tv, error) {
_, valid := Sniff(siteID)
if !valid {
return nil, ErrNotSupported
}
t := &Tv{
SiteID: siteID,
RoomID: roomID,
}
for _, opt := range opts {
opt(t)
}
return t, nil
}
func NewWithUrl(roomUrl string, opts ...Option) (*Tv, error) {
u := RoomUrl(roomUrl)
t, err := u.Stream()
if err != nil {
err = fmt.Errorf("%+v (err msg = %s)", ErrNotSupported, err.Error())
return nil, err
}
for _, opt := range opts {
opt(t)
}
return t, nil
}
type Option func(*Tv) error
func SetCookie(cookie string) Option {
return func(t *Tv) error {
t.cookie = cookie
return nil
}
}
type Info struct {
Timestamp int64
streamUrl string
roomOn bool
roomName string
streamerName string
}
// Snap takes the latest snapshot of the streamer info that could be retrieved individually.
func (tv *Tv) Snap() error {
if tv == nil {
return errors.New("tv is nil")
}
site, ok := Sniff(tv.SiteID)
if !ok {
return fmt.Errorf("site(ID = %s) not supported", tv.SiteID)
}
return site.Snap(tv)
}
func (tv *Tv) SiteName() string {
if tv == nil {
return ""
}
site, ok := Sniff(tv.SiteID)
if !ok {
return ""
}
return site.Name()
}
func (tv *Tv) StreamUrl() (string, bool) {
if tv == nil || tv.Info == nil {
return "", false
}
return tv.streamUrl, tv.roomOn
}
func (tv *Tv) RoomName() (string, bool) {
if tv == nil || tv.Info == nil {
return "", false
}
return tv.roomName, tv.roomName != EmptyRoomName
}
func (tv *Tv) StreamerName() (string, bool) {
if tv == nil || tv.Info == nil {
return "", false
}
return tv.streamerName, tv.streamerName != EmptyStreamerName
}
func (tv *Tv) String() string {
sb := &strings.Builder{}
sb.WriteString("Powered by go-olive/tv\n")
sb.WriteString(format("SiteID", tv.SiteID))
sb.WriteString(format("SiteName", tv.SiteName()))
sb.WriteString(format("RoomID", tv.RoomID))
if roomName, ok := tv.RoomName(); ok {
sb.WriteString(format("RoomName", roomName))
}
if streamerName, ok := tv.StreamerName(); ok {
sb.WriteString(format("Streamer", streamerName))
}
if streamUrl, ok := tv.StreamUrl(); ok {
sb.WriteString(format("StreamUrl", streamUrl))
}
return sb.String()
}
func format(k, v string) string {
return fmt.Sprintf(" %-12s%-s\n", k, v)
}
type RoomUrl string
func (this RoomUrl) SiteID() string {
u, err := url.Parse(string(this))
if err != nil {
return ""
}
eTLDPO, err := publicsuffix.EffectiveTLDPlusOne(u.Hostname())
if err != nil {
return ""
}
siteID := strings.Split(eTLDPO, ".")[0]
return siteID
}
func (this RoomUrl) Stream() (*Tv, error) {
site, ok := Sniff(this.SiteID())
if !ok {
return nil, ErrSiteInvalid
}
return site.Permit(this)
}