Skip to content

Commit a6f5268

Browse files
committed
Merge branch 'develop'
2 parents f778f6f + 8d83825 commit a6f5268

File tree

10 files changed

+367
-63
lines changed

10 files changed

+367
-63
lines changed

README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ the performance of a low-end server.
99
2. Cross-device response caching by client type
1010
3. Disable transcoding by forcing direct play/stream
1111
4. Redirect web app to [official one](https://app.plex.tv/desktop)
12+
5. [Plaxt](https://github.com/XanderStrike/goplaxt) integration
1213

1314
## Prerequisites
1415

@@ -31,13 +32,9 @@ env CGO_ENABLED=0 go install -trimpath -ldflags="-s -w" github.com/RoyXiang/plex
3132
* If you need a cache layer, set a value for it
3233
- `PLEX_TOKEN` (Optional, if you need it, see [here](https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/))
3334
* It is used to receive notifications from Plex Media Server
34-
* Currently, notifications are used to flush the cache of metadata
35-
- `PLAXT_BASEURL` (Optional)
36-
* Set it only if you run an instance of [plaxt](https://github.com/RoyXiang/goplaxt/releases) (forked version)
37-
* Or, you can set it to [my hosted one](https://plaxt.royxiang.me), e.g. `https://plaxt.royxiang.me`
38-
* Currently, it is finally used to sync the realtime playback progress with [Trakt](https://trakt.tv/)
35+
* Notifications are used to flush the cache of metadata
36+
- `PLAXT_URL` (Optional, e.g. `https://plaxt.astandke.com/api?id=generate-your-own-silly`)
37+
* `PLEX_TOKEN` is required
38+
* Set it if you run an instance of [Plaxt](https://github.com/XanderStrike/goplaxt)
39+
* Or, you can set it to [the official one](https://plaxt.astandke.com/)
3940
2. Run the program
40-
41-
## TODO
42-
43-
- [ ] Native [Trakt](https://trakt.tv/) integration

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/gorilla/mux v1.8.0
99
github.com/gorilla/websocket v1.5.0
1010
github.com/jrudio/go-plex-client v0.0.0-20220106065909-9e1d590b99aa
11+
github.com/xanderstrike/plexhooks v0.0.0-20200926011736-c63bcd35fe3e
1112
)
1213

1314
require (
@@ -16,4 +17,4 @@ require (
1617
github.com/google/uuid v1.3.0 // indirect
1718
)
1819

19-
replace github.com/jrudio/go-plex-client v0.0.0-20220106065909-9e1d590b99aa => github.com/RoyXiang/go-plex-client v0.0.0-20220221154622-936c609dd907
20+
replace github.com/jrudio/go-plex-client v0.0.0-20220106065909-9e1d590b99aa => github.com/RoyXiang/go-plex-client v0.0.0-20220223140842-7433de7e9b77

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
33
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
4-
github.com/RoyXiang/go-plex-client v0.0.0-20220221154622-936c609dd907 h1:R1HVInXY+cYDfNx27G3/olkiDfYzsm+N573tUKspBIY=
5-
github.com/RoyXiang/go-plex-client v0.0.0-20220221154622-936c609dd907/go.mod h1:NICqgLUxSYsDHh3n+m6xomGmRbqLxBcN4D7Jb9Z6LJ0=
4+
github.com/RoyXiang/go-plex-client v0.0.0-20220223140842-7433de7e9b77 h1:GVtLzX8S4rd6u9OflS/huI2ilbXVsxSpjipLtx/Jm2c=
5+
github.com/RoyXiang/go-plex-client v0.0.0-20220223140842-7433de7e9b77/go.mod h1:NICqgLUxSYsDHh3n+m6xomGmRbqLxBcN4D7Jb9Z6LJ0=
66
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
77
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
88
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
@@ -101,6 +101,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
101101
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
102102
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
103103
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
104+
github.com/xanderstrike/plexhooks v0.0.0-20200926011736-c63bcd35fe3e h1:IeBBs3KadFYmbWcRw/qBuM3bscr2iKC1hYBr8GS5Gps=
105+
github.com/xanderstrike/plexhooks v0.0.0-20200926011736-c63bcd35fe3e/go.mod h1:IXlzovfjAZX8O7nzPwCiYe2T0i/0enZzzqkvVq8N02M=
104106
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
105107
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
106108
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

handler/const.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const (
1212
headerPageSize = "X-Plex-Container-Size"
1313
headerPageStart = "X-Plex-Container-Start"
1414
headerToken = "X-Plex-Token"
15+
headerUserId = "X-Plex-User-Id"
1516
headerAccept = "Accept"
1617
headerAcceptLanguage = "Accept-Language"
1718
headerCacheControl = "Cache-Control"
@@ -32,4 +33,12 @@ const (
3233

3334
contentTypeAny = "*/*"
3435
contentTypeXml = "xml"
36+
37+
watchedThreshold = 90
38+
)
39+
40+
const (
41+
sessionUnplayed sessionStatus = iota
42+
sessionPlaying
43+
sessionWatched
3544
)

handler/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ var (
1616
plexClient *PlexClient
1717
redisClient *redis.Client
1818

19+
emptyStruct = struct{}{}
20+
1921
mu sync.RWMutex
2022
ml common.MultipleLock
2123
)
2224

2325
func init() {
2426
plexClient = NewPlexClient(PlexConfig{
25-
BaseUrl: os.Getenv("PLEX_BASEURL"),
26-
Token: os.Getenv("PLEX_TOKEN"),
27-
PlaxtBaseUrl: os.Getenv("PLAXT_BASEURL"),
27+
BaseUrl: os.Getenv("PLEX_BASEURL"),
28+
Token: os.Getenv("PLEX_TOKEN"),
29+
PlaxtUrl: os.Getenv("PLAXT_URL"),
2830
})
2931
if plexClient == nil {
3032
log.Fatalln("Please configure PLEX_BASEURL as a valid URL at first")
@@ -49,9 +51,8 @@ func NewRouter() http.Handler {
4951
r.Use(trafficMiddleware)
5052

5153
if redisClient != nil {
52-
r.Path("/:/eventsource/notifications").Handler(plexClient)
53-
r.Path("/:/timeline").Handler(plexClient)
54-
r.Path("/:/websockets/notifications").Handler(plexClient)
54+
// bypass cache
55+
r.PathPrefix("/:/").Handler(plexClient)
5556
r.PathPrefix("/library/parts/").Handler(plexClient)
5657

5758
staticRouter := r.Methods(http.MethodGet).Subrouter()

handler/middleware.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"strconv"
1313
"strings"
1414
"time"
15+
16+
"github.com/go-chi/chi/v5/middleware"
1517
)
1618

1719
var (
@@ -146,13 +148,13 @@ func cacheMiddleware(next http.Handler) http.Handler {
146148
resp, _ = http.ReadResponse(reader, r)
147149
}
148150
if resp == nil {
149-
nw := httptest.NewRecorder()
151+
nw := middleware.NewWrapResponseWriter(httptest.NewRecorder(), r.ProtoMajor)
150152
next.ServeHTTP(nw, r)
151-
resp = nw.Result()
153+
resp = nw.Unwrap().(*httptest.ResponseRecorder).Result()
152154
defer func() {
153155
w.Header().Set(headerCacheStatus, "MISS")
154156
w.WriteHeader(resp.StatusCode)
155-
_, _ = w.Write(nw.Body.Bytes())
157+
_, _ = w.Write(nw.Unwrap().(*httptest.ResponseRecorder).Body.Bytes())
156158
if resp.StatusCode == http.StatusOK {
157159
writeToCache(cacheKey, resp, info.Ttl)
158160
}
@@ -185,7 +187,7 @@ func cacheMiddleware(next http.Handler) http.Handler {
185187
}
186188
userId := plexClient.GetUserId(token)
187189
if userId > 0 {
188-
params.Set("X-Plex-User-Id", strconv.Itoa(userId))
190+
params.Set(headerUserId, strconv.Itoa(userId))
189191
params.Set(headerAccept, getAcceptContentType(r))
190192
} else {
191193
params.Set(headerToken, token)

0 commit comments

Comments
 (0)