-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
270 lines (235 loc) · 8.42 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
)
type metadataResponse struct {
MediaContainer *struct {
Size int `json:"size"`
AllowSync bool `json:"allowSync"`
AugmentationKey string `json:"augmentationKey"`
Identifier string `json:"identifier"`
LibrarySectionID int `json:"librarySectionID"`
LibrarySectionTitle string `json:"librarySectionTitle"`
LibrarySectionUUID string `json:"librarySectionUUID"`
MediaTagPrefix string `json:"mediaTagPrefix"`
MediaTagVersion int `json:"mediaTagVersion"`
Metadata []*struct {
RatingKey string `json:"ratingKey"`
Key string `json:"key"`
ParentRatingKey string `json:"parentRatingKey"`
GrandparentRatingKey string `json:"grandparentRatingKey"`
GUID string `json:"guid"`
ParentGUID string `json:"parentGuid"`
GrandparentGUID string `json:"grandparentGuid"`
Type string `json:"type"`
Title string `json:"title"`
GrandparentKey string `json:"grandparentKey"`
ParentKey string `json:"parentKey"`
LibrarySectionTitle string `json:"librarySectionTitle"`
LibrarySectionID int `json:"librarySectionID"`
LibrarySectionKey string `json:"librarySectionKey"`
GrandparentTitle string `json:"grandparentTitle"`
ParentTitle string `json:"parentTitle"`
ContentRating string `json:"contentRating"`
Summary string `json:"summary"`
Index int `json:"index"`
ParentIndex int `json:"parentIndex"`
Rating float64 `json:"rating"`
Year int `json:"year"`
Thumb string `json:"thumb"`
Art string `json:"art"`
ParentThumb string `json:"parentThumb"`
GrandparentThumb string `json:"grandparentThumb"`
GrandparentArt string `json:"grandparentArt"`
GrandparentTheme string `json:"grandparentTheme"`
Duration int `json:"duration"`
OriginallyAvailableAt string `json:"originallyAvailableAt"`
AddedAt int `json:"addedAt"`
UpdatedAt int `json:"updatedAt"`
Media []*media `json:"Media"`
Writer []*struct {
ID int `json:"id"`
Filter string `json:"filter"`
Tag string `json:"tag"`
} `json:"Writer"`
Extras *struct {
Size int `json:"size"`
} `json:"Extras"`
} `json:"Metadata"`
} `json:"MediaContainer"`
}
type media struct {
Title string `json:"title"`
ID int `json:"id"`
Duration int `json:"duration"`
Bitrate int `json:"bitrate"`
Width int `json:"width"`
Height int `json:"height"`
AspectRatio float64 `json:"aspectRatio"`
AudioChannels int `json:"audioChannels"`
AudioCodec string `json:"audioCodec"`
VideoCodec string `json:"videoCodec"`
VideoResolution string `json:"videoResolution"`
Container string `json:"container"`
VideoFrameRate string `json:"videoFrameRate"`
VideoProfile string `json:"videoProfile"`
Part []*struct {
ID int `json:"id"`
Key string `json:"key"`
Duration int `json:"duration"`
File string `json:"file"`
Size int `json:"size"`
Container string `json:"container"`
VideoProfile string `json:"videoProfile"`
Stream []*struct {
ID int `json:"id"`
StreamType int `json:"streamType"`
Default bool `json:"default"`
Codec string `json:"codec"`
Index int `json:"index"`
Bitrate int `json:"bitrate"`
Language string `json:"language"`
LanguageCode string `json:"languageCode"`
BitDepth int `json:"bitDepth,omitempty"`
ChromaLocation string `json:"chromaLocation,omitempty"`
ChromaSubsampling string `json:"chromaSubsampling,omitempty"`
CodedHeight string `json:"codedHeight,omitempty"`
CodedWidth string `json:"codedWidth,omitempty"`
ColorPrimaries string `json:"colorPrimaries,omitempty"`
ColorRange string `json:"colorRange,omitempty"`
ColorSpace string `json:"colorSpace,omitempty"`
ColorTrc string `json:"colorTrc,omitempty"`
FrameRate float64 `json:"frameRate,omitempty"`
HasScalingMatrix bool `json:"hasScalingMatrix,omitempty"`
Height int `json:"height,omitempty"`
Level int `json:"level,omitempty"`
Profile string `json:"profile,omitempty"`
RefFrames int `json:"refFrames,omitempty"`
ScanType string `json:"scanType,omitempty"`
Width int `json:"width,omitempty"`
DisplayTitle string `json:"displayTitle"`
Selected bool `json:"selected,omitempty"`
Channels int `json:"channels,omitempty"`
AudioChannelLayout string `json:"audioChannelLayout,omitempty"`
SamplingRate int `json:"samplingRate,omitempty"`
Title string `json:"title,omitempty"`
} `json:"Stream"`
} `json:"Part"`
}
var r = regexp.MustCompile(".* \\((.*)\\)$")
func getMediaTitle(media *media) string {
for _, part := range media.Part {
if part.Stream == nil {
continue
}
for _, stream := range part.Stream {
if stream.StreamType == 1 && stream.DisplayTitle != "" {
format := r.FindStringSubmatch(stream.DisplayTitle)
if format[1] != "" {
return format[1] + " " + media.Container
}
return media.Container
}
}
}
return "Unknown"
}
func main() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
bindAddress := flag.String("addr", "127.0.0.1:3213", "the address to bind to")
plexHost := flag.String("plex-host", "localhost:32401", "the host + port that your plex server is running on")
secure := flag.Bool("secure", true, "use https to connect to your plex server (will increase loading times) (needed if Secure Connections is set to Required)")
flag.Parse()
log.Printf("binding to %s\n", *bindAddress)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.URL.String(), "/library/metadata/") {
return
}
url := r.URL
url.Host = *plexHost
if *secure {
url.Scheme = "https"
} else {
url.Scheme = "http"
}
request, _ := http.NewRequest(r.Method, url.String(), nil)
for key, values := range r.Header {
if key == "Accept-Encoding" {
continue
}
for _, value := range values {
request.Header.Add(key, value)
}
}
response, err := http.DefaultClient.Do(request)
if err != nil {
log.Fatalf("error while firing request to server: %s\n", err.Error())
return
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("error while reading response body: %s\n", err.Error())
return
}
var parsed metadataResponse
err = json.Unmarshal(data, &parsed)
if err != nil {
log.Printf("error while parsing response: %s\n", err.Error())
w.Write(data)
return
}
if parsed.MediaContainer == nil || parsed.MediaContainer.Metadata == nil {
w.Write(data)
return
}
for _, meta := range parsed.MediaContainer.Metadata {
if meta.Media == nil {
continue
}
for _, media := range meta.Media {
if media.Part == nil {
continue
}
if media.Title == "" {
media.Title = getMediaTitle(media)
}
for _, part := range media.Part {
if part.Stream == nil {
continue
}
for _, stream := range part.Stream {
if stream.DisplayTitle == "" || stream.Title == "" {
continue
}
stream.DisplayTitle = stream.DisplayTitle + " (" + stream.Title + ")"
}
}
}
}
data, err = json.Marshal(parsed)
if err != nil {
log.Printf("error while marshaling response: %s\n", err.Error())
w.Write(data)
return
}
for key, values := range response.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}
w.Write(data)
})
err := http.ListenAndServe(*bindAddress, nil)
if err != nil {
log.Fatalf("error while starting server: %s\n", err.Error())
return
}
}