-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
youtube.go
71 lines (62 loc) · 1.41 KB
/
youtube.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
package tv
import (
"fmt"
"strings"
"time"
"github.com/go-olive/tv/util"
)
func init() {
registerSite("youtube", &youtube{})
}
type youtube struct {
base
}
func (this *youtube) Name() string {
return "油管"
}
func (this *youtube) Snap(tv *Tv) error {
tv.Info = &Info{
Timestamp: time.Now().Unix(),
}
streamID, err := this.setRoomOn(tv)
if err != nil {
return err
}
return this.setStreamURL(tv, streamID)
}
func (this *youtube) setRoomOn(tv *Tv) (string, error) {
channelURL := fmt.Sprintf("https://www.youtube.com/channel/%s", tv.RoomID)
content, err := util.GetURLContent(channelURL)
if err != nil {
return "", err
}
tv.roomOn = strings.Contains(content, `icon":{"iconType":"LIVE"}}`)
if !tv.roomOn {
return "", nil
}
streamID, err := util.Match(`"videoRenderer":{"videoId":"([^"]+)",`, content)
if err != nil {
return "", err
}
return streamID, nil
}
func (this *youtube) setStreamURL(tv *Tv, streamID string) error {
if !tv.roomOn {
return nil
}
// youtube possibly have multiple lives in one channel,
// curruently the program returns the first one.
roomURL := fmt.Sprintf("https://www.youtube.com/watch?v=%s", streamID)
tv.streamUrl = roomURL
roomContent, err := util.GetURLContent(roomURL)
if err != nil {
return err
}
title, err := util.Match(`name="title" content="([^"]+)"`, roomContent)
if err != nil {
return err
}
tv.roomName = title
tv.roomNameSet = true
return nil
}