-
Notifications
You must be signed in to change notification settings - Fork 6
/
plex.go
108 lines (97 loc) · 2.34 KB
/
plex.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
package plex
import (
"encoding/json"
"net/http"
"net/url"
)
func (a Anonymous) Video(match *DiscoverMatch) (*OnDemand, error) {
req, err := http.NewRequest("GET", "https://vod.provider.plex.tv", nil)
if err != nil {
return nil, err
}
req.URL.Path = "/library/metadata/" + match.RatingKey
req.Header = http.Header{
"accept": {"application/json"},
"x-plex-token": {a.AuthToken},
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var s struct {
MediaContainer struct {
Metadata []OnDemand
}
}
if err := json.NewDecoder(res.Body).Decode(&s); err != nil {
return nil, err
}
return &s.MediaContainer.Metadata[0], nil
}
type MediaPart struct {
Key string
License string
}
func (MediaPart) RequestBody(b []byte) ([]byte, error) {
return b, nil
}
func (MediaPart) RequestHeader() (http.Header, error) {
return http.Header{}, nil
}
func (p MediaPart) RequestUrl() (string, bool) {
return p.License, true
}
func (MediaPart) ResponseBody(b []byte) ([]byte, error) {
return b, nil
}
type OnDemand struct {
Media []struct {
Part []MediaPart
Protocol string
}
}
func (o OnDemand) DASH(a Anonymous) (*MediaPart, bool) {
for _, media := range o.Media {
if media.Protocol == "dash" {
p := media.Part[0]
p.Key = a.abs(p.Key, url.Values{})
p.License = a.abs(p.License, url.Values{
"x-plex-drm": {"widevine"},
})
return &p, true
}
}
return nil, false
}
type Anonymous struct {
AuthToken string
}
func (a *Anonymous) New() error {
req, err := http.NewRequest(
"POST", "https://plex.tv/api/v2/users/anonymous", nil,
)
if err != nil {
return err
}
req.Header = http.Header{
"Accept": {"application/json"},
"X-Plex-Product": {"Plex Mediaverse"},
"X-Plex-Client-Identifier": {"!"},
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
return json.NewDecoder(res.Body).Decode(a)
}
func (a Anonymous) abs(path string, query url.Values) string {
query.Set("x-plex-token", a.AuthToken)
var u url.URL
u.Host = "vod.provider.plex.tv"
u.Path = path
u.RawQuery = query.Encode()
u.Scheme = "https"
return u.String()
}