Skip to content

Commit

Permalink
Add basic include/exclude filter
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiofalci committed Mar 9, 2020
1 parent 5162534 commit 85b8dae
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
6 changes: 4 additions & 2 deletions sconsify.go
Expand Up @@ -41,7 +41,8 @@ func main() {
providedStatusFile := flag.String("status-file", "", "File that sconsify will output status such as track being played.")
providedStatusFileTemplate := flag.String("status-file-template", "", "Status file template.")
providedUi := flag.Bool("ui", true, "Run Sconsify with Console User Interface. If false then no User Interface will be presented and it'll shuffle tracks.")
providedPlaylists := flag.String("playlists", "", "Select just some Playlists to play. Comma separated list.")
providedFilterInclude := flag.String("filter-include", "", "Don't exclude playlists. Comma separated list of playlist name or ID.")
providedFilterExclude := flag.String("filter-exclude", "", "Exclude playlists. Comma separated list of playlist name or ID.")
providedPreferredBitrate := flag.String("preferred-bitrate", "320k", "Preferred bitrate: 96k, 160k, 320k.")
providedNoUiSilent := flag.Bool("noui-silent", false, "Silent mode when no UI is used.")
providedNoUiRepeatOn := flag.Bool("noui-repeat-on", true, "Play your playlist and repeat it after the last track.")
Expand Down Expand Up @@ -79,7 +80,8 @@ func main() {

initConf := &spotify.SpotifyInitConf{
WebApiAuth: *providedWebApi,
PlaylistFilter: *providedPlaylists,
IncludeFilter: *providedFilterInclude,
ExcludeFilter: *providedFilterExclude,
PreferredBitrate: *providedPreferredBitrate,
CacheWebApiToken: *providedWebApiCacheToken,
CacheWebApiContent: *providedWebApiCacheContent,
Expand Down
9 changes: 6 additions & 3 deletions spotify/spotify.go
Expand Up @@ -21,14 +21,16 @@ type Spotify struct {
pa *portAudio
session *sp.Session
appKey []byte
playlistFilter []string
includeFilter []string
excludeFilter []string
client *webspotify.Client
cacheWebApiContent bool
}

type SpotifyInitConf struct {
WebApiAuth bool
PlaylistFilter string
IncludeFilter string
ExcludeFilter string
PreferredBitrate string
CacheWebApiToken bool
CacheWebApiContent bool
Expand All @@ -46,7 +48,8 @@ func Initialise(initConf *SpotifyInitConf, username string, pass []byte, events

func initialiseSpotify(initConf *SpotifyInitConf, username string, pass []byte, events *sconsify.Events, publisher *sconsify.Publisher) error {
spotify := &Spotify{events: events, publisher: publisher}
spotify.setPlaylistFilter(initConf.PlaylistFilter)
spotify.setIncludeFilter(initConf.IncludeFilter)
spotify.setExcludeFilter(initConf.ExcludeFilter)
spotify.cacheWebApiContent = initConf.CacheWebApiContent
if err := spotify.initKey(); err != nil {
return err
Expand Down
54 changes: 39 additions & 15 deletions spotify/spotify_playlists.go
Expand Up @@ -144,7 +144,7 @@ func (spotify *Spotify) loadPlaylists(offset int, privateUser *webspotify.Privat
options := &webspotify.Options{Limit: &limit, Offset: &offset}
if simplePlaylistPage, err := spotify.client.GetPlaylistsForUserOpt(privateUser.ID, options); err == nil {
for _, webPlaylist := range simplePlaylistPage.Playlists {
if spotify.isOnFilter(webPlaylist.Name) {
if spotify.isOnFilter(webPlaylist.Name, webPlaylist.ID.String()) {
spotify.loadPlaylistTracks(&webPlaylist, playlists)
}
}
Expand Down Expand Up @@ -187,9 +187,9 @@ func (spotify *Spotify) loadPlaylistTracks(webPlaylist *webspotify.SimplePlaylis
total = playlistTrackPage.Total

if offset > total {
infrastructure.Debugf("%v: loaded %v from %v tracks\n", playlist.Name(), total, total)
infrastructure.Debugf("%v (%v): loaded %v from %v tracks\n", playlist.Name(), playlist.ToSpotifyID(), total, total)
} else {
infrastructure.Debugf("%v: loaded %v from %v tracks\n", playlist.Name(), offset, total)
infrastructure.Debugf("%v (%v): loaded %v from %v tracks\n", playlist.Name(), playlist.ToSpotifyID(), offset, total)
}
}

Expand Down Expand Up @@ -293,27 +293,51 @@ func (spotify *Spotify) initTrack(playlistTrack *sp.PlaylistTrack) *sconsify.Tra
}

func (spotify *Spotify) canAddPlaylist(playlist *sp.Playlist, playlistType sp.PlaylistType) bool {
return playlistType == sp.PlaylistTypePlaylist && spotify.isOnFilter(playlist.Name())
return playlistType == sp.PlaylistTypePlaylist && spotify.isOnFilter(playlist.Name(), playlist.Link().String())
}

func (spotify *Spotify) isOnFilter(playlist string) bool {
if spotify.playlistFilter == nil {
func (spotify *Spotify) isOnFilter(playlistName string, playlistId string) bool {
if spotify.includeFilter == nil && spotify.excludeFilter == nil {
return true
}
for _, filter := range spotify.playlistFilter {
if filter == playlist {
return true
if spotify.includeFilter != nil && spotify.excludeFilter != nil {
infrastructure.Debugf("Cannot use both filter-exclude and filter-include. Ignoring filters.")
return true
}
if spotify.includeFilter != nil {
for _, filter := range spotify.includeFilter {
if filter == playlistName || filter == playlistId {
return true
}
}
return false
}
if spotify.excludeFilter != nil {
for _, filter := range spotify.excludeFilter {
if filter == playlistName || filter == playlistId {
return false
}
}
}
return false
return true
}

func (spotify *Spotify) setIncludeFilter(includeFilter string) {
if includeFilter == "" {
return
}
spotify.includeFilter = strings.Split(includeFilter, ",")
for i := range spotify.includeFilter {
spotify.includeFilter[i] = strings.Trim(spotify.includeFilter[i], " ")
}
}

func (spotify *Spotify) setPlaylistFilter(playlistFilter string) {
if playlistFilter == "" {
func (spotify *Spotify) setExcludeFilter(excludeFilter string) {
if excludeFilter == "" {
return
}
spotify.playlistFilter = strings.Split(playlistFilter, ",")
for i := range spotify.playlistFilter {
spotify.playlistFilter[i] = strings.Trim(spotify.playlistFilter[i], " ")
spotify.excludeFilter = strings.Split(excludeFilter, ",")
for i := range spotify.excludeFilter {
spotify.excludeFilter[i] = strings.Trim(spotify.excludeFilter[i], " ")
}
}

0 comments on commit 85b8dae

Please sign in to comment.