forked from lithdew/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
44 lines (35 loc) · 929 Bytes
/
player.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
package youtube
import (
"errors"
"fmt"
"regexp"
"time"
)
var (
RegexWatchPlayerConfig = regexp.MustCompile(`ytplayer\.config = ({(?:"\w+":(?:.*?))*});`)
RegexEmbedPlayerConfig = regexp.MustCompile(`yt\.setConfig\({'PLAYER_CONFIG': (.*?)}\)`)
)
type Player struct {
Transport
Assets
Streams
}
func (p Player) ResolveURL(v Format) (string, error) {
return p.ResolveURLDeadline(v, zeroTime)
}
func (p Player) ResolveURLTimeout(v Format, timeout time.Duration) (string, error) {
return p.ResolveURLDeadline(v, time.Now().Add(timeout))
}
func (p Player) ResolveURLDeadline(v Format, deadline time.Time) (string, error) {
if v.URL != nil {
return *v.URL, nil
}
if v.Cipher == nil {
return "", errors.New("no url could be found")
}
script, err := p.Assets.LoadJSDeadline(p, deadline)
if err != nil {
return "", fmt.Errorf("failed to load player script: %w", err)
}
return v.Cipher.DecodeURL(script)
}