def play_album(album, artist, add=False): # album must be present; artist is optional if not album: return "You didn't provide an album title." s = 'album:' + ' AND album:'.join(album.split()) if artist: s = s + ' artist:' + ' AND artist:'.join(artist.split()) #only brings back actual matches but 25 seems like max for most albums result = solr.search(s, fl='score,track,uri,artist,title,album', sort='score desc', rows=25) tracks = result.docs if not tracks: return selected_album = tracks[0]['album'] try: tracks = sorted([t for t in tracks],key=itemgetter('track')) except KeyError: pass # The if t['album']==selected_album only comes into play # if we retrieved more than one album selected_tracks = [t for t in tracks if t['album']==selected_album] uris = [t.get('uri') for t in selected_tracks] sonos_actions.play(False, uris) titles = [t.get('title', '')+'-'+t.get('artist', '') for t in selected_tracks] title_list = "\n".join([f"{t[0]}. {t[1]}" for t in enumerate(titles, start=1)]) #return f"I will play {len(uris)} tracks from {selected_album}:\n{title_list}." return f"{len(uris)} tracks from {selected_album}:\n{title_list}"
def mix(artist1, artist2): all_tracks = [] for artist in (artist1, artist2): if artist: s = 'artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, fl='artist,title,uri', rows=500) count = len(result) if count: print(f"Total track count for {artist} was {count}") tracks = result.docs k = 5 if count >= 5 else count random_tracks = random.sample(tracks, k) all_tracks.append(random_tracks) else: return f"I couldn't find any tracks for {artist}" else: return "I couldn't find one or both of the artists you were looking for." x = all_tracks[0] y = all_tracks[1] selected_tracks = [t for sublist in zip(x,y) for t in sublist] uris = [t.get('uri') for t in selected_tracks] sonos_actions.play(False, uris) titles = [t.get('title', '')+'-'+t.get('artist', '') for t in selected_tracks] title_list = "\n".join([f"{t[0]}. {t[1]}" for t in enumerate(titles, start=1)]) return f"The mix for {artist1} and {artist2}:\n{title_list}."
def do_browse(self, s): uris = track_display(s) if uris: sonos_actions.play(True, uris) self.onecmd_plus_hooks("queue") self.msg = '' else: self.msg = "You didn't select anything"
def play_album(album, artist): # album must be present; artist is optional print("album =",album) print("artist=",artist) if album: s = 'album:' + ' AND album:'.join(album.split()) if artist: s = s + ' artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, fl='score,track,uri,album,title', sort='score desc', rows=25) #**{'rows':25}) #only brings back actual matches but 25 seems like max for most albums tracks = result.docs if tracks: selected_album = tracks[0]['album'] try: tracks = sorted([t for t in tracks],key=itemgetter('track')) except KeyError: pass # The if t['album']==selected_album only comes into play if we retrieved more than one album selected_tracks = [t for t in tracks if t['album']==selected_album] uris = [t.get('uri') for t in selected_tracks] msg = sonos_actions.play(False, uris) print("PlayAlbum return msg from zmq:", msg) titles = ', '.join([t.get('title', '') for t in selected_tracks]) output_speech = f"I will play {len(uris)} tracks from {selected_album}: {titles}" else: output_speech = "I couldn't find any songs from album {}.".format(album) else: output_speech = "I couldn't even find the album." return statement(output_speech)
def mix(artist1, artist2): print("artist1, artist2 = ",artist1,artist2) all_tracks = [] for artist in (artist1, artist2): if artist: s = 'artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, fl='artist,title,uri', rows=500) count = len(result) if count: print("Total track count for {} was {}".format(artist, count)) tracks = result.docs k = 5 if count >= 5 else count selected_tracks = random.sample(tracks, k) all_tracks.append(selected_tracks) else: output_speech = "I couldn't find any tracks for {}".format(artist) return statement(output_speech) else: output_speech = "I couldn't find one or both of the artists you were looking for." return statement(output_speech) x = all_tracks[0] y = all_tracks[1] mix = [t for sublist in zip(x,y) for t in sublist] uris = [t.get('uri') for t in mix] msg = sonos_actions.play(False, uris) print("Mix return msg from zmq:", msg) titles_artists = ', '.join([t.get('title')+' - '+t.get('artist') for t in mix]) return statement(f"I will shuffle {titles_artists}.")
def play_track(title, artist, add): #note the decorator will set add to None # title must be present; artist is optional print("artist =", artist) print("title =", title) print("add =", add) if not title: return statement("I couldn't find the track.") s = 'title:' + ' AND title:'.join(title.split()) if artist: s = s + ' artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, rows=1) #**{'rows':1}) if not len(result): return statement("I couldn't find the track {} by {}.".format( title, artist)) track = result.docs[0] uri = track['uri'] msg = sonos_actions.play(False, [uri]) print("PlayTrack return msg from zmq:", msg) action = 'add' if add else 'play' return statement( f"I will {action} {track.get('title', '')} by {track.get('artist', '')} from album {track.get('album', '')}" )
def mix(artist1, artist2): print("artist1, artist2 = ", artist1, artist2) all_tracks = [] for artist in (artist1, artist2): if artist: s = 'artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, fl='artist,title,uri', rows=500) count = len(result) if count: print("Total track count for {} was {}".format(artist, count)) tracks = result.docs k = 5 if count >= 5 else count selected_tracks = random.sample(tracks, k) all_tracks.append(selected_tracks) else: output_speech = "I couldn't find any tracks for {}".format( artist) return statement(output_speech) else: output_speech = "I couldn't find one or both of the artists you were looking for." return statement(output_speech) x = all_tracks[0] y = all_tracks[1] mix = [t for sublist in zip(x, y) for t in sublist] uris = [t.get('uri') for t in mix] msg = sonos_actions.play(False, uris) print("Mix return msg from zmq:", msg) titles_artists = ', '.join( [t.get('title') + ' - ' + t.get('artist') for t in mix]) return statement(f"I will shuffle {titles_artists}.")
def play_station(station): if station.lower()=='deborah': s = 'album:(c)' result = solr.search(s, fl='uri,title,artist', rows=600) count = len(result) print(f"Total track count for Deborah tracks was {count}") tracks = result.docs selected_tracks = random.sample(tracks, 20) # randomly decided to pick 20 songs uris = [t.get('uri') for t in selected_tracks] sonos_actions.play(False, uris) titles = [t.get('title', '')+'-'+t.get('artist', '') for t in selected_tracks] title_list = "\n".join([f"{t[0]}. {t[1]}" for t in enumerate(titles, start=1)]) return f"I will play these songs that Deborah chose:\n{title_list}." else: sonos_actions.play_station(station) return f"I will try to play station {station}."
def play_track(title, artist, add): #note the decorator will set add to None # title must be present; artist is optional if not title: return "You didn't provide a track title." s = 'title:' + ' AND title:'.join(title.split()) if artist: s = s + ' artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, rows=1) if not len(result): #return f"I couldn't find the track {title}{' by'+artist if artist else ''}." return track = result.docs[0] uri = track['uri'] sonos_actions.play(add, [uri]) #action = 'add' if add else 'play' return f"{track.get('title', '')} by {track.get('artist', '')} " \ f"from album {track.get('album', '')}"
def do_select(self, s): if 'by' in s: title, artist = s.split(' by ') else: title, artist = s ,'' if not title: self.msg = "You didn't provide a track title." return s = 'title:' + ' AND title:'.join(title.split()) if artist: s = s + ' artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, rows=5) if not len(result): self.msg = f"I couldn't find any track with title {title} by {artist}." return tracks = result.docs # list of tuples to select is [(uri, title from album), # (uri2, title2 from album2) ...] uri = self.select2([(t.get('uri'), t.get('title', '')+" from "+t.get('album', '')) for t in tracks], "Which track? ") sonos_actions.play(True, [uri]) self.msg = uri
def play_station(station): if station.lower()=='deborah': s = 'album:(c)' result = solr.search(s, fl='uri', rows=600) count = len(result) print("Total track count for Deborah tracks was {}".format(count)) tracks = result.docs selected_tracks = random.sample(tracks, 20) # randomly decided to pick 20 songs uris = [t.get('uri') for t in selected_tracks] msg = sonos_actions.play(False, uris) else: msg = sonos_actions.play_station(station) print("PlayStation({}) return msg from zmq:".format(station), msg) return statement("I will try to play station {}.".format(station))
def play_station(station): if station.lower() == 'deborah': s = 'album:(c)' result = solr.search(s, fl='uri', rows=600) count = len(result) print("Total track count for Deborah tracks was {}".format(count)) tracks = result.docs selected_tracks = random.sample( tracks, 20) # randomly decided to pick 20 songs uris = [t.get('uri') for t in selected_tracks] msg = sonos_actions.play(False, uris) else: msg = sonos_actions.play_station(station) print("PlayStation({}) return msg from zmq:".format(station), msg) return statement("I will try to play station {}.".format(station))
def shuffle(artist): if not artist: return statement("I couldn't find the artist you were looking for. Sorry.") s = 'artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, fl='artist,title,uri', rows=500) count = len(result) if not count: return statement("I couldn't find any tracks for {}".format(artist)) print("Total track count for {} was {}".format(artist, count)) tracks = result.docs k = 10 if count >= 10 else count selected_tracks = random.sample(tracks, k) uris = [t.get('uri') for t in selected_tracks] msg = sonos_actions.play(False, uris) print("Shuffle return msg from zmq:", msg) titles = ', '.join([t.get('title') for t in selected_tracks]) return statement(f"I will shuffle {titles}.")
def shuffle(artist): if not artist: return statement( "I couldn't find the artist you were looking for. Sorry.") s = 'artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, fl='artist,title,uri', rows=500) count = len(result) if not count: return statement("I couldn't find any tracks for {}".format(artist)) print("Total track count for {} was {}".format(artist, count)) tracks = result.docs k = 10 if count >= 10 else count selected_tracks = random.sample(tracks, k) uris = [t.get('uri') for t in selected_tracks] msg = sonos_actions.play(False, uris) print("Shuffle return msg from zmq:", msg) titles = ', '.join([t.get('title') for t in selected_tracks]) return statement(f"I will shuffle {titles}.")
def play_album(album, artist): # album must be present; artist is optional print("album =", album) print("artist=", artist) if album: s = 'album:' + ' AND album:'.join(album.split()) if artist: s = s + ' artist:' + ' AND artist:'.join(artist.split()) result = solr.search( s, fl='score,track,uri,album,title', sort='score desc', rows=25 ) #**{'rows':25}) #only brings back actual matches but 25 seems like max for most albums tracks = result.docs if tracks: selected_album = tracks[0]['album'] try: tracks = sorted([t for t in tracks], key=itemgetter('track')) except KeyError: pass # The if t['album']==selected_album only comes into play if we retrieved more than one album selected_tracks = [ t for t in tracks if t['album'] == selected_album ] uris = [t.get('uri') for t in selected_tracks] msg = sonos_actions.play(False, uris) print("PlayAlbum return msg from zmq:", msg) titles = ', '.join([t.get('title', '') for t in selected_tracks]) output_speech = f"I will play {len(uris)} tracks from {selected_album}: {titles}" else: output_speech = "I couldn't find any songs from album {}.".format( album) else: output_speech = "I couldn't even find the album." return statement(output_speech)
def play_track(title, artist, add): #note the decorator will set add to None # title must be present; artist is optional print("artist =",artist) print("title =",title) print("add =", add) if not title: return statement("I couldn't find the track.") s = 'title:' + ' AND title:'.join(title.split()) if artist: s = s + ' artist:' + ' AND artist:'.join(artist.split()) result = solr.search(s, rows=1) #**{'rows':1}) if not len(result): return statement("I couldn't find the track {} by {}.".format(title,artist)) track = result.docs[0] uri = track['uri'] msg = sonos_actions.play(False, [uri]) print("PlayTrack return msg from zmq:", msg) action = 'add' if add else 'play' return statement(f"I will {action} {track.get('title', '')} by {track.get('artist', '')} from album {track.get('album', '')}")