Skip to content

Commit

Permalink
live processing of items
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz committed Feb 9, 2020
1 parent e77128e commit 0152fd5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 92 deletions.
37 changes: 8 additions & 29 deletions cmd/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/l3uddz/mediarr/database"
providerObj "github.com/l3uddz/mediarr/provider"
pvrObj "github.com/l3uddz/mediarr/pvr"
"github.com/l3uddz/mediarr/utils/media"
"github.com/spf13/cobra"
"strings"
)
Expand All @@ -16,6 +15,8 @@ var moviesCmd = &cobra.Command{

Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
var err error

// validate core inputs
if err := parseValidateInputs(args); err != nil {
log.WithError(err).Fatal("Failed validating inputs")
Expand Down Expand Up @@ -44,15 +45,15 @@ var moviesCmd = &cobra.Command{
}

// get existing media
existingMediaItems, err := pvr.GetExistingMedia()
existingMediaItems, err = pvr.GetExistingMedia()
if err != nil {
log.WithError(err).Fatal("Failed retrieving existing media from pvr")
}

// build logic map
logic := map[string]interface{}{
"page-from": flagPageFrom,
"page-to": flagPageTo,
"limit": flagLimit,
"want-callback": shouldAcceptMediaItem,
}

// build param map
Expand All @@ -67,29 +68,8 @@ var moviesCmd = &cobra.Command{
log.WithError(err).Fatal("Failed retrieving media from provider")
}

// remove existing media items
newMediaItems, err := media.PruneExistingMedia(existingMediaItems, foundMediaItems)
if err != nil {
log.WithError(err).Fatal("Failed removing existing media from provider media items")
}

newMediaItemsSize := len(newMediaItems)
log.WithField("new_media_items", newMediaItemsSize).Info("Pruned existing media items from provider items")

// iterate items evaluating against filters
for _, mediaItem := range newMediaItems {
// ignore this item?
ignore, err := pvr.ShouldIgnore(&mediaItem)
if err != nil {
log.WithError(err).Error("Failed evaluating ignore expressions against: %+v", mediaItem)
continue
}

if ignore {
log.Debugf("Ignoring: %+v", mediaItem)
continue
}

// iterate accepted items
for _, mediaItem := range foundMediaItems {
log.Infof("Accepted: %+v", mediaItem)
}
},
Expand All @@ -105,8 +85,7 @@ func init() {
// optional flags
moviesCmd.Flags().BoolVarP(&flagRefreshCache, "refresh-cache", "r", false, "Refresh the locally stored cache.")

moviesCmd.Flags().IntVar(&flagPageFrom, "page-from", 1, "Start from page.")
moviesCmd.Flags().IntVar(&flagPageTo, "page-to", 0, "Finish on page.")
moviesCmd.Flags().IntVar(&flagLimit, "limit", 0, "Max accepted items to add.")

moviesCmd.Flags().StringVar(&flagCountry, "country", "", "Country to filter results.")
moviesCmd.Flags().StringVar(&flagLanguage, "language", "", "Language to filter results.")
Expand Down
34 changes: 32 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ var (
flagRefreshCache = false

flagSearchType string
flagPageFrom int
flagPageTo int
flagLimit int

flagCountry string
flagLanguage string
Expand All @@ -43,6 +42,8 @@ var (
pvrConfig *config.Pvr
pvr pvrObj.Interface

existingMediaItems map[string]config.MediaItem

providerName string
lowerProviderName string
providerCfg *config.Provider
Expand Down Expand Up @@ -148,3 +149,32 @@ func parseValidateInputs(args []string) error {

return nil
}

func shouldAcceptMediaItem(mediaItem *config.MediaItem) bool {
// item already exists in pvr?
if mediaItem.ImdbId != "" {
if _, exists := existingMediaItems[mediaItem.ImdbId]; exists {
return false
}
}
if mediaItem.TmdbId != "" {
if _, exists := existingMediaItems[mediaItem.TmdbId]; exists {
return false
}
}
if mediaItem.TvdbId != "" {
if _, exists := existingMediaItems[mediaItem.TvdbId]; exists {
return false
}
}

// pvr should ignore this item?
if ignore, err := pvr.ShouldIgnore(mediaItem); err != nil {
log.WithError(err).Error("Failed evaluating ignore expressions against: %+v", mediaItem)
return false
} else if ignore {
return false
}

return true
}
37 changes: 8 additions & 29 deletions cmd/shows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/l3uddz/mediarr/database"
providerObj "github.com/l3uddz/mediarr/provider"
pvrObj "github.com/l3uddz/mediarr/pvr"
"github.com/l3uddz/mediarr/utils/media"
"github.com/spf13/cobra"
"strings"
)
Expand All @@ -16,6 +15,8 @@ var showsCmd = &cobra.Command{

Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
var err error

// validate inputs
if err := parseValidateInputs(args); err != nil {
log.WithError(err).Fatal("Failed validating inputs")
Expand Down Expand Up @@ -44,15 +45,15 @@ var showsCmd = &cobra.Command{
}

// get existing media
existingMediaItems, err := pvr.GetExistingMedia()
existingMediaItems, err = pvr.GetExistingMedia()
if err != nil {
log.WithError(err).Fatal("Failed retrieving existing media from pvr")
}

// build logic map
logic := map[string]interface{}{
"page-from": flagPageFrom,
"page-to": flagPageTo,
"limit": flagLimit,
"want-callback": shouldAcceptMediaItem,
}

// build param map
Expand All @@ -67,29 +68,8 @@ var showsCmd = &cobra.Command{
log.WithError(err).Fatal("Failed retrieving media from provider")
}

// remove existing media items
newMediaItems, err := media.PruneExistingMedia(existingMediaItems, foundMediaItems)
if err != nil {
log.WithError(err).Fatal("Failed removing existing media from provider media items")
}

newMediaItemsSize := len(newMediaItems)
log.WithField("new_media_items", newMediaItemsSize).Info("Pruned existing media items from provider items")

// iterate items evaluating against filters
for _, mediaItem := range newMediaItems {
// ignore this item?
ignore, err := pvr.ShouldIgnore(&mediaItem)
if err != nil {
log.WithError(err).Error("Failed evaluating ignore expressions against: %+v", mediaItem)
continue
}

if ignore {
log.Debugf("Ignoring: %+v", mediaItem)
continue
}

// iterate accepted items
for _, mediaItem := range foundMediaItems {
log.Infof("Accepted: %+v", mediaItem)
}
},
Expand All @@ -105,8 +85,7 @@ func init() {
// optional flags
showsCmd.Flags().BoolVarP(&flagRefreshCache, "refresh-cache", "r", false, "Refresh the locally stored cache.")

showsCmd.Flags().IntVar(&flagPageFrom, "page-from", 1, "Start from page.")
showsCmd.Flags().IntVar(&flagPageTo, "page-to", 0, "Finish on page.")
showsCmd.Flags().IntVar(&flagLimit, "limit", 0, "Max accepted items to add.")

showsCmd.Flags().StringVar(&flagCountry, "country", "", "Country to filter results.")
showsCmd.Flags().StringVar(&flagLanguage, "language", "", "Language to filter results.")
Expand Down
62 changes: 38 additions & 24 deletions provider/tmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,6 @@ func (p *Tmdb) GetMovies(searchType string, logic map[string]interface{}, params

/* Private - Sub-Implements */

func (p *Tmdb) getLogicParam(logic map[string]interface{}, key string) interface{} {
if v, exists := logic[key]; exists {
return v
}

return nil
}

func (p *Tmdb) getRequestParams(params map[string]string) req.Param {
// set request params
reqParams := req.Param{
Expand Down Expand Up @@ -228,18 +220,23 @@ func (p *Tmdb) getMovies(endpoint string, logic map[string]interface{}, params m

p.log.Tracef("Request params: %+v", params)

// parse logic params
var wantCallback func(mediaItem *config.MediaItem) bool = nil
limit := 0
limitReached := false

if v := getLogicParam(logic, "limit"); v != nil {
limit = v.(int)
}

if v := getLogicParam(logic, "want-callback"); v != nil {
wantCallback = v.(func(*config.MediaItem) bool)
}

// fetch all page results
mediaItems := make(map[string]config.MediaItem, 0)
mediaItemsSize := 0
page := 1
pageTo := 0

if v := p.getLogicParam(logic, "page-from"); v != nil {
page = v.(int)
}
if v := p.getLogicParam(logic, "page-to"); v != nil {
pageTo = v.(int)
}

for {
// set params
Expand Down Expand Up @@ -296,8 +293,8 @@ func (p *Tmdb) getMovies(endpoint string, logic map[string]interface{}, params m
continue
}

// set item
mediaItems[itemId] = config.MediaItem{
// init media item
mediaItem := config.MediaItem{
Provider: "tmdb",
TvdbId: "",
TmdbId: itemId,
Expand All @@ -311,18 +308,35 @@ func (p *Tmdb) getMovies(endpoint string, logic map[string]interface{}, params m
Languages: []string{item.OriginalLanguage},
}

// media item wanted?
if wantCallback != nil && !wantCallback(&mediaItem) {
p.log.Tracef("Ignoring: %+v", mediaItem)
continue
} else {
p.log.Debugf("Accepted: %+v", mediaItem)
}

// set media item
mediaItems[itemId] = mediaItem
mediaItemsSize += 1

// stop when limit reached
if limit > 0 && mediaItemsSize >= limit {
// limit was supplied via cli and we have reached this limit
limitReached = true
break
}
}

p.log.WithFields(logrus.Fields{
"page": page,
"pages": s.TotalPages,
}).Debug("Retrieved")
"page": page,
"pages": s.TotalPages,
"accepted_items": mediaItemsSize,
}).Info("Retrieved")

// loop logic

if pageTo > 0 && s.Page == pageTo {
if limitReached {
// the limit has been reached for accepted items
break
}

Expand All @@ -333,6 +347,6 @@ func (p *Tmdb) getMovies(endpoint string, logic map[string]interface{}, params m
}
}

p.log.WithField("movies", mediaItemsSize).Info("Retrieved media items")
p.log.WithField("accepted_items", mediaItemsSize).Info("Retrieved media items")
return mediaItems, nil
}
44 changes: 36 additions & 8 deletions provider/tvmaze.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (p *TvMaze) GetShows(searchType string, logic map[string]interface{}, param

switch searchType {
case SearchTypeSchedule:
return p.getScheduleShows(params)
return p.getScheduleShows(logic, params)
default:
break
}
Expand All @@ -166,7 +166,7 @@ func (p *TvMaze) GetMovies(searchType string, logic map[string]interface{}, para

/* Private - Sub-Implements */

func (p *TvMaze) getScheduleShows(params map[string]string) (map[string]config.MediaItem, error) {
func (p *TvMaze) getScheduleShows(logic map[string]interface{}, params map[string]string) (map[string]config.MediaItem, error) {
// send request
resp, err := web.GetResponse(web.GET, web.JoinURL(p.apiUrl, "/schedule/full"), providerDefaultTimeout,
&providerDefaultRetry, p.rl)
Expand All @@ -188,7 +188,19 @@ func (p *TvMaze) getScheduleShows(params map[string]string) (map[string]config.M

// process response
mediaItems := make(map[string]config.MediaItem, 0)
itemsSize := 0
mediaItemsSize := 0

// parse logic params
var wantCallback func(mediaItem *config.MediaItem) bool = nil
limit := 0

if v := getLogicParam(logic, "limit"); v != nil {
limit = v.(int)
}

if v := getLogicParam(logic, "want-callback"); v != nil {
wantCallback = v.(func(*config.MediaItem) bool)
}

for _, item := range s {
// skip invalid items
Expand All @@ -215,10 +227,8 @@ func (p *TvMaze) getScheduleShows(params map[string]string) (map[string]config.M
continue
}

// add item
itemsSize += 1

mediaItems[itemId] = config.MediaItem{
// init media item
mediaItem := config.MediaItem{
Provider: "tvmaze",
TvdbId: itemId,
ImdbId: item.Embedded.Show.Externals.Imdb,
Expand All @@ -230,8 +240,26 @@ func (p *TvMaze) getScheduleShows(params map[string]string) (map[string]config.M
Languages: []string{item.Embedded.Show.Language},
Genres: []string{item.Embedded.Show.Type},
}

// media item wanted?
if wantCallback != nil && !wantCallback(&mediaItem) {
p.log.Tracef("Ignoring: %+v", mediaItem)
continue
} else {
p.log.Debugf("Accepted: %+v", mediaItem)
}

// set item
mediaItems[itemId] = mediaItem
mediaItemsSize += 1

// stop when limit reached
if limit > 0 && mediaItemsSize >= limit {
// limit was supplied via cli and we have reached this limit
break
}
}

p.log.WithField("shows", itemsSize).Info("Retrieved media items")
p.log.WithField("accepted_items", mediaItemsSize).Info("Retrieved media items")
return mediaItems, nil
}
Loading

0 comments on commit 0152fd5

Please sign in to comment.