Skip to content

Commit

Permalink
Fix json error on successful SignIn() (jrudio#44)
Browse files Browse the repository at this point in the history
* fixed the types for metadata json responses

* added GetPlaylist() to cli

* added GetPlaylist() to cli

* fixed GetPlaylist() - catch non-200 status http errors

* changed types on properties that are int or string

* handle webhook error when if no plex pass

* fixed json error on SignIn() if subscription present
  • Loading branch information
jrudio authored Jan 6, 2022
1 parent c201db8 commit 76aabd2
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 158 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
plex
vendor/*

*.sh
.idea
66 changes: 50 additions & 16 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -15,14 +16,11 @@ import (
)

const (
errKeyNotFound = "key not found"
errNoPlexToken = "no plex auth token in datastore"
errPleaseSignIn = "use command 'sign-in' or 'link-app' to authorize us"
errNoPlexServerInfo = "no plex server in datastore"
errPleaseChooseServer = "use command 'pick-server' to pick a plex server :)"
errFailedToGetPlexToken = "failed to get plex token: %v"
errInvalidSelection = "invalid selection"
errEpisodeIdIsRequired = "episode id is required"
errKeyNotFound = "Key not found"
errNoPlexToken = "no plex auth token in datastore"
errPleaseSignIn = "use command 'sign-in' or 'link-app' to authorize us"
errNoPlexServerInfo = "no plex server in datastore"
errPleaseChooseServer = "use command 'pick-server' to pick a plex server :)"
)

func initPlex(db store, checkForToken bool, checkForServerInfo bool) (*plex.Plex, error) {
Expand All @@ -36,7 +34,7 @@ func initPlex(db store, checkForToken bool, checkForServerInfo bool) (*plex.Plex
if err != nil && err.Error() == errKeyNotFound {
return plexConn, fmt.Errorf("%s\n%s", errNoPlexToken, errPleaseSignIn)
} else if err != nil {
return plexConn, fmt.Errorf(errFailedToGetPlexToken, err)
return plexConn, fmt.Errorf("failed getting plex token: %v", err)
}

return plex.New("", plexToken)
Expand All @@ -49,7 +47,7 @@ func initPlex(db store, checkForToken bool, checkForServerInfo bool) (*plex.Plex
plexToken, err := db.getPlexToken()

if err != nil {
return plexConn, fmt.Errorf(errFailedToGetPlexToken, err)
return plexConn, fmt.Errorf("failed getting plex token: %v", err)
}

plexServer, err := db.getPlexServer()
Expand Down Expand Up @@ -447,7 +445,7 @@ func pickServer(c *cli.Context) error {

// bound check input
if serverIndex < 0 || serverIndex > (len(servers)-1) {
return errors.New(errInvalidSelection)
return errors.New("invalid selection")
}

selectedServer := servers[serverIndex]
Expand All @@ -466,7 +464,7 @@ func pickServer(c *cli.Context) error {

// bound check again
if urlIndex < 0 || urlIndex > (len(selectedServer.Connection)-1) {
return errors.New(errInvalidSelection)
return errors.New("invalid selection")
}

// persist selection to disk
Expand Down Expand Up @@ -677,7 +675,7 @@ func getEpisode(c *cli.Context) error {
}

if c.NArg() == 0 {
return cli.NewExitError(errEpisodeIdIsRequired, 1)
return cli.NewExitError("episode id is required", 1)
}

result, err := plexConn.GetEpisode(c.Args().First())
Expand Down Expand Up @@ -796,7 +794,7 @@ func stopPlayback(c *cli.Context) error {

// bound check user input
if sessionIndex < 0 || sessionIndex > sessionCount-1 {
return cli.NewExitError(errInvalidSelection, 1)
return cli.NewExitError("invalid selection", 1)
}

selectedSession := sessions.MediaContainer.Metadata[sessionIndex]
Expand Down Expand Up @@ -859,7 +857,7 @@ func getMetadata(c *cli.Context) error {
}

if c.NArg() == 0 {
return cli.NewExitError(errEpisodeIdIsRequired, 1)
return cli.NewExitError("episode id is required", 1)
}

result, err := plexConn.GetMetadata(c.Args().First())
Expand Down Expand Up @@ -928,7 +926,7 @@ func downloadMedia(c *cli.Context) error {

// bound check user input
if selection < 0 || selection > len(results.MediaContainer.Metadata)-1 {
return cli.NewExitError(errInvalidSelection, 1)
return cli.NewExitError("invalid selection", 1)
}

selectedMedia := results.MediaContainer.Metadata[selection]
Expand All @@ -942,3 +940,39 @@ func downloadMedia(c *cli.Context) error {

return nil
}

func getPlaylist(c *cli.Context) error {
db, err := startDB()

if err != nil {
return cli.NewExitError(err, 1)
}

defer db.Close()

plexConn, err := initPlex(db, true, true)

if err != nil {
return err
}

if c.NArg() == 0 {
return cli.NewExitError("playlist id is required", 1)
}

playlistID, err := strconv.ParseInt(c.Args().First(), 10, 64)

if err != nil {
return cli.NewExitError(err, 1)
}

result, err := plexConn.GetPlaylist(int(playlistID))

if err != nil {
return cli.NewExitError(err, 1)
}

fmt.Println(result)

return nil
}
5 changes: 5 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ func main() {
},
},
},
{
Name: "playlist",
Usage: "print playlsit items on plex server",
Action: getPlaylist,
},
}

if err := app.Run(os.Args); err != nil {
Expand Down
1 change: 1 addition & 0 deletions error_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ const (
ErrorPINNotAuthorized = "pin is not authorized yet"
ErrorLinkAccount = "failed to link account: %s"
ErrorFailedToSetWebhook = "failed to set webhook"
ErrorWebhook = "webhook error: %s"
)
Loading

0 comments on commit 76aabd2

Please sign in to comment.