Skip to content

Commit 76aabd2

Browse files
authored
Fix json error on successful SignIn() (#44)
* 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
1 parent c201db8 commit 76aabd2

File tree

7 files changed

+174
-158
lines changed

7 files changed

+174
-158
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
plex
22
vendor/*
3-
3+
*.sh
44
.idea

cmd/commands.go

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/url"
77
"os"
88
"path/filepath"
9+
"strconv"
910
"strings"
1011
"time"
1112

@@ -15,14 +16,11 @@ import (
1516
)
1617

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

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

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

5149
if err != nil {
52-
return plexConn, fmt.Errorf(errFailedToGetPlexToken, err)
50+
return plexConn, fmt.Errorf("failed getting plex token: %v", err)
5351
}
5452

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

448446
// bound check input
449447
if serverIndex < 0 || serverIndex > (len(servers)-1) {
450-
return errors.New(errInvalidSelection)
448+
return errors.New("invalid selection")
451449
}
452450

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

467465
// bound check again
468466
if urlIndex < 0 || urlIndex > (len(selectedServer.Connection)-1) {
469-
return errors.New(errInvalidSelection)
467+
return errors.New("invalid selection")
470468
}
471469

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

679677
if c.NArg() == 0 {
680-
return cli.NewExitError(errEpisodeIdIsRequired, 1)
678+
return cli.NewExitError("episode id is required", 1)
681679
}
682680

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

797795
// bound check user input
798796
if sessionIndex < 0 || sessionIndex > sessionCount-1 {
799-
return cli.NewExitError(errInvalidSelection, 1)
797+
return cli.NewExitError("invalid selection", 1)
800798
}
801799

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

861859
if c.NArg() == 0 {
862-
return cli.NewExitError(errEpisodeIdIsRequired, 1)
860+
return cli.NewExitError("episode id is required", 1)
863861
}
864862

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

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

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

943941
return nil
944942
}
943+
944+
func getPlaylist(c *cli.Context) error {
945+
db, err := startDB()
946+
947+
if err != nil {
948+
return cli.NewExitError(err, 1)
949+
}
950+
951+
defer db.Close()
952+
953+
plexConn, err := initPlex(db, true, true)
954+
955+
if err != nil {
956+
return err
957+
}
958+
959+
if c.NArg() == 0 {
960+
return cli.NewExitError("playlist id is required", 1)
961+
}
962+
963+
playlistID, err := strconv.ParseInt(c.Args().First(), 10, 64)
964+
965+
if err != nil {
966+
return cli.NewExitError(err, 1)
967+
}
968+
969+
result, err := plexConn.GetPlaylist(int(playlistID))
970+
971+
if err != nil {
972+
return cli.NewExitError(err, 1)
973+
}
974+
975+
fmt.Println(result)
976+
977+
return nil
978+
}

cmd/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ func main() {
164164
},
165165
},
166166
},
167+
{
168+
Name: "playlist",
169+
Usage: "print playlsit items on plex server",
170+
Action: getPlaylist,
171+
},
167172
}
168173

169174
if err := app.Run(os.Args); err != nil {

error_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ const (
1414
ErrorPINNotAuthorized = "pin is not authorized yet"
1515
ErrorLinkAccount = "failed to link account: %s"
1616
ErrorFailedToSetWebhook = "failed to set webhook"
17+
ErrorWebhook = "webhook error: %s"
1718
)

0 commit comments

Comments
 (0)