mirror of https://github.com/hrfee/jfa-go
parent
1cf8d3037b
commit
762dac2581
@ -1,173 +0,0 @@
|
||||
package mediabrowser
|
||||
|
||||
// Almost identical to jfapi, with the most notable change being the password workaround.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func embyDeleteUser(emby *MediaBrowser, 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
|
||||
}
|
||||
|
||||
func embyGetUsers(emby *MediaBrowser, public bool) ([]User, int, error) {
|
||||
var result []User
|
||||
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 result[0].ID[8] == '-' {
|
||||
emby.Hyphens = true
|
||||
}
|
||||
return result, status, nil
|
||||
}
|
||||
return emby.userCache, 200, nil
|
||||
}
|
||||
|
||||
func embyUserByName(emby *MediaBrowser, username string, public bool) (User, int, error) {
|
||||
var match User
|
||||
find := func() (User, int, error) {
|
||||
users, status, err := emby.GetUsers(public)
|
||||
if err != nil || status != 200 {
|
||||
return User{}, status, err
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.Name == username {
|
||||
return user, status, err
|
||||
}
|
||||
}
|
||||
return User{}, status, err
|
||||
}
|
||||
match, status, err := find()
|
||||
if match.Name == "" {
|
||||
emby.CacheExpiry = time.Now()
|
||||
match, status, err = find()
|
||||
}
|
||||
return match, status, err
|
||||
}
|
||||
|
||||
func embyUserByID(emby *MediaBrowser, userID string, public bool) (User, int, error) {
|
||||
if emby.CacheExpiry.After(time.Now()) {
|
||||
for _, user := range emby.userCache {
|
||||
if user.ID == userID {
|
||||
return user, 200, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if public {
|
||||
users, status, err := emby.GetUsers(public)
|
||||
if err != nil || status != 200 {
|
||||
return User{}, status, err
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.ID == userID {
|
||||
return user, status, nil
|
||||
}
|
||||
}
|
||||
return User{}, status, err
|
||||
}
|
||||
var result User
|
||||
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 User{}, status, err
|
||||
}
|
||||
json.Unmarshal([]byte(data), &result)
|
||||
return result, status, nil
|
||||
}
|
||||
|
||||
// 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 embyNewUser(emby *MediaBrowser, username, password string) (User, 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 User
|
||||
json.Unmarshal([]byte(response), &recv)
|
||||
if err != nil || !(status == 200 || status == 204) {
|
||||
return User{}, status, err
|
||||
}
|
||||
// Step 2: Set password
|
||||
id := recv.ID
|
||||
url = fmt.Sprintf("%s/Users/%s/Password", emby.Server, 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) {
|
||||
_, err = emby.DeleteUser(id)
|
||||
}
|
||||
return recv, status, nil
|
||||
}
|
||||
|
||||
func embySetPolicy(emby *MediaBrowser, userID string, policy Policy) (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
|
||||
}
|
||||
|
||||
func embySetConfiguration(emby *MediaBrowser, userID string, configuration Configuration) (int, error) {
|
||||
url := fmt.Sprintf("%s/Users/%s/Configuration", emby.Server, userID)
|
||||
_, status, err := emby.post(url, configuration, false)
|
||||
return status, err
|
||||
}
|
||||
|
||||
func embyGetDisplayPreferences(emby *MediaBrowser, 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
|
||||
}
|
||||
|
||||
func embySetDisplayPreferences(emby *MediaBrowser, 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
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
module github.com/hrfee/jfa-go/mediabrowser
|
||||
|
||||
go 1.15
|
@ -1,162 +0,0 @@
|
||||
package mediabrowser
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func jfDeleteUser(jf *MediaBrowser, 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 {
|
||||
req.Header.Add(name, value)
|
||||
}
|
||||
resp, err := jf.httpClient.Do(req)
|
||||
defer jf.timeoutHandler()
|
||||
return resp.StatusCode, err
|
||||
}
|
||||
|
||||
func jfGetUsers(jf *MediaBrowser, public bool) ([]User, int, error) {
|
||||
var result []User
|
||||
var data string
|
||||
var status int
|
||||
var err error
|
||||
if time.Now().After(jf.CacheExpiry) {
|
||||
if public {
|
||||
url := fmt.Sprintf("%s/users/public", jf.Server)
|
||||
data, status, err = jf.get(url, nil)
|
||||
} else {
|
||||
url := fmt.Sprintf("%s/users", jf.Server)
|
||||
data, status, err = jf.get(url, jf.loginParams)
|
||||
}
|
||||
if err != nil || status != 200 {
|
||||
return nil, status, err
|
||||
}
|
||||
err := json.Unmarshal([]byte(data), &result)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, status, err
|
||||
}
|
||||
jf.userCache = result
|
||||
jf.CacheExpiry = time.Now().Add(time.Minute * time.Duration(jf.cacheLength))
|
||||
if result[0].ID[8] == '-' {
|
||||
jf.Hyphens = true
|
||||
}
|
||||
return result, status, nil
|
||||
}
|
||||
return jf.userCache, 200, nil
|
||||
}
|
||||
|
||||
func jfUserByName(jf *MediaBrowser, username string, public bool) (User, int, error) {
|
||||
var match User
|
||||
find := func() (User, int, error) {
|
||||
users, status, err := jf.GetUsers(public)
|
||||
if err != nil || status != 200 {
|
||||
return User{}, status, err
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.Name == username {
|
||||
return user, status, err
|
||||
}
|
||||
}
|
||||
return User{}, status, err
|
||||
}
|
||||
match, status, err := find()
|
||||
if match.Name == "" {
|
||||
jf.CacheExpiry = time.Now()
|
||||
match, status, err = find()
|
||||
}
|
||||
return match, status, err
|
||||
}
|
||||
|
||||
func jfUserByID(jf *MediaBrowser, userID string, public bool) (User, int, error) {
|
||||
if jf.CacheExpiry.After(time.Now()) {
|
||||
for _, user := range jf.userCache {
|
||||
if user.ID == userID {
|
||||
return user, 200, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if public {
|
||||
users, status, err := jf.GetUsers(public)
|
||||
if err != nil || status != 200 {
|
||||
return User{}, status, err
|
||||
}
|
||||
for _, user := range users {
|
||||
if user.ID == userID {
|
||||
return user, status, nil
|
||||
}
|
||||
}
|
||||
return User{}, status, err
|
||||
}
|
||||
var result User
|
||||
var data string
|
||||
var status int
|
||||
var err error
|
||||
url := fmt.Sprintf("%s/users/%s", jf.Server, userID)
|
||||
data, status, err = jf.get(url, jf.loginParams)
|
||||
if err != nil || status != 200 {
|
||||
return User{}, status, err
|
||||
}
|
||||
json.Unmarshal([]byte(data), &result)
|
||||
return result, status, nil
|
||||
}
|
||||
|
||||
func jfNewUser(jf *MediaBrowser, username, password string) (User, int, error) {
|
||||
url := fmt.Sprintf("%s/Users/New", jf.Server)
|
||||
stringData := map[string]string{
|
||||
"Name": username,
|
||||
"Password": password,
|
||||
}
|
||||
data := make(map[string]interface{})
|
||||
for key, value := range stringData {
|
||||
data[key] = value
|
||||
}
|
||||
response, status, err := jf.post(url, data, true)
|
||||
var recv User
|
||||
json.Unmarshal([]byte(response), &recv)
|
||||
if err != nil || !(status == 200 || status == 204) {
|
||||
return User{}, status, err
|
||||
}
|
||||
return recv, status, nil
|
||||
}
|
||||
|
||||
func jfSetPolicy(jf *MediaBrowser, userID string, policy Policy) (int, error) {
|
||||
url := fmt.Sprintf("%s/Users/%s/Policy", jf.Server, userID)
|
||||
_, status, err := jf.post(url, policy, false)
|
||||
if err != nil || status != 200 {
|
||||
return status, err
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func jfSetConfiguration(jf *MediaBrowser, userID string, configuration Configuration) (int, error) {
|
||||
url := fmt.Sprintf("%s/Users/%s/Configuration", jf.Server, userID)
|
||||
_, status, err := jf.post(url, configuration, false)
|
||||
return status, err
|
||||
}
|
||||
|
||||
func jfGetDisplayPreferences(jf *MediaBrowser, 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) {
|
||||
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
|
||||
}
|
||||
|
||||
func jfSetDisplayPreferences(jf *MediaBrowser, 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) {
|
||||
return status, err
|
||||
}
|
||||
return status, nil
|
||||
}
|
@ -1,322 +0,0 @@
|
||||
// Mediabrowser provides user-related bindings to the Jellyfin & Emby APIs.
|
||||
// Some data aren't bound to structs as jfa-go doesn't need to interact with them, for example DisplayPreferences.
|
||||
// See Jellyfin/Emby swagger docs for more info on them.
|
||||
package mediabrowser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimeoutHandler should recover from an http timeout or panic.
|
||||
type TimeoutHandler func()
|
||||
|
||||
// NewNamedTimeoutHandler returns a new Timeout handler that logs the error.
|
||||
// name is the name of the server to use in the log (e.g Jellyfin/Emby)
|
||||
// addr is the address of the server being accessed
|
||||
// if noFail is false, the program will exit on a timeout.
|
||||
func NewNamedTimeoutHandler(name, addr string, noFail bool) TimeoutHandler {
|
||||
return func() {
|
||||
if r := recover(); r != nil {
|
||||
out := fmt.Sprintf("Failed to authenticate with %s @ %s: Timed out", name, addr)
|
||||
if noFail {
|
||||
log.Print(out)
|
||||
} else {
|
||||
log.Fatalf(out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type serverType int
|
||||
|
||||
const (
|
||||
JellyfinServer serverType = iota
|
||||
EmbyServer
|
||||
)
|
||||
|
||||
// ServerInfo stores info about the server.
|
||||
type ServerInfo struct {
|
||||
LocalAddress string `json:"LocalAddress"`
|
||||
Name string `json:"ServerName"`
|
||||
Version string `json:"Version"`
|
||||
OS string `json:"OperatingSystem"`
|
||||
ID string `json:"Id"`
|
||||
}
|
||||
|
||||
// MediaBrowser is an api instance of Jellyfin/Emby.
|
||||
type MediaBrowser 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 []User
|
||||
CacheExpiry time.Time
|
||||
cacheLength int
|
||||
noFail bool
|
||||
Hyphens bool
|
||||
serverType serverType
|
||||
timeoutHandler TimeoutHandler
|
||||
}
|
||||
|
||||
// NewServer returns a new Mediabrowser object.
|
||||
func NewServer(st serverType, server, client, version, device, deviceID string, timeoutHandler TimeoutHandler, cacheTimeout int) (*MediaBrowser, error) {
|
||||
mb := &MediaBrowser{}
|
||||
mb.serverType = st
|
||||
mb.Server = server
|
||||
mb.client = client
|
||||
mb.version = version
|
||||
mb.device = device
|
||||
mb.deviceID = deviceID
|
||||
mb.useragent = fmt.Sprintf("%s/%s", client, version)
|
||||
mb.timeoutHandler = timeoutHandler
|
||||
mb.auth = fmt.Sprintf("MediaBrowser Client=\"%s\", Device=\"%s\", DeviceId=\"%s\", Version=\"%s\"", client, device, deviceID, version)
|
||||
mb.header = map[string]string{
|
||||
"Accept": "application/json",
|
||||
"Content-type": "application/json; charset=UTF-8",
|
||||
"X-Application": mb.useragent,
|
||||
"Accept-Charset": "UTF-8,*",
|
||||
"Accept-Encoding": "gzip",
|
||||
"User-Agent": mb.useragent,
|
||||
"X-Emby-Authorization": mb.auth,
|
||||
}
|
||||
mb.httpClient = &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
infoURL := fmt.Sprintf("%s/System/Info/Public", server)
|
||||
req, _ := http.NewRequest("GET", infoURL, nil)
|
||||
resp, err := mb.httpClient.Do(req)
|
||||
defer mb.timeoutHandler()
|
||||
if err == nil {
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
json.Unmarshal(data, &mb.ServerInfo)
|
||||
}
|
||||
mb.cacheLength = cacheTimeout
|
||||
mb.CacheExpiry = time.Now()
|
||||
return mb, nil
|
||||
}
|
||||
|
||||
func (mb *MediaBrowser) 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 mb.header {
|
||||
req.Header.Add(name, value)
|
||||
}
|
||||
resp, err := mb.httpClient.Do(req)
|
||||
defer mb.timeoutHandler()
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
if resp.StatusCode == 401 && mb.Authenticated {
|
||||
mb.Authenticated = false
|
||||
_, _, authErr := mb.Authenticate(mb.Username, mb.password)
|
||||
if authErr == nil {
|
||||
v1, v2, v3 := mb.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 (mb *MediaBrowser) post(url string, data interface{}, response bool) (string, int, error) {
|
||||
params, _ := json.Marshal(data)
|
||||
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(params))
|
||||
for name, value := range mb.header {
|
||||
req.Header.Add(name, value)
|
||||
}
|
||||
resp, err := mb.httpClient.Do(req)
|
||||
defer mb.timeoutHandler()
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
if resp.StatusCode == 401 && mb.Authenticated {
|
||||
mb.Authenticated = false
|
||||
_, _, authErr := mb.Authenticate(mb.Username, mb.password)
|
||||
if authErr == nil {
|
||||
v1, v2, v3 := mb.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
|
||||
}
|
||||
|
||||
// Authenticate attempts to authenticate using a username & password
|
||||
func (mb *MediaBrowser) Authenticate(username, password string) (User, int, error) {
|
||||
mb.Username = username
|
||||
mb.password = password
|
||||
mb.loginParams = map[string]string{
|
||||
"Username": username,
|
||||
"Pw": password,
|
||||
"Password": password,
|
||||
}
|
||||
buffer := &bytes.Buffer{}
|
||||
encoder := json.NewEncoder(buffer)
|
||||
encoder.SetEscapeHTML(false)
|
||||
err := encoder.Encode(mb.loginParams)
|
||||
if err != nil {
|
||||
return User{}, 0, err
|
||||
}
|
||||
// loginParams, _ := json.Marshal(jf.loginParams)
|
||||
url := fmt.Sprintf("%s/Users/authenticatebyname", mb.Server)
|
||||
req, err := http.NewRequest("POST", url, buffer)
|
||||
defer mb.timeoutHandler()
|
||||
if err != nil {
|
||||
return User{}, 0, err
|
||||
}
|
||||
for name, value := range mb.header {
|
||||
req.Header.Add(name, value)
|
||||
}
|
||||
resp, err := mb.httpClient.Do(req)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
return User{}, resp.StatusCode, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var d io.Reader
|
||||
switch resp.Header.Get("Content-Encoding") {
|
||||
case "gzip":
|
||||
d, _ = gzip.NewReader(resp.Body)
|
||||
default:
|
||||
d = resp.Body
|
||||
}
|
||||
data, err := io.ReadAll(d)
|
||||
if err != nil {
|
||||
return User{}, 0, err
|
||||
}
|
||||
var respData map[string]interface{}
|
||||
json.Unmarshal(data, &respData)
|
||||
mb.AccessToken = respData["AccessToken"].(string)
|
||||
var user User
|
||||
ju, err := json.Marshal(respData["User"])
|
||||
if err != nil {
|
||||
return User{}, 0, err
|
||||
}
|
||||
json.Unmarshal(ju, &user)
|
||||
mb.userID = user.ID
|
||||
mb.auth = fmt.Sprintf("MediaBrowser Client=\"%s\", Device=\"%s\", DeviceId=\"%s\", Version=\"%s\", Token=\"%s\"", mb.client, mb.device, mb.deviceID, mb.version, mb.AccessToken)
|
||||
mb.header["X-Emby-Authorization"] = mb.auth
|
||||
mb.Authenticated = true
|
||||
return user, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
// DeleteUser deletes the user corresponding to the provided ID.
|
||||
func (mb *MediaBrowser) DeleteUser(userID string) (int, error) {
|
||||
if mb.serverType == JellyfinServer {
|
||||
return jfDeleteUser(mb, userID)
|
||||
}
|
||||
return embyDeleteUser(mb, userID)
|
||||
}
|
||||
|
||||
// GetUsers returns all (visible) users on the Emby instance.
|
||||
func (mb *MediaBrowser) GetUsers(public bool) ([]User, int, error) {
|
||||
if mb.serverType == JellyfinServer {
|
||||
return jfGetUsers(mb, public)
|
||||
}
|
||||
return embyGetUsers(mb, public)
|
||||
}
|
||||
|
||||
// UserByName returns the user corresponding to the provided username.
|
||||
func (mb *MediaBrowser) UserByName(username string, public bool) (User, int, error) {
|
||||
if mb.serverType == JellyfinServer {
|
||||
return jfUserByName(mb, username, public)
|
||||
}
|
||||
return embyUserByName(mb, username, public)
|
||||
}
|
||||
|
||||
// UserByID returns the user corresponding to the provided ID.
|
||||
func (mb *MediaBrowser) UserByID(userID string, public bool) (User, int, error) {
|
||||
if mb.serverType == JellyfinServer {
|
||||
return jfUserByID(mb, userID, public)
|
||||
}
|
||||
return embyUserByID(mb, userID, public)
|
||||
}
|
||||
|
||||
// NewUser creates a new user with the provided username and password.
|
||||
func (mb *MediaBrowser) NewUser(username, password string) (User, int, error) {
|
||||
if mb.serverType == JellyfinServer {
|
||||
return jfNewUser(mb, username, password)
|
||||
}
|
||||
return embyNewUser(mb, username, password)
|
||||
}
|
||||
|
||||
// SetPolicy sets the access policy for the user corresponding to the provided ID.
|
||||
func (mb *MediaBrowser) SetPolicy(userID string, policy Policy) (int, error) {
|
||||
if mb.serverType == JellyfinServer {
|
||||
return jfSetPolicy(mb, userID, policy)
|
||||
}
|
||||
return embySetPolicy(mb, userID, policy)
|
||||
}
|
||||
|
||||
// SetConfiguration sets the configuration (part of homescreen layout) for the user corresponding to the provided ID.
|
||||
func (mb *MediaBrowser) SetConfiguration(userID string, configuration Configuration) (int, error) {
|
||||
if mb.serverType == JellyfinServer {
|
||||
return jfSetConfiguration(mb, userID, configuration)
|
||||
}
|
||||
return embySetConfiguration(mb, userID, configuration)
|
||||
}
|
||||
|
||||
// GetDisplayPreferences gets the displayPreferences (part of homescreen layout) for the user corresponding to the provided ID.
|
||||
func (mb *MediaBrowser) GetDisplayPreferences(userID string) (map[string]interface{}, int, error) {
|
||||
if mb.serverType == JellyfinServer {
|
||||
return jfGetDisplayPreferences(mb, userID)
|
||||
}
|
||||
return embyGetDisplayPreferences(mb, userID)
|
||||
}
|
||||
|
||||
// SetDisplayPreferences sets the displayPreferences (part of homescreen layout) for the user corresponding to the provided ID.
|
||||
func (mb *MediaBrowser) SetDisplayPreferences(userID string, displayprefs map[string]interface{}) (int, error) {
|
||||
if mb.serverType == JellyfinServer {
|
||||
return jfSetDisplayPreferences(mb, userID, displayprefs)
|
||||
}
|
||||
return embySetDisplayPreferences(mb, userID, displayprefs)
|
||||
}
|
@ -1,118 +0,0 @@
|
||||
package mediabrowser
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type magicParse struct {
|
||||
Parsed time.Time `json:"parseme"`
|
||||
}
|
||||
|
||||
// Time embeds time.Time with a custom JSON Unmarshal method to work with Jellyfin & Emby's time formatting.
|
||||
type Time struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t *Time) UnmarshalJSON(b []byte) (err error) {
|
||||
// Trim quotes from beginning and end, and any number of Zs (indicates UTC).
|
||||
for b[0] == '"' {
|
||||
b = b[1:]
|
||||
}
|
||||
for b[len(b)-1] == '"' || b[len(b)-1] == 'Z' {
|
||||
b = b[:len(b)-1]
|
||||
}
|
||||
// Trim nanoseconds and anything after, we don't care
|
||||
i := len(b) - 1
|
||||
for b[i] != '.' && i > 0 {
|
||||
i--
|
||||
}
|
||||
if i != 0 {
|
||||
b = b[:i]
|
||||
}
|
||||
t.Time, err = time.Parse("2006-01-02T15:04:05", string(b))
|
||||
return
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Name string `json:"Name"`
|
||||
ServerID string `json:"ServerId"`
|
||||
ID string `json:"Id"`
|
||||
HasPassword bool `json:"HasPassword"`
|
||||
HasConfiguredPassword bool `json:"HasConfiguredPassword"`
|
||||
HasConfiguredEasyPassword bool `json:"HasConfiguredEasyPassword"`
|
||||
EnableAutoLogin bool `json:"EnableAutoLogin"`
|
||||
LastLoginDate Time `json:"LastLoginDate"`
|
||||
LastActivityDate Time `json:"LastActivityDate"`
|
||||
Configuration Configuration `json:"Configuration"`
|
||||
// Policy stores the user's permissions.
|
||||
Policy Policy `json:"Policy"`
|
||||
}
|
||||
|
||||
type SessionInfo struct {
|
||||
RemoteEndpoint string `json:"RemoteEndPoint"`
|
||||
UserID string `json:"UserId"`
|
||||
}
|
||||
|
||||
type AuthenticationResult struct {
|
||||
User User `json:"User"`
|
||||
AccessToken string `json:"AccessToken"`
|
||||
ServerID string `json:"ServerId"`
|
||||
SessionInfo SessionInfo `json:"SessionInfo"`
|
||||
}
|
||||
|
||||
type Configuration struct {
|
||||
PlayDefaultAudioTrack bool `json:"PlayDefaultAudioTrack"`
|
||||
SubtitleLanguagePreference string `json:"SubtitleLanguagePreference"`
|
||||
DisplayMissingEpisodes bool `json:"DisplayMissingEpisodes"`
|
||||
GroupedFolders []interface{} `json:"GroupedFolders"`
|
||||
SubtitleMode string `json:"SubtitleMode"`
|
||||
DisplayCollectionsView bool `json:"DisplayCollectionsView"`
|
||||
EnableLocalPassword bool `json:"EnableLocalPassword"`
|
||||
OrderedViews []interface{} `json:"OrderedViews"`
|
||||
LatestItemsExcludes []interface{} `json:"LatestItemsExcludes"`
|
||||
MyMediaExcludes []interface{} `json:"MyMediaExcludes"`
|
||||
HidePlayedInLatest bool `json:"HidePlayedInLatest"`
|
||||
RememberAudioSelections bool `json:"RememberAudioSelections"`
|
||||
RememberSubtitleSelections bool `json:"RememberSubtitleSelections"`
|
||||
EnableNextEpisodeAutoPlay bool `json:"EnableNextEpisodeAutoPlay"`
|
||||
}
|
||||
type Policy struct {
|
||||
IsAdministrator bool `json:"IsAdministrator"`
|
||||
IsHidden bool `json:"IsHidden"`
|
||||
IsDisabled bool `json:"IsDisabled"`
|
||||
BlockedTags []interface{} `json:"BlockedTags"`
|
||||
EnableUserPreferenceAccess bool `json:"EnableUserPreferenceAccess"`
|
||||
AccessSchedules []interface{} `json:"AccessSchedules"`
|
||||
BlockUnratedItems []interface{} `json:"BlockUnratedItems"`
|
||||
EnableRemoteControlOfOtherUsers bool `json:"EnableRemoteControlOfOtherUsers"`
|
||||
EnableSharedDeviceControl bool `json:"EnableSharedDeviceControl"`
|
||||
EnableRemoteAccess bool `json:"EnableRemoteAccess"`
|
||||
EnableLiveTvManagement bool `json:"EnableLiveTvManagement"`
|
||||
EnableLiveTvAccess bool `json:"EnableLiveTvAccess"`
|
||||
EnableMediaPlayback bool `json:"EnableMediaPlayback"`
|
||||
EnableAudioPlaybackTranscoding bool `json:"EnableAudioPlaybackTranscoding"`
|
||||
EnableVideoPlaybackTranscoding bool `json:"EnableVideoPlaybackTranscoding"`
|
||||
EnablePlaybackRemuxing bool `json:"EnablePlaybackRemuxing"`
|
||||
ForceRemoteSourceTranscoding bool `json:"ForceRemoteSourceTranscoding"`
|
||||
EnableContentDeletion bool `json:"EnableContentDeletion"`
|
||||
EnableContentDeletionFromFolders []interface{} `json:"EnableContentDeletionFromFolders"`
|
||||
EnableContentDownloading bool `json:"EnableContentDownloading"`
|
||||
EnableSyncTranscoding bool `json:"EnableSyncTranscoding"`
|
||||
EnableMediaConversion bool `json:"EnableMediaConversion"`
|
||||
EnabledDevices []interface{} `json:"EnabledDevices"`
|
||||
EnableAllDevices bool `json:"EnableAllDevices"`
|
||||
EnabledChannels []interface{} `json:"EnabledChannels"`
|
||||
EnableAllChannels bool `json:"EnableAllChannels"`
|
||||
EnabledFolders []string `json:"EnabledFolders"`
|
||||
EnableAllFolders bool `json:"EnableAllFolders"`
|
||||
InvalidLoginAttemptCount int `json:"InvalidLoginAttemptCount"`
|
||||
LoginAttemptsBeforeLockout int `json:"LoginAttemptsBeforeLockout"`
|
||||
MaxActiveSessions int `json:"MaxActiveSessions"`
|
||||
EnablePublicSharing bool `json:"EnablePublicSharing"`
|
||||
BlockedMediaFolders []interface{} `json:"BlockedMediaFolders"`
|
||||
BlockedChannels []interface{} `json:"BlockedChannels"`
|
||||
RemoteClientBitrateLimit int `json:"RemoteClientBitrateLimit"`
|
||||
AuthenticationProviderID string `json:"AuthenticationProviderId"`
|
||||
PasswordResetProviderID string `json:"PasswordResetProviderId"`
|
||||
SyncPlayAccess string `json:"SyncPlayAccess"`
|
||||
}
|
Loading…
Reference in new issue