Skip to content

Commit

Permalink
Merge pull request #3 from jeffrg/master
Browse files Browse the repository at this point in the history
add method for getting all shows
  • Loading branch information
Michael Robinson authored Jul 18, 2019
2 parents 0248c6d + 82dd674 commit ace96d9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
10 changes: 7 additions & 3 deletions tvmaze/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ func NewClient() Client {
return Client{}
}

func (c Client) get(url url.URL, ret interface{}) (err error) {
func (c Client) get(url url.URL, ret interface{}) (status int, err error) {
r, err := http.Get(url.String())
if err != nil {
return errors.Wrapf(err, "failed to get url: %s", url.String())
return 0, errors.Wrapf(err, "failed to get url: %s", url.String())
}

if r.StatusCode >= http.StatusBadRequest {
return r.StatusCode, errors.Errorf("received error status code (%d): %s", r.StatusCode, r.Status)
}

defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(&ret)
return r.StatusCode, json.NewDecoder(r.Body).Decode(&ret)
}

func baseURLWithPath(path string) url.URL {
Expand Down
6 changes: 3 additions & 3 deletions tvmaze/episode.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Episode struct {
func (c Client) GetEpisodes(s Show) (episodes []Episode, err error) {
url := baseURLWithPath(fmt.Sprintf("shows/%d/episodes", s.ID))

if err = c.get(url, &episodes); err != nil {
if _, err = c.get(url, &episodes); err != nil {
return nil, err
}

Expand All @@ -33,7 +33,7 @@ func (c Client) GetNextEpisode(s Show) (*Episode, error) {
url := baseURLWithPathQuery(fmt.Sprintf("shows/%d", s.ID), "embed", "nextepisode")

var embed embeddedNextEpisode
if err := c.get(url, &embed); err != nil {
if _, err := c.get(url, &embed); err != nil {
return nil, err
}

Expand All @@ -51,7 +51,7 @@ func (c Client) GetEpisode(s Show, season int, episode int) (*Episode, error) {
})

var epOut Episode
if err := c.get(url, &epOut); err != nil {
if _, err := c.get(url, &epOut); err != nil {
return nil, err
}
return &epOut, nil
Expand Down
33 changes: 25 additions & 8 deletions tvmaze/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package tvmaze
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
"time"

log "github.com/sirupsen/logrus"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

// ShowResponse wraps a TV Maze search response
Expand Down Expand Up @@ -106,11 +108,25 @@ func (s Show) GetIMDBID() string {
return val
}

// GetShows returns a list of all shows in the TVMaze database. When the end of the index
// is reached, a nil slice is returned.
func (c Client) GetShows(page int) ([]Show, error) {
url := baseURLWithPathQuery("shows", "page", strconv.Itoa(page))
shows := []Show{}
if status, err := c.get(url, &shows); err != nil {
if status == http.StatusNotFound {
return nil, nil
}
return nil, err
}
return shows, nil
}

// FindShows finds all matches for a given search string
func (c Client) FindShows(name string) (s []ShowResponse, err error) {
url := baseURLWithPathQuery("search/shows", "q", name)

if err := c.get(url, &s); err != nil {
if _, err := c.get(url, &s); err != nil {
return nil, err
}

Expand All @@ -122,7 +138,7 @@ func (c Client) GetShow(name string) (*Show, error) {
url := baseURLWithPathQuery("singlesearch/shows", "q", name)

show := &Show{}
if err := c.get(url, show); err != nil {
if _, err := c.get(url, show); err != nil {
return nil, err
}

Expand All @@ -134,7 +150,7 @@ func (c Client) GetShowWithID(tvMazeID string) (*Show, error) {
url := baseURLWithPath(fmt.Sprintf("shows/%s", tvMazeID))

show := &Show{}
if err := c.get(url, show); err != nil {
if _, err := c.get(url, show); err != nil {
return nil, err
}

Expand All @@ -146,7 +162,7 @@ func (c Client) GetShowWithTVRageID(tvRageID string) (*Show, error) {
url := baseURLWithPathQuery("lookup/shows", "tvrage", tvRageID)

show := &Show{}
if err := c.get(url, show); err != nil {
if _, err := c.get(url, show); err != nil {
return nil, err
}

Expand All @@ -158,17 +174,18 @@ func (c Client) GetShowWithTVDBID(TVDBID string) (*Show, error) {
url := baseURLWithPathQuery("lookup/shows", "thetvdb", TVDBID)

show := &Show{}
if err := c.get(url, show); err != nil {
if _, err := c.get(url, show); err != nil {
return nil, err
}

return show, nil
}

// RefreshShow refreshes a show from the server
func (c Client) RefreshShow(show *Show) (err error) {
func (c Client) RefreshShow(show *Show) error {
url := baseURLWithPath(fmt.Sprintf("shows/%d", show.ID))
return c.get(url, &show)
_, err := c.get(url, &show)
return err
}

// Date represents a date from tvmaze, supporting nullability
Expand Down
10 changes: 10 additions & 0 deletions tvmaze/tvmaze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import (
func TestTVMaze(t *testing.T) {
c := DefaultClient

t.Run("get shows", func(t *testing.T) {
t.Parallel()
results, err := c.GetShows(0)
require.NoError(t, err)
require.NotEmpty(t, len(results))
results, err = c.GetShows(99999) // This should trigger a 404, as there aren't these many pages in the index
require.NoError(t, err)
require.Nil(t, results)
})

t.Run("find show", func(t *testing.T) {
t.Parallel()
results, err := c.FindShows("archer")
Expand Down

0 comments on commit ace96d9

Please sign in to comment.