Skip to content

Commit

Permalink
add ability for provider to specify search types supported
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz committed Feb 9, 2020
1 parent 06d6e8d commit cbf3de1
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cmd/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
pvrObj "github.com/l3uddz/mediarr/pvr"
"github.com/l3uddz/mediarr/utils/media"
"github.com/spf13/cobra"
"strings"
)

var moviesCmd = &cobra.Command{
Expand All @@ -15,7 +16,7 @@ var moviesCmd = &cobra.Command{

Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
// validate inputs
// validate core inputs
if err := parseValidateInputs(args); err != nil {
log.WithError(err).Fatal("Failed validating inputs")
}
Expand All @@ -31,6 +32,12 @@ var moviesCmd = &cobra.Command{
log.WithError(err).Fatalf("Failed initializing provider object for: %s", providerName)
}

// validate provider supports search type
if supported := provider.SupportsMoviesSearchType(flagSearchType); !supported {
log.WithField("search_type", flagSearchType).Fatalf("Unsupported search type, valid types: %s",
strings.Join(provider.GetMoviesSearchTypes(), ", "))
}

// init pvr object
if err := pvr.Init(pvrObj.MOVIE); err != nil {
log.WithError(err).Fatalf("Failed initializing pvr object for: %s", pvrName)
Expand All @@ -43,7 +50,7 @@ var moviesCmd = &cobra.Command{
}

// retrieve media
foundMediaItems, err := provider.GetShows()
foundMediaItems, err := provider.GetMovies()
if err != nil {
log.WithError(err).Fatal("Failed retrieving media from provider")
}
Expand Down Expand Up @@ -79,5 +86,10 @@ var moviesCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(moviesCmd)

// required flags
moviesCmd.Flags().StringVarP(&flagSearchType, "search-type", "t", "", "Search type.")
_ = moviesCmd.MarkFlagRequired("search-type")

// optional flags
moviesCmd.Flags().BoolVarP(&flagRefreshCache, "refresh-cache", "r", false, "Refresh the locally stored cache.")
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var (
flagLogFile = "activity.log"
flagRefreshCache = false

flagSearchType string

// Global vars
log *logrus.Entry
continueRunning *atomic.Bool
Expand Down
12 changes: 12 additions & 0 deletions cmd/shows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
pvrObj "github.com/l3uddz/mediarr/pvr"
"github.com/l3uddz/mediarr/utils/media"
"github.com/spf13/cobra"
"strings"
)

var showsCmd = &cobra.Command{
Expand Down Expand Up @@ -36,6 +37,12 @@ var showsCmd = &cobra.Command{
log.WithError(err).Fatalf("Failed initializing pvr object for: %s", pvrName)
}

// validate provider supports search type
if supported := provider.SupportsShowsSearchType(flagSearchType); !supported {
log.WithField("search_type", flagSearchType).Fatalf("Unsupported search type, valid types: %s",
strings.Join(provider.GetShowsSearchTypes(), ", "))
}

// get existing media
existingMediaItems, err := pvr.GetExistingMedia()
if err != nil {
Expand Down Expand Up @@ -79,5 +86,10 @@ var showsCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(showsCmd)

// required flags
showsCmd.Flags().StringVarP(&flagSearchType, "search-type", "t", "", "Search type.")
_ = showsCmd.MarkFlagRequired("search-type")

// optional flags
showsCmd.Flags().BoolVarP(&flagRefreshCache, "refresh-cache", "r", false, "Refresh the locally stored cache.")
}
7 changes: 7 additions & 0 deletions provider/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ import "github.com/l3uddz/mediarr/config"

type Interface interface {
Init(MediaType, *config.Provider) error

GetShowsSearchTypes() []string
GetMoviesSearchTypes() []string
SupportsShowsSearchType(string) bool
SupportsMoviesSearchType(string) bool

GetShows() (map[string]config.MediaItem, error)
GetMovies() (map[string]config.MediaItem, error)
}
29 changes: 29 additions & 0 deletions provider/tmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/imroc/req"
"github.com/l3uddz/mediarr/config"
"github.com/l3uddz/mediarr/logger"
"github.com/l3uddz/mediarr/utils/lists"
"github.com/l3uddz/mediarr/utils/web"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -20,6 +21,9 @@ type Tmdb struct {
apiKey string

genres map[int]string

supportedShowsSearchTypes []string
supportedMoviesSearchTypes []string
}

type TmdbGenre struct {
Expand All @@ -40,6 +44,11 @@ func NewTmdb() *Tmdb {
apiUrl: "https://api.themoviedb.org/3",
apiKey: "",
genres: make(map[int]string, 0),

supportedShowsSearchTypes: []string{},
supportedMoviesSearchTypes: []string{
SEARCH_TYPE_NOW,
},
}
}

Expand Down Expand Up @@ -108,6 +117,26 @@ func (p *Tmdb) Init(mediaType MediaType, cfg *config.Provider) error {
return nil
}

func (p *Tmdb) GetShowsSearchTypes() []string {
return p.supportedShowsSearchTypes
}

func (p *Tmdb) SupportsShowsSearchType(searchType string) bool {
return lists.StringListContains(p.supportedShowsSearchTypes, searchType, false)
}

func (p *Tmdb) GetMoviesSearchTypes() []string {
return p.supportedMoviesSearchTypes
}

func (p *Tmdb) SupportsMoviesSearchType(searchType string) bool {
return lists.StringListContains(p.supportedMoviesSearchTypes, searchType, false)
}

func (p *Tmdb) GetShows() (map[string]config.MediaItem, error) {
return nil, errors.New("unsupported media type")
}

func (p *Tmdb) GetMovies() (map[string]config.MediaItem, error) {
return nil, errors.New("unsupported media type")
}
29 changes: 29 additions & 0 deletions provider/tvmaze.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/l3uddz/mediarr/config"
"github.com/l3uddz/mediarr/logger"
"github.com/l3uddz/mediarr/utils/lists"
"github.com/l3uddz/mediarr/utils/web"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -22,6 +23,9 @@ type TvMaze struct {
apiKey string

rl *ratelimit.Limiter

supportedShowsSearchTypes []string
supportedMoviesSearchTypes []string
}

type TvMazeScheduleItem struct {
Expand Down Expand Up @@ -100,6 +104,11 @@ func NewTvMaze() *TvMaze {
cfg: nil,
apiUrl: "http://api.tvmaze.com",
apiKey: "",

supportedShowsSearchTypes: []string{
SEARCH_TYPE_SCHEDULE,
},
supportedMoviesSearchTypes: []string{},
}
}

Expand All @@ -123,6 +132,22 @@ func (p *TvMaze) Init(mediaType MediaType, cfg *config.Provider) error {
return nil
}

func (p *TvMaze) GetShowsSearchTypes() []string {
return p.supportedShowsSearchTypes
}

func (p *TvMaze) SupportsShowsSearchType(searchType string) bool {
return lists.StringListContains(p.supportedShowsSearchTypes, searchType, false)
}

func (p *TvMaze) GetMoviesSearchTypes() []string {
return p.supportedMoviesSearchTypes
}

func (p *TvMaze) SupportsMoviesSearchType(searchType string) bool {
return lists.StringListContains(p.supportedMoviesSearchTypes, searchType, false)
}

func (p *TvMaze) GetShows() (map[string]config.MediaItem, error) {
// send request
resp, err := web.GetResponse(web.GET, web.JoinURL(p.apiUrl, "/schedule/full"), providerDefaultTimeout,
Expand Down Expand Up @@ -192,3 +217,7 @@ func (p *TvMaze) GetShows() (map[string]config.MediaItem, error) {
p.log.WithField("shows", itemsSize).Info("Retrieved media items")
return mediaItems, nil
}

func (p *TvMaze) GetMovies() (map[string]config.MediaItem, error) {
return nil, errors.New("unsupported media type")
}
5 changes: 5 additions & 0 deletions provider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ const (
SHOW MediaType = iota + 1
MOVIE
)

const (
SEARCH_TYPE_SCHEDULE string = "schedule"
SEARCH_TYPE_NOW string = "now_playing"
)

0 comments on commit cbf3de1

Please sign in to comment.