106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
type plexStats struct {
|
|
currentSessions float64
|
|
numMovies float64
|
|
numTV float64
|
|
}
|
|
|
|
type PlexClient struct {
|
|
baseUrl string
|
|
client *http.Client
|
|
plexToken string
|
|
}
|
|
|
|
func setupClient(settings *serverSettings) PlexClient {
|
|
transCfg := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
}
|
|
|
|
return PlexClient{
|
|
baseUrl: settings.plexAddr,
|
|
client: &http.Client{Transport: transCfg},
|
|
plexToken: settings.plexToken,
|
|
}
|
|
|
|
}
|
|
|
|
// sendRequest sends a request and resturns response (always assuming a get request)
|
|
func (pc *PlexClient) sendRequest(url string, body []byte) (result []byte) {
|
|
fullUrl := pc.baseUrl + url
|
|
req, err := http.NewRequest("GET", fullUrl, bytes.NewBuffer(body))
|
|
if err != nil {
|
|
fmt.Println("Error creating new request", err)
|
|
return
|
|
}
|
|
req.Header.Add("Accept", "application/json")
|
|
q := req.URL.Query()
|
|
q.Add("X-Plex-Token", pc.plexToken)
|
|
req.URL.RawQuery = q.Encode()
|
|
res, err := pc.client.Do(req)
|
|
if err != nil {
|
|
fmt.Println("Error sending request", err)
|
|
return
|
|
}
|
|
defer res.Body.Close()
|
|
resBody, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
fmt.Println("Unable to read response body: ", err)
|
|
return
|
|
}
|
|
return resBody
|
|
|
|
}
|
|
|
|
func (pc *PlexClient) getActiveSessions() *ActiveSessions {
|
|
result := pc.sendRequest("/status/sessions", nil)
|
|
currentActiveSessions := ActiveSessions{}
|
|
err := json.Unmarshal(result, ¤tActiveSessions)
|
|
if err != nil {
|
|
fmt.Println("Error unmarshalling current active sessions: ", err)
|
|
}
|
|
return ¤tActiveSessions
|
|
}
|
|
|
|
func (pc *PlexClient) getNumMovies() *ActiveSessions {
|
|
result := pc.sendRequest("/library/sections/3/all", nil) //Hard code library ID for now TODO: Grab from library/sections call
|
|
libraryAll := ActiveSessions{}
|
|
err := json.Unmarshal(result, &libraryAll)
|
|
if err != nil {
|
|
fmt.Println("Error unmarshalling current active sessions: ", err)
|
|
}
|
|
return &libraryAll
|
|
}
|
|
|
|
func (pc *PlexClient) getNumTV() *ActiveSessions {
|
|
result := pc.sendRequest("/library/sections/2/all", nil) //Hard code library ID for now TODO: Grab from library/sections call
|
|
libraryAll := ActiveSessions{}
|
|
err := json.Unmarshal(result, &libraryAll)
|
|
if err != nil {
|
|
fmt.Println("Error unmarshalling current active sessions: ", err)
|
|
}
|
|
return &libraryAll
|
|
}
|
|
|
|
func (pc *PlexClient) gatherAllStats() *plexStats {
|
|
allStats := plexStats{}
|
|
|
|
sessionsData := pc.getActiveSessions()
|
|
libraryDetails := pc.getNumMovies()
|
|
tvshowDetails := pc.getNumTV()
|
|
|
|
allStats.currentSessions = float64(sessionsData.MediaContainer.Size)
|
|
allStats.numMovies = float64(libraryDetails.MediaContainer.Size)
|
|
allStats.numTV = float64(tvshowDetails.MediaContainer.Size)
|
|
return &allStats
|
|
}
|