-
Notifications
You must be signed in to change notification settings - Fork 4
/
discover.go
85 lines (73 loc) · 1.58 KB
/
discover.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
package plex
import (
"encoding/json"
"net/http"
"net/url"
"strings"
)
type Path struct {
s string
}
// https://watch.plex.tv/movie/the-hurt-locker
// https://watch.plex.tv/watch/movie/the-hurt-locker
func (p *Path) Set(s string) error {
s = strings.TrimPrefix(s, "https://watch.plex.tv")
p.s = strings.TrimPrefix(s, "/watch")
return nil
}
func (p Path) String() string {
return p.s
}
func (a Anonymous) Discover(p Path) (*DiscoverMatch, error) {
req, err := http.NewRequest(
"GET", "https://discover.provider.plex.tv/library/metadata/matches", nil,
)
if err != nil {
return nil, err
}
req.Header.Set("accept", "application/json")
req.URL.RawQuery = url.Values{
"url": {p.s},
"x-plex-token": {a.AuthToken},
}.Encode()
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var match struct {
MediaContainer struct {
Metadata []DiscoverMatch
}
}
if err := json.NewDecoder(res.Body).Decode(&match); err != nil {
return nil, err
}
return &match.MediaContainer.Metadata[0], nil
}
type DiscoverMatch struct {
GrandparentTitle string
Index int
ParentIndex int
RatingKey string
Title string
Year int
}
type Namer struct {
D *DiscoverMatch
}
func (n Namer) Episode() int {
return n.D.Index
}
func (n Namer) Season() int {
return n.D.ParentIndex
}
func (n Namer) Show() string {
return n.D.GrandparentTitle
}
func (n Namer) Title() string {
return n.D.Title
}
func (n Namer) Year() int {
return n.D.Year
}