mirror of
https://github.com/aunefyren/wrapperr
synced 2024-11-10 13:44:12 +00:00
787b00aebc
Different components moved to packages which are imported. More proper and clean compared to previous implementation.
161 lines
4.8 KiB
Go
161 lines
4.8 KiB
Go
package modules
|
|
|
|
import (
|
|
"aunefyren/wrapperr/models"
|
|
"aunefyren/wrapperr/utilities"
|
|
"encoding/json"
|
|
"errors"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func TautulliTestConnection(TautulliPort int, TautulliIP string, TautulliHttps bool, TautulliRoot string, TautulliApiKey string) (bool, error) {
|
|
|
|
url_string, err := utilities.BuildURL(TautulliPort, TautulliIP, TautulliHttps, TautulliRoot)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return false, errors.New("Failed to build Tautulli connection URL.")
|
|
}
|
|
|
|
url_string = url_string + "api/v2/" + "?apikey=" + TautulliApiKey + "&cmd=status"
|
|
|
|
params := url.Values{}
|
|
payload := strings.NewReader(params.Encode())
|
|
|
|
req, err := http.NewRequest("GET", url_string, payload)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return false, errors.New("Failed to reach Tautulli server.")
|
|
}
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return false, errors.New("Failed to reach Tautulli server.")
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
var body_reply models.TautulliStatusReply
|
|
json.Unmarshal(body, &body_reply)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return false, errors.New("Failed to parse Tautulli response.")
|
|
}
|
|
|
|
var tautulli_status bool = false
|
|
|
|
if body_reply.Response.Result == "success" {
|
|
|
|
tautulli_status = true
|
|
|
|
}
|
|
|
|
return tautulli_status, nil
|
|
}
|
|
|
|
func TautulliGetUserId(TautulliPort int, TautulliIP string, TautulliHttps bool, TautulliRoot string, TautulliApiKey string, PlexUser string) (int, string, error) {
|
|
|
|
url_string, err := utilities.BuildURL(TautulliPort, TautulliIP, TautulliHttps, TautulliRoot)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, "", errors.New("Failed to build Tautulli connection URL.")
|
|
}
|
|
|
|
url_string = url_string + "api/v2/" + "?apikey=" + TautulliApiKey + "&cmd=get_users"
|
|
|
|
params := url.Values{}
|
|
payload := strings.NewReader(params.Encode())
|
|
|
|
req, err := http.NewRequest("GET", url_string, payload)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, "", errors.New("Failed to reach Tautulli server.")
|
|
}
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, "", errors.New("Failed to reach Tautulli server.")
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
var body_reply models.TautulliGetUsersReply
|
|
json.Unmarshal(body, &body_reply)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return 0, "", errors.New("Failed to parse Tautulli response.")
|
|
}
|
|
|
|
for i := 0; i < len(body_reply.Response.Data); i++ {
|
|
if body_reply.Response.Data[i].UserID != 0 && (strings.ToLower(body_reply.Response.Data[i].Username) == strings.ToLower(PlexUser) || strings.ToLower(body_reply.Response.Data[i].Email) == strings.ToLower(PlexUser)) {
|
|
|
|
var username string
|
|
|
|
if body_reply.Response.Data[i].FriendlyName != "" {
|
|
username = body_reply.Response.Data[i].FriendlyName
|
|
} else if body_reply.Response.Data[i].Username != "" {
|
|
username = body_reply.Response.Data[i].Username
|
|
} else {
|
|
return 0, "", errors.New("Failed retrieve Plex username.")
|
|
}
|
|
|
|
return body_reply.Response.Data[i].UserID, username, nil
|
|
}
|
|
}
|
|
|
|
log.Println("Could not find any user that matched the given Plex Identity: '" + PlexUser + "'.")
|
|
return 0, "", errors.New("Failed to find user.")
|
|
}
|
|
|
|
func TautulliDownloadStatistics(TautulliPort int, TautulliIP string, TautulliHttps bool, TautulliRoot string, TautulliApiKey string, TautulliLength int, Libraries string, Grouping string, StartDate string) ([]models.TautulliHistoryItem, error) {
|
|
|
|
url_string, err := utilities.BuildURL(TautulliPort, TautulliIP, TautulliHttps, TautulliRoot)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return []models.TautulliHistoryItem{}, errors.New("Failed to build Tautulli connection URL.")
|
|
}
|
|
|
|
url_string = url_string + "api/v2/" + "?apikey=" + TautulliApiKey + "&cmd=get_history&order_column=date&order_dir=desc&include_activity=0" + Libraries + "&grouping=" + Grouping + "&length=" + strconv.Itoa(TautulliLength) + "&start_date=" + StartDate
|
|
|
|
params := url.Values{}
|
|
payload := strings.NewReader(params.Encode())
|
|
|
|
req, err := http.NewRequest("GET", url_string, payload)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return []models.TautulliHistoryItem{}, errors.New("Failed to reach Tautulli server.")
|
|
}
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return []models.TautulliHistoryItem{}, errors.New("Failed to reach Tautulli server.")
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
var body_reply models.TautulliGetHistoryReply
|
|
json.Unmarshal(body, &body_reply)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return []models.TautulliHistoryItem{}, errors.New("Failed to parse Tautulli response.")
|
|
}
|
|
|
|
return body_reply.Response.Data.Data, nil
|
|
|
|
}
|