Skip to content

Commit

Permalink
Merge pull request #2 from drewstinnett/feature-client-services
Browse files Browse the repository at this point in the history
Feature client services
  • Loading branch information
drewstinnett authored Oct 1, 2022
2 parents efe0d50 + f24a89e commit 338b0cf
Show file tree
Hide file tree
Showing 15 changed files with 513 additions and 343 deletions.
32 changes: 32 additions & 0 deletions cli/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"

"github.com/drewstinnett/go-letterboxd"
"github.com/gobwas/glob"
"github.com/rs/zerolog/log"
)

// Given a slice of strings, return a slice of ListIDs
Expand All @@ -23,3 +25,33 @@ func parseListArgs(args []string) ([]*letterboxd.ListID, error) {
}
return ret, nil
}

func mustParseListArgs(args []string) []*letterboxd.ListID {
lists, err := parseListArgs(args)
if err != nil {
panic(err)
}
return lists
}

// MatchesGlobOf returns true if an item matches any of the given globs
func MatchesGlobOf(item string, globs []string) bool {
for _, matchGlob := range globs {
g := glob.MustCompile(matchGlob)
got := g.Match(item)
if got {
return true
}
}
log.Debug().Str("title", item).Msg("Skipping because it matches no globs")
return false
}

func ContainsString(ss []string, s string) bool {
for _, item := range ss {
if s == item {
return true
}
}
return false
}
76 changes: 73 additions & 3 deletions cli/cmd/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ func TestParseListArgs(t *testing.T) {
wantErr bool
}{
{
[]string{"foo/bar"},
[]*letterboxd.ListID{
args: []string{"foo/bar"},
want: []*letterboxd.ListID{
{
User: "foo",
Slug: "bar",
},
},
false,
wantErr: false,
},
{
args: []string{"foobar"},
wantErr: true,
},
}
for _, tt := range tests {
Expand All @@ -33,3 +37,69 @@ func TestParseListArgs(t *testing.T) {
}
}
}

func TestMustParseListArgs(t *testing.T) {
tests := []struct {
args []string
want []*letterboxd.ListID
wantPanic bool
}{
{
args: []string{"foo/bar"},
want: []*letterboxd.ListID{
{
User: "foo",
Slug: "bar",
},
},
wantPanic: false,
},
{
args: []string{"foobar"},
wantPanic: true,
},
}
for _, tt := range tests {
if tt.wantPanic {
require.Panics(t, func() { mustParseListArgs(tt.args) })
} else {
got := mustParseListArgs(tt.args)
require.Equal(t, tt.want, got)
}
}
}

func TestMatchesGlobOf(t *testing.T) {
tests := map[string]struct {
item string
globs []string
want bool
}{
"good": {
item: "foo", globs: []string{"f*"}, want: true,
},
"bad": {
item: "foo", globs: []string{"b*"}, want: false,
},
}
for k, tt := range tests {
got := MatchesGlobOf(tt.item, tt.globs)
require.Equal(t, tt.want, got, k)
}
}

func TestContainsString(t *testing.T) {
tests := []struct {
s []string
e string
r bool
}{
{s: []string{"a", "b", "c"}, e: "a", r: true},
{s: []string{"a", "b", "c"}, e: "d", r: false},
}

for _, test := range tests {
got := ContainsString(test.s, test.e)
require.Equal(t, test.r, got)
}
}
95 changes: 25 additions & 70 deletions cli/cmd/recommend.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
"fmt"
"time"

