Skip to content

Commit

Permalink
trakt: add person search to movies
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz committed Feb 11, 2020
1 parent 2f736da commit 84b5ec3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ var moviesCmd = &cobra.Command{
"genre": flagGenre,
"year": flagYear,
"rating": flagRating,

"query": flagQueryStr,
}

// retrieve media
Expand Down Expand Up @@ -104,6 +106,7 @@ func init() {

moviesCmd.Flags().IntVar(&flagLimit, "limit", 0, "Max accepted items to add.")

moviesCmd.Flags().StringVar(&flagQueryStr, "query", "", "Query for search.")
moviesCmd.Flags().StringVar(&flagCountry, "country", "", "Countries to filter results.")
moviesCmd.Flags().StringVar(&flagLanguage, "language", "", "Languages to filter results.")
moviesCmd.Flags().StringVar(&flagGenre, "genre", "", "Genres to filter results.")
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
flagSearchType string
flagLimit int

flagQueryStr string
flagCountry string
flagLanguage string
flagGenre string
Expand Down
31 changes: 28 additions & 3 deletions provider/trakt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/sirupsen/logrus"
"go.uber.org/ratelimit"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -73,6 +74,10 @@ type TraktMoviesResponse struct {
Movie *TraktMovie `json:"movie"`
}

type TraktPersonCastResponse struct {
Cast []TraktMoviesResponse
}

type TraktShowIds struct {
Trakt int `json:"trakt"`
Slug string `json:"slug"`
Expand Down Expand Up @@ -133,6 +138,7 @@ func NewTrakt() *Trakt {
SearchTypePopular,
SearchTypeNow,
SearchTypeWatched,
SearchTypePerson,
},
}
}
Expand Down Expand Up @@ -224,6 +230,13 @@ func (p *Trakt) GetMovies(searchType string, logic map[string]interface{}, param
return p.getMovies("/movies/boxoffice", logic, params)
case SearchTypeWatched:
return p.getMovies("/movies/watched", logic, params)
case SearchTypePerson:
queryStr, ok := params["query"]
if !ok || queryStr == "" {
return nil, errors.New("person search must have a --query string provided, e.g. bryan-cranston")
}

return p.getMovies(fmt.Sprintf("/people/%s/movies", queryStr), logic, params)
default:
break
}
Expand Down Expand Up @@ -375,9 +388,21 @@ func (p *Trakt) getMovies(endpoint string, logic map[string]interface{}, params

// decode response
var s []TraktMoviesResponse
if err := resp.ToJSON(&s); err != nil {
_ = resp.Response().Body.Close()
return nil, errors.WithMessage(err, "failed decoding movies api response")

if !strings.Contains(endpoint, "/people/") {
// non person search
if err := resp.ToJSON(&s); err != nil {
_ = resp.Response().Body.Close()
return nil, errors.WithMessage(err, "failed decoding movies api response")
}
} else {
// person search
var tmp TraktPersonCastResponse
if err := resp.ToJSON(&tmp); err != nil {
_ = resp.Response().Body.Close()
return nil, errors.WithMessage(err, "failed decoding movies api response")
}
s = tmp.Cast
}

_ = resp.Response().Body.Close()
Expand Down
3 changes: 2 additions & 1 deletion provider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ const (
SearchTypeUpcoming string = "upcoming"
SearchTypePopular string = "popular"
SearchTypeTrending string = "trending"
SearchTypeWatched string = "watched"
SearchTypeWatched string = "watched"
SearchTypePerson string = "person"
)

0 comments on commit 84b5ec3

Please sign in to comment.