mirror of
https://github.com/hrfee/jfa-go.git
synced 2025-01-03 15:00:12 +00:00
fix most incompatibilites, start separating api clients
This commit is contained in:
parent
4aae655180
commit
5d56ed5378
6
api.go
6
api.go
@ -848,6 +848,11 @@ func parseDT(date string) time.Time {
|
||||
if err == nil {
|
||||
return dt
|
||||
}
|
||||
// emby method
|
||||
dt, err = time.Parse("2006-01-02T15:04:05.0000000+00:00", date)
|
||||
if err == nil {
|
||||
return dt
|
||||
}
|
||||
// magic method
|
||||
// some stored dates from jellyfin have no timezone at the end, if not we assume UTC
|
||||
if date[len(date)-1] != 'Z' {
|
||||
@ -882,6 +887,7 @@ func (app *appContext) GetUsers(gc *gin.Context) {
|
||||
var user respUser
|
||||
user.LastActive = "n/a"
|
||||
if jfUser["LastActivityDate"] != nil {
|
||||
fmt.Println(jfUser["LastActivityDate"].(string))
|
||||
date := parseDT(jfUser["LastActivityDate"].(string))
|
||||
user.LastActive = app.formatDatetime(date)
|
||||
// fmt.Printf("%s: %s, %s, %+v\n", jfUser["Name"].(string), jfUser["LastActivityDate"].(string), user.LastActive, date)
|
||||
|
6
go.mod
6
go.mod
@ -4,7 +4,7 @@ go 1.14
|
||||
|
||||
replace github.com/hrfee/jfa-go/docs => ./docs
|
||||
|
||||
replace github.com/hrfee/jfa-go/jfapi => ./jfapi
|
||||
replace github.com/hrfee/jfa-go/mediabrowser => ./mediabrowser
|
||||
|
||||
replace github.com/hrfee/jfa-go/common => ./common
|
||||
|
||||
@ -24,9 +24,9 @@ require (
|
||||
github.com/gofrs/uuid v3.3.0+incompatible // indirect
|
||||
github.com/golang/protobuf v1.4.3
|
||||
github.com/google/uuid v1.1.2 // indirect
|
||||
github.com/hrfee/jfa-go/common v0.0.0-20201112212552-b6f3cd7c1f71
|
||||
github.com/hrfee/jfa-go/common v0.0.0-20210105184019-fdc97b4e86cc
|
||||
github.com/hrfee/jfa-go/docs v0.0.0-20201112212552-b6f3cd7c1f71
|
||||
github.com/hrfee/jfa-go/jfapi v0.0.0-20201112212552-b6f3cd7c1f71
|
||||
github.com/hrfee/jfa-go/mediabrowser v0.0.0-20201112212552-b6f3cd7c1f71
|
||||
github.com/hrfee/jfa-go/ombi v0.0.0-20201112212552-b6f3cd7c1f71
|
||||
github.com/jordan-wright/email v4.0.1-0.20200917010138-e1c00e156980+incompatible
|
||||
github.com/json-iterator/go v1.1.10 // indirect
|
||||
|
@ -1,7 +0,0 @@
|
||||
module github.com/hrfee/jfa-go/jfapi
|
||||
|
||||
go 1.15
|
||||
|
||||
replace github.com/hrfee/jfa-go/common => ../common
|
||||
|
||||
require github.com/hrfee/jfa-go/common v0.0.0-00010101000000-000000000000
|
59
main.go
59
main.go
@ -26,6 +26,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/hrfee/jfa-go/common"
|
||||
_ "github.com/hrfee/jfa-go/docs"
|
||||
"github.com/hrfee/jfa-go/emby"
|
||||
"github.com/hrfee/jfa-go/jfapi"
|
||||
"github.com/hrfee/jfa-go/ombi"
|
||||
"github.com/lithammer/shortuuid/v3"
|
||||
@ -45,18 +46,19 @@ type User struct {
|
||||
// contains everything the application needs, essentially. Wouldn't do this in the future.
|
||||
type appContext struct {
|
||||
// defaults *Config
|
||||
config *ini.File
|
||||
configPath string
|
||||
configBasePath string
|
||||
configBase settings
|
||||
dataPath string
|
||||
localPath string
|
||||
cssClass string
|
||||
jellyfinLogin bool
|
||||
users []User
|
||||
invalidTokens []string
|
||||
jf *jfapi.Jellyfin
|
||||
authJf *jfapi.Jellyfin
|
||||
config *ini.File
|
||||
configPath string
|
||||
configBasePath string
|
||||
configBase settings
|
||||
dataPath string
|
||||
localPath string
|
||||
cssClass string
|
||||
jellyfinLogin bool
|
||||
users []User
|
||||
invalidTokens []string
|
||||
// Keeping jf name because I can't think of a better one
|
||||
jf common.MediaBrowserStruct
|
||||
authJf common.MediaBrowserStruct
|
||||
ombi *ombi.Ombi
|
||||
datePattern string
|
||||
timePattern string
|
||||
@ -439,15 +441,30 @@ func start(asDaemon, firstCall bool) {
|
||||
|
||||
server := app.config.Section("jellyfin").Key("server").String()
|
||||
cacheTimeout := int(app.config.Section("jellyfin").Key("cache_timeout").MustUint(30))
|
||||
app.jf, _ = jfapi.NewJellyfin(
|
||||
server,
|
||||
app.config.Section("jellyfin").Key("client").String(),
|
||||
app.config.Section("jellyfin").Key("version").String(),
|
||||
app.config.Section("jellyfin").Key("device").String(),
|
||||
app.config.Section("jellyfin").Key("device_id").String(),
|
||||
common.NewTimeoutHandler("Jellyfin", server, true),
|
||||
cacheTimeout,
|
||||
)
|
||||
mediaBrowser := app.config.Section("jellyfin").Key("type").String()
|
||||
if mediaBrowser == "emby" {
|
||||
app.info.Println("Using Emby server type")
|
||||
app.jf, _ = emby.NewEmby(
|
||||
server,
|
||||
app.config.Section("jellyfin").Key("client").String(),
|
||||
app.config.Section("jellyfin").Key("version").String(),
|
||||
app.config.Section("jellyfin").Key("device").String(),
|
||||
app.config.Section("jellyfin").Key("device_id").String(),
|
||||
common.NewTimeoutHandler("Emby", server, true),
|
||||
cacheTimeout,
|
||||
)
|
||||
} else {
|
||||
app.info.Println("Using Jellyfin server type")
|
||||
app.jf, _ = jfapi.NewJellyfin(
|
||||
server,
|
||||
app.config.Section("jellyfin").Key("client").String(),
|
||||
app.config.Section("jellyfin").Key("version").String(),
|
||||
app.config.Section("jellyfin").Key("device").String(),
|
||||
app.config.Section("jellyfin").Key("device_id").String(),
|
||||
common.NewTimeoutHandler("Jellyfin", server, true),
|
||||
cacheTimeout,
|
||||
)
|
||||
}
|
||||
var status int
|
||||
_, status, err = app.jf.Authenticate(app.config.Section("jellyfin").Key("username").String(), app.config.Section("jellyfin").Key("password").String())
|
||||
if status != 200 || err != nil {
|
||||
|
351
mediabrowser/emby.go
Normal file
351
mediabrowser/emby.go
Normal file
@ -0,0 +1,351 @@
|
||||
package mediabrowser
|
||||
|
||||
// Almost identical to jfapi, with the most notable change being the password workaround.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hrfee/jfa-go/common"
|
||||
)
|
||||
|
||||
// NewEmby returns a new Emby object.
|
||||
func NewEmby(server, client, version, device, deviceID string, timeoutHandler common.TimeoutHandler, cacheTimeout int) (*MediaBrowserStruct, error) {
|
||||
emby := &Emby{}
|
||||
emby.Server = server
|
||||
emby.client = client
|
||||
emby.version = version
|
||||
emby.device = device
|
||||
emby.deviceID = deviceID
|
||||
emby.useragent = fmt.Sprintf("%s/%s", client, version)
|
||||
emby.timeoutHandler = timeoutHandler
|
||||
emby.auth = fmt.Sprintf("MediaBrowser Client=\"%s\", Device=\"%s\", DeviceId=\"%s\", Version=\"%s\"", client, device, deviceID, version)
|
||||
emby.header = map[string]string{
|
||||
"Accept": "application/json",
|
||||
"Content-type": "application/json; charset=UTF-8",
|
||||
"X-Application": emby.useragent,
|
||||
"Accept-Charset": "UTF-8,*",
|
||||
"Accept-Encoding": "gzip",
|
||||
"User-Agent": emby.useragent,
|
||||
"X-Emby-Authorization": emby.auth,
|
||||
}
|
||||
emby.httpClient = &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
infoURL := fmt.Sprintf("%s/System/Info/Public", server)
|
||||
req, _ := http.NewRequest("GET", infoURL, nil)
|
||||
resp, err := emby.httpClient.Do(req)
|
||||
defer emby.timeoutHandler()
|
||||
if err == nil {
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
json.Unmarshal(data, &emby.ServerInfo)
|
||||
}
|
||||
emby.cacheLength = cacheTimeout
|
||||
emby.CacheExpiry = time.Now()
|
||||
return emby, nil
|
||||
}
|
||||
|
||||
// Authenticate attempts to authenticate using a username & password
|
||||
func (emby *MediaBrowserStruct) Authenticate(username, password string) (map[string]interface{}, int, error) {
|
||||
emby.Username = username
|
||||
emby.password = password
|
||||
emby.loginParams = map[string]string{
|
||||
"Username": username,
|
||||
"Pw": password,
|
||||
"Password": password,
|
||||
}
|
||||
buffer := &bytes.Buffer{}
|
||||
encoder := json.NewEncoder(buffer)
|
||||
encoder.SetEscapeHTML(false)
|
||||
err := encoder.Encode(emby.loginParams)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
// loginParams, _ := json.Marshal(jf.loginParams)
|
||||
url := fmt.Sprintf("%s/Users/authenticatebyname", emby.Server)
|
||||
req, err := http.NewRequest("POST", url, buffer)
|
||||
defer emby.timeoutHandler()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
for name, value := range emby.header {
|
||||
req.Header.Add(name, value)
|
||||
}
|
||||
resp, err := emby.httpClient.Do(req)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
return nil, resp.StatusCode, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var data io.Reader
|
||||
switch resp.Header.Get("Content-Encoding") {
|
||||
case "gzip":
|
||||
data, _ = gzip.NewReader(resp.Body)
|
||||
default:
|
||||
data = resp.Body
|
||||
}
|
||||
var respData map[string]interface{}
|
||||
json.NewDecoder(data).Decode(&respData)
|
||||
emby.AccessToken = respData["AccessToken"].(string)
|
||||
user := respData["User"].(map[string]interface{})
|
||||
emby.userID = respData["User"].(map[string]interface{})["Id"].(string)
|
||||
emby.auth = fmt.Sprintf("MediaBrowser Client=\"%s\", Device=\"%s\", DeviceId=\"%s\", Version=\"%s\", Token=\"%s\"", emby.client, emby.device, emby.deviceID, emby.version, emby.AccessToken)
|
||||
emby.header["X-Emby-Authorization"] = emby.auth
|
||||
emby.Authenticated = true
|
||||
return user, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
func (emby *MediaBrowserStruct) get(url string, params map[string]string) (string, int, error) {
|
||||
var req *http.Request
|
||||
if params != nil {
|
||||
jsonParams, _ := json.Marshal(params)
|
||||
req, _ = http.NewRequest("GET", url, bytes.NewBuffer(jsonParams))
|
||||
} else {
|
||||
req, _ = http.NewRequest("GET", url, nil)
|
||||
}
|
||||
for name, value := range emby.header {
|
||||
req.Header.Add(name, value)
|
||||
}
|
||||
resp, err := emby.httpClient.Do(req)
|
||||
defer emby.timeoutHandler()
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
if resp.StatusCode == 401 && emby.Authenticated {
|
||||
emby.Authenticated = false
|
||||
_, _, authErr := emby.Authenticate(emby.Username, emby.password)
|
||||
if authErr == nil {
|
||||
v1, v2, v3 := emby.get(url, params)
|
||||
return v1, v2, v3
|
||||
}
|
||||
}
|
||||
return "", resp.StatusCode, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var data io.Reader
|
||||
encoding := resp.Header.Get("Content-Encoding")
|
||||
switch encoding {
|
||||
case "gzip":
|
||||
data, _ = gzip.NewReader(resp.Body)
|
||||
default:
|
||||
data = resp.Body
|
||||
}
|
||||
buf := new(strings.Builder)
|
||||
io.Copy(buf, data)
|
||||
//var respData map[string]interface{}
|
||||
//json.NewDecoder(data).Decode(&respData)
|
||||
return buf.String(), resp.StatusCode, nil
|
||||
}
|
||||
|
||||
func (emby *MediaBrowserStruct) post(url string, data map[string]interface{}, response bool) (string, int, error) {
|
||||
params, _ := json.Marshal(data)
|
||||
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(params))
|
||||
for name, value := range emby.header {
|
||||
req.Header.Add(name, value)
|
||||
}
|
||||
resp, err := emby.httpClient.Do(req)
|
||||
defer emby.timeoutHandler()
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
if resp.StatusCode == 401 && emby.Authenticated {
|
||||
emby.Authenticated = false
|
||||
_, _, authErr := emby.Authenticate(emby.Username, emby.password)
|
||||
if authErr == nil {
|
||||
v1, v2, v3 := emby.post(url, data, response)
|
||||
return v1, v2, v3
|
||||
}
|
||||
}
|
||||
return "", resp.StatusCode, err
|
||||
}
|
||||
if response {
|
||||
defer resp.Body.Close()
|
||||
var outData io.Reader
|
||||
switch resp.Header.Get("Content-Encoding") {
|
||||
case "gzip":
|
||||
outData, _ = gzip.NewReader(resp.Body)
|
||||
default:
|
||||
outData = resp.Body
|
||||
}
|
||||
buf := new(strings.Builder)
|
||||
io.Copy(buf, outData)
|
||||
return buf.String(), resp.StatusCode, nil
|
||||
}
|
||||
return "", resp.StatusCode, nil
|
||||
}
|
||||
|
||||
// DeleteUser deletes the user corresponding to the provided ID.
|
||||
func (emby *MediaBrowserStruct) DeleteUser(userID string) (int, error) {
|
||||
url := fmt.Sprintf("%s/Users/%s", emby.Server, userID)
|
||||
req, _ := http.NewRequest("DELETE", url, nil)
|
||||
for name, value := range emby.header {
|
||||
req.Header.Add(name, value)
|
||||
}
|
||||
resp, err := emby.httpClient.Do(req)
|
||||
defer emby.timeoutHandler()
|
||||
return resp.StatusCode, err
|
||||
}
|
||||
|
||||
// GetUsers returns all (visible) users on the Emby instance.
|
||||
func (emby *MediaBrowserStruct) GetUsers(public bool) ([]map[string]interface{}, int, error) {
|
||||
var result []map[string]interface{}
|
||||
var data string
|
||||
var status int
|
||||
var err error
|
||||
if time.Now().After(emby.CacheExpiry) {
|
||||
if public {
|
||||
url := fmt.Sprintf("%s/users/public", emby.Server)
|
||||
data, status, err = emby.get(url, nil)
|
||||
} else {
|
||||
url := fmt.Sprintf("%s/users", emby.Server)
|
||||
data, status, err = emby.get(url, emby.loginParams)
|
||||
}
|
||||
if err != nil || status != 200 {
|
||||
return nil, status, err
|
||||
}
|
||||
json.Unmarshal([]byte(data), &result)
|
||||
emby.userCache = result
|
||||
emby.CacheExpiry = time.Now().Add(time.Minute * time.Duration(emby.cacheLength))
|
||||
if id, ok := result[0]["Id"]; ok {
|
||||
if id.(string)[8] == '-' {
|
||||
emby.Hyphens = true
|
||||
}
|
||||
}
|
||||
return result, status, nil
|
||||
}
|
||||
return emby.userCache, 200, nil
|
||||
}
|
||||
|
||||
// UserByName returns the user corresponding to the provided username.
|
||||
func (emby *MediaBrowserStruct) UserByName(username string, public bool) (map[string]interface{}, int, error) {
|
||||
var match map[string]interface{}
|
||||
find := func() (map[string]interface{}, int, error) {
|
||||
users, status, err := emby.GetUsers(public)
|
||||
if err != nil || status != 200 {
|
||||
return nil, status, err
|
||||
}
|
||||
for _, user := range users {
|
||||
if user["Name"].(string) == username {
|
||||
return user, status, err
|
||||
}
|
||||
}
|
||||
return nil, status, err
|
||||
}
|
||||
match, status, err := find()
|
||||
if match == nil {
|
||||
emby.CacheExpiry = time.Now()
|
||||
match, status, err = find()
|
||||
}
|
||||
return match, status, err
|
||||
}
|
||||
|
||||
// UserByID returns the user corresponding to the provided ID.
|
||||
func (emby *MediaBrowserStruct) UserByID(userID string, public bool) (map[string]interface{}, int, error) {
|
||||
if emby.CacheExpiry.After(time.Now()) {
|
||||
for _, user := range emby.userCache {
|
||||
if user["Id"].(string) == userID {
|
||||
return user, 200, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if public {
|
||||
users, status, err := emby.GetUsers(public)
|
||||
if err != nil || status != 200 {
|
||||
return nil, status, err
|
||||
}
|
||||
for _, user := range users {
|
||||
if user["Id"].(string) == userID {
|
||||
return user, status, nil
|
||||
}
|
||||
}
|
||||
return nil, status, err
|
||||
}
|
||||
var result map[string]interface{}
|
||||
var data string
|
||||
var status int
|
||||
var err error
|
||||
url := fmt.Sprintf("%s/users/%s", emby.Server, userID)
|
||||
data, status, err = emby.get(url, emby.loginParams)
|
||||
if err != nil || status != 200 {
|
||||
return nil, status, err
|
||||
}
|
||||
json.Unmarshal([]byte(data), &result)
|
||||
return result, status, nil
|
||||
}
|
||||
|
||||
// NewUser creates a new user with the provided username and password.
|
||||
// Since emby doesn't allow one to specify a password on user creation, we:
|
||||
// Create the account
|
||||
// Immediately disable it
|
||||
// Set password
|
||||
// Reeenable it
|
||||
func (emby *MediaBrowserStruct) NewUser(username, password string) (map[string]interface{}, int, error) {
|
||||
url := fmt.Sprintf("%s/Users/New", emby.Server)
|
||||
data := map[string]interface{}{
|
||||
"Name": username,
|
||||
}
|
||||
response, status, err := emby.post(url, data, true)
|
||||
var recv map[string]interface{}
|
||||
json.Unmarshal([]byte(response), &recv)
|
||||
if err != nil || !(status == 200 || status == 204) {
|
||||
return nil, status, err
|
||||
}
|
||||
// Step 2: Set password
|
||||
id := recv["Id"].(string)
|
||||
url = fmt.Sprintf("/Users/%s/Password", id)
|
||||
data = map[string]interface{}{
|
||||
"Id": id,
|
||||
"CurrentPw": "",
|
||||
"NewPw": password,
|
||||
}
|
||||
_, status, err = emby.post(url, data, false)
|
||||
// Step 3: If setting password errored, try to delete the account
|
||||
if err != nil || !(status == 200 || status == 204) {
|
||||
status, err = emby.DeleteUser(id)
|
||||
}
|
||||
return recv, status, nil
|
||||
}
|
||||
|
||||
// SetPolicy sets the access policy for the user corresponding to the provided ID.
|
||||
func (emby *MediaBrowserStruct) SetPolicy(userID string, policy map[string]interface{}) (int, error) {
|
||||
url := fmt.Sprintf("%s/Users/%s/Policy", emby.Server, userID)
|
||||
_, status, err := emby.post(url, policy, false)
|
||||
if err != nil || status != 200 {
|
||||
return status, err
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
// SetConfiguration sets the configuration (part of homescreen layout) for the user corresponding to the provided ID.
|
||||
func (emby *MediaBrowserStruct) SetConfiguration(userID string, configuration map[string]interface{}) (int, error) {
|
||||
url := fmt.Sprintf("%s/Users/%s/Configuration", emby.Server, userID)
|
||||
_, status, err := emby.post(url, configuration, false)
|
||||
return status, err
|
||||
}
|
||||
|
||||
// GetDisplayPreferences gets the displayPreferences (part of homescreen layout) for the user corresponding to the provided ID.
|
||||
func (emby *MediaBrowserStruct) GetDisplayPreferences(userID string) (map[string]interface{}, int, error) {
|
||||
url := fmt.Sprintf("%s/DisplayPreferences/usersettings?userId=%s&client=emby", emby.Server, userID)
|
||||
data, status, err := emby.get(url, nil)
|
||||
if err != nil || !(status == 204 || status == 200) {
|
||||
return nil, status, err
|
||||
}
|
||||
var displayprefs map[string]interface{}
|
||||
err = json.Unmarshal([]byte(data), &displayprefs)
|
||||
if err != nil {
|
||||
return nil, status, err
|
||||
}
|
||||
return displayprefs, status, nil
|
||||
}
|
||||
|
||||
// SetDisplayPreferences sets the displayPreferences (part of homescreen layout) for the user corresponding to the provided ID.
|
||||
func (emby *MediaBrowserStruct) SetDisplayPreferences(userID string, displayprefs map[string]interface{}) (int, error) {
|
||||
url := fmt.Sprintf("%s/DisplayPreferences/usersettings?userId=%s&client=emby", emby.Server, userID)
|
||||
_, status, err := emby.post(url, displayprefs, false)
|
||||
if err != nil || !(status == 204 || status == 200) {
|
||||
return status, err
|
||||
}
|
||||
return status, nil
|
||||
}
|
7
mediabrowser/go.mod
Normal file
7
mediabrowser/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/hrfee/jfa-go/mediabrowser
|
||||
|
||||
go 1.15
|
||||
|
||||
replace github.com/hrfee/jfa-go/common => ../common
|
||||
|
||||
require github.com/hrfee/jfa-go/common v0.0.0-20210105184019-fdc97b4e86cc
|
@ -1,4 +1,4 @@
|
||||
package jfapi
|
||||
package mediabrowser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -14,43 +14,9 @@ import (
|
||||
"github.com/hrfee/jfa-go/common"
|
||||
)
|
||||
|
||||
type serverInfo struct {
|
||||
LocalAddress string `json:"LocalAddress"`
|
||||
Name string `json:"ServerName"`
|
||||
Version string `json:"Version"`
|
||||
OS string `json:"OperatingSystem"`
|
||||
ID string `json:"Id"`
|
||||
}
|
||||
|
||||
// Jellyfin represents a running Jellyfin instance.
|
||||
type Jellyfin struct {
|
||||
Server string
|
||||
client string
|
||||
version string
|
||||
device string
|
||||
deviceID string
|
||||
useragent string
|
||||
auth string
|
||||
header map[string]string
|
||||
ServerInfo serverInfo
|
||||
Username string
|
||||
password string
|
||||
Authenticated bool
|
||||
AccessToken string
|
||||
userID string
|
||||
httpClient *http.Client
|
||||
loginParams map[string]string
|
||||
userCache []map[string]interface{}
|
||||
CacheExpiry time.Time
|
||||
cacheLength int
|
||||
noFail bool
|
||||
Hyphens bool
|
||||
timeoutHandler common.TimeoutHandler
|
||||
}
|
||||
|
||||
// NewJellyfin returns a new Jellyfin object.
|
||||
func NewJellyfin(server, client, version, device, deviceID string, timeoutHandler common.TimeoutHandler, cacheTimeout int) (*Jellyfin, error) {
|
||||
jf := &Jellyfin{}
|
||||
func NewJellyfin(server, client, version, device, deviceID string, timeoutHandler common.TimeoutHandler, cacheTimeout int) (*MediaBrowserStruct, error) {
|
||||
jf := &MediaBrowserStruct{}
|
||||
jf.Server = server
|
||||
jf.client = client
|
||||
jf.version = version
|
||||
@ -85,7 +51,7 @@ func NewJellyfin(server, client, version, device, deviceID string, timeoutHandle
|
||||
}
|
||||
|
||||
// Authenticate attempts to authenticate using a username & password
|
||||
func (jf *Jellyfin) Authenticate(username, password string) (map[string]interface{}, int, error) {
|
||||
func (jf *MediaBrowserStruct) Authenticate(username, password string) (map[string]interface{}, int, error) {
|
||||
jf.Username = username
|
||||
jf.password = password
|
||||
jf.loginParams = map[string]string{
|
||||
@ -133,7 +99,7 @@ func (jf *Jellyfin) Authenticate(username, password string) (map[string]interfac
|
||||
return user, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
func (jf *Jellyfin) get(url string, params map[string]string) (string, int, error) {
|
||||
func (jf *MediaBrowserStruct) get(url string, params map[string]string) (string, int, error) {
|
||||
var req *http.Request
|
||||
if params != nil {
|
||||
jsonParams, _ := json.Marshal(params)
|
||||
@ -173,7 +139,7 @@ func (jf *Jellyfin) get(url string, params map[string]string) (string, int, erro
|
||||
return buf.String(), resp.StatusCode, nil
|
||||
}
|
||||
|
||||
func (jf *Jellyfin) post(url string, data map[string]interface{}, response bool) (string, int, error) {
|
||||
func (jf *MediaBrowserStruct) post(url string, data map[string]interface{}, response bool) (string, int, error) {
|
||||
params, _ := json.Marshal(data)
|
||||
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(params))
|
||||
for name, value := range jf.header {
|
||||
@ -209,7 +175,7 @@ func (jf *Jellyfin) post(url string, data map[string]interface{}, response bool)
|
||||
}
|
||||
|
||||
// DeleteUser deletes the user corresponding to the provided ID.
|
||||
func (jf *Jellyfin) DeleteUser(userID string) (int, error) {
|
||||
func (jf *MediaBrowserStruct) DeleteUser(userID string) (int, error) {
|
||||
url := fmt.Sprintf("%s/Users/%s", jf.Server, userID)
|
||||
req, _ := http.NewRequest("DELETE", url, nil)
|
||||
for name, value := range jf.header {
|
||||
@ -221,7 +187,7 @@ func (jf *Jellyfin) DeleteUser(userID string) (int, error) {
|
||||
}
|
||||
|
||||
// GetUsers returns all (visible) users on the Jellyfin instance.
|
||||
func (jf *Jellyfin) GetUsers(public bool) ([]map[string]interface{}, int, error) {
|
||||
func (jf *MediaBrowserStruct) GetUsers(public bool) ([]map[string]interface{}, int, error) {
|
||||
var result []map[string]interface{}
|
||||
var data string
|
||||
var status int
|
||||
@ -251,7 +217,7 @@ func (jf *Jellyfin) GetUsers(public bool) ([]map[string]interface{}, int, error)
|
||||
}
|
||||
|
||||
// UserByName returns the user corresponding to the provided username.
|
||||
func (jf *Jellyfin) UserByName(username string, public bool) (map[string]interface{}, int, error) {
|
||||
func (jf *MediaBrowserStruct) UserByName(username string, public bool) (map[string]interface{}, int, error) {
|
||||
var match map[string]interface{}
|
||||
find := func() (map[string]interface{}, int, error) {
|
||||
users, status, err := jf.GetUsers(public)
|
||||
@ -274,7 +240,7 @@ func (jf *Jellyfin) UserByName(username string, public bool) (map[string]interfa
|
||||
}
|
||||
|
||||
// UserByID returns the user corresponding to the provided ID.
|
||||
func (jf *Jellyfin) UserByID(userID string, public bool) (map[string]interface{}, int, error) {
|
||||
func (jf *MediaBrowserStruct) UserByID(userID string, public bool) (map[string]interface{}, int, error) {
|
||||
if jf.CacheExpiry.After(time.Now()) {
|
||||
for _, user := range jf.userCache {
|
||||
if user["Id"].(string) == userID {
|
||||
@ -308,7 +274,7 @@ func (jf *Jellyfin) UserByID(userID string, public bool) (map[string]interface{}
|
||||
}
|
||||
|
||||
// NewUser creates a new user with the provided username and password.
|
||||
func (jf *Jellyfin) NewUser(username, password string) (map[string]interface{}, int, error) {
|
||||
func (jf *MediaBrowserStruct) NewUser(username, password string) (map[string]interface{}, int, error) {
|
||||
url := fmt.Sprintf("%s/Users/New", jf.Server)
|
||||
stringData := map[string]string{
|
||||
"Name": username,
|
||||
@ -328,7 +294,7 @@ func (jf *Jellyfin) NewUser(username, password string) (map[string]interface{},
|
||||
}
|
||||
|
||||
// SetPolicy sets the access policy for the user corresponding to the provided ID.
|
||||
func (jf *Jellyfin) SetPolicy(userID string, policy map[string]interface{}) (int, error) {
|
||||
func (jf *MediaBrowserStruct) SetPolicy(userID string, policy map[string]interface{}) (int, error) {
|
||||
url := fmt.Sprintf("%s/Users/%s/Policy", jf.Server, userID)
|
||||
_, status, err := jf.post(url, policy, false)
|
||||
if err != nil || status != 200 {
|
||||
@ -338,14 +304,14 @@ func (jf *Jellyfin) SetPolicy(userID string, policy map[string]interface{}) (int
|
||||
}
|
||||
|
||||
// SetConfiguration sets the configuration (part of homescreen layout) for the user corresponding to the provided ID.
|
||||
func (jf *Jellyfin) SetConfiguration(userID string, configuration map[string]interface{}) (int, error) {
|
||||
func (jf *MediaBrowserStruct) SetConfiguration(userID string, configuration map[string]interface{}) (int, error) {
|
||||
url := fmt.Sprintf("%s/Users/%s/Configuration", jf.Server, userID)
|
||||
_, status, err := jf.post(url, configuration, false)
|
||||
return status, err
|
||||
}
|
||||
|
||||
// GetDisplayPreferences gets the displayPreferences (part of homescreen layout) for the user corresponding to the provided ID.
|
||||
func (jf *Jellyfin) GetDisplayPreferences(userID string) (map[string]interface{}, int, error) {
|
||||
func (jf *MediaBrowserStruct) GetDisplayPreferences(userID string) (map[string]interface{}, int, error) {
|
||||
url := fmt.Sprintf("%s/DisplayPreferences/usersettings?userId=%s&client=emby", jf.Server, userID)
|
||||
data, status, err := jf.get(url, nil)
|
||||
if err != nil || !(status == 204 || status == 200) {
|
||||
@ -360,7 +326,7 @@ func (jf *Jellyfin) GetDisplayPreferences(userID string) (map[string]interface{}
|
||||
}
|
||||
|
||||
// SetDisplayPreferences sets the displayPreferences (part of homescreen layout) for the user corresponding to the provided ID.
|
||||
func (jf *Jellyfin) SetDisplayPreferences(userID string, displayprefs map[string]interface{}) (int, error) {
|
||||
func (jf *MediaBrowserStruct) SetDisplayPreferences(userID string, displayprefs map[string]interface{}) (int, error) {
|
||||
url := fmt.Sprintf("%s/DisplayPreferences/usersettings?userId=%s&client=emby", jf.Server, userID)
|
||||
_, status, err := jf.post(url, displayprefs, false)
|
||||
if err != nil || !(status == 204 || status == 200) {
|
53
mediabrowser/mediabrowser.go
Normal file
53
mediabrowser/mediabrowser.go
Normal file
@ -0,0 +1,53 @@
|
||||
package mediabrowser
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type serverInfo struct {
|
||||
LocalAddress string `json:"LocalAddress"`
|
||||
Name string `json:"ServerName"`
|
||||
Version string `json:"Version"`
|
||||
OS string `json:"OperatingSystem"`
|
||||
ID string `json:"Id"`
|
||||
}
|
||||
|
||||
type MediaBrowserStruct struct {
|
||||
Server string
|
||||
client string
|
||||
version string
|
||||
device string
|
||||
deviceID string
|
||||
useragent string
|
||||
auth string
|
||||
header map[string]string
|
||||
ServerInfo serverInfo
|
||||
Username string
|
||||
password string
|
||||
Authenticated bool
|
||||
AccessToken string
|
||||
userID string
|
||||
httpClient *http.Client
|
||||
loginParams map[string]string
|
||||
userCache []map[string]interface{}
|
||||
CacheExpiry time.Time
|
||||
cacheLength int
|
||||
noFail bool
|
||||
Hyphens bool
|
||||
timeoutHandler TimeoutHandler
|
||||
}
|
||||
|
||||
// MediaBrowser is an api instance of Jellyfin/Emby.
|
||||
type MediaBrowser interface {
|
||||
Authenticate(username, password string) (map[string]interface{}, int, error)
|
||||
DeleteUser(userID string) (int, error)
|
||||
GetUsers(public bool) ([]map[string]interface{}, int, error)
|
||||
UserByName(username string, public bool) (map[string]interface{}, int, error)
|
||||
UserByID(userID string, public bool) (map[string]interface{}, int, error)
|
||||
NewUser(username, password string) (map[string]interface{}, int, error)
|
||||
SetPolicy(userID string, policy map[string]interface{}) (int, error)
|
||||
SetConfiguration(userID string, configuration map[string]interface{}) (int, error)
|
||||
GetDisplayPreferences(userID string) (map[string]interface{}, int, error)
|
||||
SetDisplayPreferences(userID string, displayprefs map[string]interface{}) (int, error)
|
||||
}
|
Loading…
Reference in New Issue
Block a user