"github.com/apex/log"
tmdb "github.com/cyruzin/golang-tmdb"
"github.com/drewstinnett/go-letterboxd"
"github.com/drewstinnett/letswatch"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
Expand All @@ -47,19 +47,19 @@ var recommendCmd = &cobra.Command{
isoBatchFilter := &letterboxd.FilmBatchOpts{}

if len(movieCollectOpts.Lists) > 0 {
log.Info("Getting lists")
log.Info().Msg("Getting lists")
isoBatchFilter.List = movieCollectOpts.Lists
}

if movieCollectOpts.Watchlist {
log.Info("Adding Watchlist to ISO")
log.Info().Msg("Adding Watchlist to ISO")
isoBatchFilter.WatchList = []string{meInfo.LetterboxdUsername}
}

// Collect watched films first
watchedIDs := []string{}
if !movieFilterOpts.IncludeWatched {
log.Info("Getting watched films")
log.Info().Msg("Getting watched films")
wfilmC := make(chan *letterboxd.Film)
wdoneC := make(chan error)
go lwc.LetterboxdClient.User.StreamWatched(nil, meInfo.LetterboxdUsername, wfilmC, wdoneC)
Expand All @@ -70,16 +70,14 @@ var recommendCmd = &cobra.Command{
if film.ExternalIDs != nil {
watchedIDs = append(watchedIDs, film.ExternalIDs.IMDB)
} else {
log.WithFields(log.Fields{
"title": film.Title,
}).Debugf("No external IDs, skipping")
log.Debug().Str("title", film.Title).Msg("No external IDs, skipping")
}
case err := <-wdoneC:
if err != nil {
log.WithError(err).Error("Failed to get watched films")
log.Error().Err(err).Msg("Failed to get watched films")
wdoneC <- err
} else {
log.Debug("Finished getting watched films")
log.Debug().Msg("Finished getting watched films")
loop = false
}
}
Expand All @@ -96,10 +94,10 @@ var recommendCmd = &cobra.Command{
isoFilms = append(isoFilms, film)
case err := <-done:
if err != nil {
log.WithError(err).Error("Failed to get iso films")
log.Error().Err(err).Msg("Failed to get iso films")
done <- err
} else {
log.Debug("Finished streaming ISO films")
log.Debug().Msg("Finished streaming ISO films")
loop = false
}
}
Expand All @@ -111,9 +109,7 @@ var recommendCmd = &cobra.Command{
var disqualified bool
for _, watchedMovie := range watchedIDs {
if item.ExternalIDs.IMDB == watchedMovie {
log.WithFields(log.Fields{
"film": item.Title,
}).Debug("Already watched")
log.Debug().Str("film", item.Title).Msg("Already watched")
disqualified = true
// TODO: Should we break further up?
break
Expand All @@ -124,9 +120,7 @@ var recommendCmd = &cobra.Command{
}
// Do some checking on the y ear
if (movieFilterOpts.Earliest > 0) && (item.Year < movieFilterOpts.Earliest) {
log.WithFields(log.Fields{
"film": item.Title,
}).Debug("Released too early")
log.Debug().Str("film", item.Title).Msg("Released too early")
continue
}

Expand All @@ -135,27 +129,15 @@ var recommendCmd = &cobra.Command{
if item.ExternalIDs.IMDB != "" {
m, err = lwc.TMDB.GetWithIMDBID(ctx, item.ExternalIDs.IMDB)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"imdbid": item.ExternalIDs.IMDB,
"tmdbid": item.ExternalIDs.TMDB,
"title": item.Title,
}).Warn("Error getting movie from TMDB")
log.Warn().Err(err).Str("imdbid", item.ExternalIDs.IMDB).Str("tmdbid", item.ExternalIDs.TMDB).Str("title", item.Title).Msg("Error getting movie from TMDB")
continue
}
} else {
log.WithFields(log.Fields{
"imdbid": item.ExternalIDs.IMDB,
"tmdbid": item.ExternalIDs.TMDB,
"title": item.Title,
}).Debug("Movie does not have an IMDB entry. Skipping...")
log.Debug().Str("imdbid", item.ExternalIDs.IMDB).Str("tmdbid", item.ExternalIDs.TMDB).Str("title", item.Title).Msg("Movie does not have an IMDB entry. Skipping...")
}

if m == nil {
log.WithFields(log.Fields{
"imdbid": item.ExternalIDs.IMDB,
"tmdbid": item.ExternalIDs.TMDB,
"title": item.Title,
}).Warn("No TMDB data for film")
log.Warn().Err(err).Str("imdbid", item.ExternalIDs.IMDB).Str("tmdbid", item.ExternalIDs.TMDB).Str("title", item.Title).Msg("No TMDB data for film")
continue
}

Expand All @@ -169,48 +151,33 @@ var recommendCmd = &cobra.Command{
if len(movieFilterOpts.Directors) > 0 {
directorIntersection := intersection(movieFilterOpts.Directors, directors)
if len(directorIntersection) == 0 {
log.WithFields(log.Fields{
"film": m.Title,
"directors": directors,
"want-genre": movieFilterOpts.Directors,
}).Debug("Film does not have any of the directors we want")
log.Debug().Str("film", m.Title).Strs("directors", directors).Strs("want-genre", movieFilterOpts.Directors).Msg("Film does not have any of the directors we want")
continue
}
}

// Filter based on language
if movieFilterOpts.Language != "" && m.OriginalLanguage != movieFilterOpts.Language {
log.WithFields(log.Fields{
"film": m.Title,
"language": m.OriginalLanguage,
}).Debug("Wrong language")
log.Debug().Str("film", m.Title).Str("language", m.OriginalLanguage).Msg("Wrong language")
continue
}

rt := time.Duration(m.Runtime) * time.Minute
if movieFilterOpts.MaxRuntime != 0 && rt > movieFilterOpts.MaxRuntime {
log.WithFields(log.Fields{
"film": m.Title,
"runtime": m.Runtime,
"max-time": movieFilterOpts.MaxRuntime,
}).Debug("Too long")
log.Debug().Str("film", m.Title).Int("runtime", m.Runtime).Str("max-time", fmt.Sprint(movieFilterOpts.MaxRuntime)).Msg("Too long")
continue
}
if movieFilterOpts.MinRuntime != 0 && rt < movieFilterOpts.MinRuntime {
log.WithFields(log.Fields{
"film": m.Title,
"runtime": m.Runtime,
"min-time": movieFilterOpts.MinRuntime,
}).Debug("Too short")
log.Debug().Str("film", m.Title).Int("runtime", m.Runtime).Str("min-time", fmt.Sprint(movieFilterOpts.MinRuntime)).Msg("Too short")
continue
}

// Ok, looks good, lets find where it's streaming
streaming, err := letswatch.GetStreamingChannels(int(m.ID))
// streaming, err := letswatch.GetStreamingChannels(int(m.ID))
streaming, err := lwc.TMDB.GetStreamingChannels(int(m.ID))
// streaming, err := lwc.TMDB
if err != nil {
log.WithError(err).WithFields(log.Fields{
"title": item.Title,
}).Warn("Error getting streaming channels")
log.Warn().Err(err).Str("title", item.Title).Msg("Error getting streaming channels")
}

// Just my streaming?
Expand All @@ -223,11 +190,7 @@ var recommendCmd = &cobra.Command{
cobra.CheckErr(err)
}
if !isAvailOnPlex && len(streamingOnMy) == 0 {
log.WithFields(log.Fields{
"film": m.Title,
"streaming": streaming,
"my-streaming": meInfo.SubscribedTo,
}).Debug("Film not on any of my streaming subscriptions or plex")
log.Debug().Str("film", m.Title).Strs("streaming", streaming).Strs("my-streaming", meInfo.SubscribedTo).Msg("Film not on any of my streaming subscriptions or plex")
continue
}
} else if movieFilterOpts.OnlyNotMyStreaming {
Expand All @@ -236,11 +199,7 @@ var recommendCmd = &cobra.Command{
cobra.CheckErr(err)
}
if len(streamingOnMy) > 0 || isAvailOnPlex {
log.WithFields(log.Fields{
"film": m.Title,
"streaming": streaming,
"my-streaming": meInfo.SubscribedTo,
}).Debug("Film is on one of my streaming subscriptions")
log.Debug().Str("film", m.Title).Strs("streaming", streaming).Strs("my-streaming", meInfo.SubscribedTo).Msg("Film is on one of my streaming subscriptions")
continue
}
}
Expand All @@ -253,11 +212,7 @@ var recommendCmd = &cobra.Command{
if len(movieFilterOpts.Genres) > 0 {
genreIntersection := intersection(movieFilterOpts.Genres, genres)
if len(genreIntersection) == 0 {
log.WithFields(log.Fields{
"film": m.Title,
"genres": genres,
"want-genre": movieFilterOpts.Genres,
}).Debug("Film does not have any of the genres we want")
log.Debug().Str("film", m.Title).Strs("genres", genres).Strs("want-genres", movieFilterOpts.Genres).Msg("Film does not have any of the genres we want")
continue
}
}
Expand Down
Loading

0 comments on commit 338b0cf

Please sign in to comment.