-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tv.go
139 lines (116 loc) · 2.26 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
package tv
import (
"fmt"
"net/url"
"strings"
"golang.org/x/net/publicsuffix"
)
var _ ITv = (*Tv)(nil)
type ITv interface {
Refresh()
StreamUrl() (string, bool)
RoomName() (string, bool)
StreamerName() (string, bool)
}
type Tv struct {
SiteID string
RoomID string
*Parms
*Info
}
func NewTv(siteID, roomID string) *Tv {
tv := &Tv{
SiteID: siteID,
RoomID: roomID,
}
return tv
}
type Parms struct {
Cookie string
}
type Option func(*Tv) error
type Info struct {
Timestamp int64
streamUrl string
roomOn bool
roomName string
roomNameSet bool
streamerName string
streamerNameSet bool
}
func (tv *Tv) Refresh() {
if tv == nil {
return
}
site, ok := Sniff(tv.SiteID)
if !ok {
return
}
site.Snap(tv)
}
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.roomNameSet
}
func (tv *Tv) StreamerName() (string, bool) {
if tv == nil || tv.Info == nil {
return "", false
}
return tv.streamerName, tv.streamerNameSet
}
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("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 Streamer interface {
Stream() *Tv
}
func (tv *Tv) Stream() *Tv {
return tv
}
type RoomUrl string
func NewRoomUrl(roomUrl string) RoomUrl {
return RoomUrl(roomUrl)
}
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 {
site, ok := Sniff(this.SiteID())
if !ok {
return nil
}
return site.Permit(this)
}