-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplayback.go
95 lines (88 loc) · 2.03 KB
/
playback.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
package amc
// github.com/89z
import (
"bytes"
"errors"
"github.com/89z/mech"
"github.com/89z/mech/widevine"
"io"
"net/http"
"strings"
)
func (p Playback) Key(privateKey, clientID, kID []byte) ([]byte, error) {
source := p.DASH()
mod, err := widevine.NewModule(privateKey, clientID, kID)
if err != nil {
return nil, err
}
in, err := mod.Marshal()
if err != nil {
return nil, err
}
req, err := http.NewRequest(
"POST", source.Key_Systems.Widevine.License_URL, bytes.NewReader(in),
)
if err != nil {
return nil, err
}
req.Header.Set("bcov-auth", p.BcJWT)
LogLevel.Dump(req)
res, err := new(http.Transport).RoundTrip(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.New(res.Status)
}
out, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
keys, err := mod.Unmarshal(out)
if err != nil {
return nil, err
}
return keys.Content().Key, nil
}
func (p Playback) DASH() *Source {
for _, source := range p.PlaybackJsonData.Sources {
if source.Type == "application/dash+xml" {
return &source
}
}
return nil
}
type Playback struct {
PlaybackJsonData PlaybackJsonData
BcJWT string
}
func (p Playback) Base() string {
var buf strings.Builder
buf.WriteString(p.PlaybackJsonData.Custom_Fields.Show)
buf.WriteByte('-')
buf.WriteString(p.PlaybackJsonData.Custom_Fields.Season)
buf.WriteByte('-')
buf.WriteString(p.PlaybackJsonData.Custom_Fields.Episode)
buf.WriteByte('-')
buf.WriteString(mech.Clean(p.PlaybackJsonData.Name))
return buf.String()
}
type PlaybackJsonData struct {
Custom_Fields struct {
Show string // 1
Season string // 2
Episode string // 3
}
Name string // 4
Sources []Source
}
type Source struct {
Key_Systems *struct {
Widevine struct {
License_URL string
} `json:"com.widevine.alpha"`
}
Src string
Type string
}