Compare commits
2 Commits
839b7f8bdd
...
ed79119f3f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed79119f3f | ||
|
|
66ebae34b0 |
53
main.go
53
main.go
@@ -41,36 +41,33 @@ type Jelly struct {
|
|||||||
func NewJelly(ctx context.Context, u, p, apitoken, optAccessToken string) (Jelly, error) {
|
func NewJelly(ctx context.Context, u, p, apitoken, optAccessToken string) (Jelly, error) {
|
||||||
jelly := Jelly{u: u, apitoken: apitoken, accesstoken: optAccessToken}
|
jelly := Jelly{u: u, apitoken: apitoken, accesstoken: optAccessToken}
|
||||||
|
|
||||||
if optAccessToken == "" {
|
if optAccessToken != "" {
|
||||||
resp, err := jelly.post(ctx, "https://jellyfin.home.blapointe.com/Users/AuthenticateByName", map[string]string{
|
} else if accessToken, err := jelly.login(ctx, p); err != nil {
|
||||||
"Username": u,
|
return Jelly{}, err
|
||||||
"Pw": p,
|
} else {
|
||||||
}, "Authorization", jelly._authHeader(jelly.apitoken))
|
jelly.accesstoken = accessToken
|
||||||
if err != nil {
|
|
||||||
return Jelly{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var AccessToken struct {
|
|
||||||
AccessToken string
|
|
||||||
}
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&AccessToken); err != nil {
|
|
||||||
return Jelly{}, err
|
|
||||||
}
|
|
||||||
jelly.accesstoken = AccessToken.AccessToken
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return jelly, nil
|
return jelly, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jelly Jelly) authHeader() string {
|
func (jelly Jelly) login(ctx context.Context, p string) (string, error) {
|
||||||
return jelly._authHeader(jelly.accesstoken)
|
resp, err := jelly.post(ctx, "/Users/AuthenticateByName", map[string]string{
|
||||||
|
"Username": jelly.u,
|
||||||
|
"Pw": p,
|
||||||
|
}, "Authorization", jelly._authHeader(jelly.apitoken))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var AccessToken struct {
|
||||||
|
AccessToken string
|
||||||
|
}
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&AccessToken)
|
||||||
|
return AccessToken.AccessToken, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jelly Jelly) _authHeader(token string) string {
|
func (jelly Jelly) post(ctx context.Context, path string, body any, headers ...string) (*http.Response, error) {
|
||||||
return fmt.Sprintf("MediaBrowser Token=%q, Client=%q, Version=%q, DeviceId=%q, Device=%q", token, "client", "version", "deviceId"+jelly.u, "device")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (jelly Jelly) post(ctx context.Context, url string, body any, headers ...string) (*http.Response, error) {
|
|
||||||
t := http.Transport{
|
t := http.Transport{
|
||||||
DisableKeepAlives: true,
|
DisableKeepAlives: true,
|
||||||
}
|
}
|
||||||
@@ -80,7 +77,7 @@ func (jelly Jelly) post(ctx context.Context, url string, body any, headers ...st
|
|||||||
}
|
}
|
||||||
|
|
||||||
b, _ := json.Marshal(body)
|
b, _ := json.Marshal(body)
|
||||||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("https://jellyfin.home.blapointe.com%s", path), bytes.NewReader(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -107,3 +104,11 @@ func (jelly Jelly) post(ctx context.Context, url string, body any, headers ...st
|
|||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (jelly Jelly) authHeader() string {
|
||||||
|
return jelly._authHeader(jelly.accesstoken)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jelly Jelly) _authHeader(token string) string {
|
||||||
|
return fmt.Sprintf("MediaBrowser Token=%q, Client=%q, Version=%q, DeviceId=%q, Device=%q", token, "client", "version", "deviceId"+jelly.u, "device")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user