Skip to content

Commit

Permalink
add basic playlist and podcast search support
Browse files Browse the repository at this point in the history
  • Loading branch information
ceuk committed Jul 8, 2020
1 parent bd682d6 commit 22c2b1c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="spotui",
version="0.1.14",
version="0.1.15",
author="ceuk",
description="Spotify TUI",
long_description=long_description,
Expand Down
24 changes: 16 additions & 8 deletions spotui/src/MainForm.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, stdscr):
NowPlaying(stdscr),
]
self.search_component = SearchInput(self.stdscr, self.api,
self.search_tracks)
self.search)
self.device_menu_component = DeviceMenu(self.stdscr, self.api,
self.select_device,
self.hide_popup)
Expand All @@ -68,7 +68,7 @@ def __init__(self, stdscr):
self.popup = None

# Set initial tracklist
if self.status and 'context' in self.status and self.status["context"]["uri"]:
if self.status and 'context' in self.status and type(self.status["context"]) is dict and 'uri' in self.status["context"]:
self.change_tracklist(
self.api.get_playlist_tracks(self.status["context"]["uri"]), "Previous Session")
else:
Expand Down Expand Up @@ -150,16 +150,24 @@ def select_next_component(self):
self.select_next_component()

def play_track(self, track):
if track['type'] == 'playlist':
self.change_tracklist(self.api.get_playlist_tracks(
track['id'] if track['id'] else track['uri']), track['name'], track['uri'])
return
if track['type'] == 'show':
self.change_tracklist(self.api.show_episodes(
track['id']), track['name'], track['uri'])
return
if self.device_id:
if self.tracklist_uri:
self.api.start_playback(self.device_id, None,
self.tracklist_uri, {"uri": track})
self.tracklist_uri, {"uri": track["uri"]})
else:
self.api.start_playback(
self.device_id,
list(map(self.__map_tracklist, self.components[0].tracks)),
None,
{"uri": track},
{"uri": track["uri"]},
)

@debounce(0.5)
Expand Down Expand Up @@ -212,11 +220,11 @@ def seek_forward(self):
progress = self.status["progress_ms"]
self.api.seek_track(self.device_id, progress + 10000)

def search_tracks(self, query):
def search(self, query):
self.hide_popup()
query = query.strip()
if query and len(query) > 1:
results = self.api.search_tracks(query)
results = self.api.search(query)
self.change_tracklist(results, "Searching: " + query)
self.render()

Expand All @@ -236,8 +244,8 @@ def show_device_menu(self):
def show_search_bar(self):
if self.popup:
return
self.popup = self.search_component
self.pause_updates = True
self.popup = self.search_component
self.components[self.active_component].deactivate()
self.popup.activate()
self.render()
Expand All @@ -246,11 +254,11 @@ def select_device(self, device_id):
self.device_id = device_id

def hide_popup(self):
self.pause_updates = False
if self.popup:
self.popup.deactivate()
self.popup = None
self.components[self.active_component].activate()
self.pause_updates = False
self.stdscr.clear()
self.render()

Expand Down
4 changes: 3 additions & 1 deletion spotui/src/TracksMenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ def __map_tracks(self, track):
)

def handler():
self.play_track(track["uri"])
self.play_track(track)

return {
"text": track_name + " " + artist_name,
"handler": handler,
"type": track['type'],
"id": track["id"],
"highlight": highlight,
}

Expand Down
39 changes: 32 additions & 7 deletions spotui/src/spotifyApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ def get_playing(self):
except Exception as e:
pass

def search_tracks(self, query):
def search(self, query):
try:
self.auth()
if not self.token:
return False
results = self.client.search(query, 20)
tracks = results["tracks"]
items = tracks["items"]
return list(map(self.__map_tracks, items))
classes = ['track', 'show', 'playlist']
items = [self.__search_all(c, query) for c in classes]
flat_items = [item for sublist in items for item in sublist]
return list(map(self.__map_tracks, flat_items))
except Exception as e:
pass

Expand Down Expand Up @@ -135,6 +135,17 @@ def get_recently_played(self):
except Exception as e:
pass

def show_episodes(self, id):
try:
self.auth()
if not self.token:
return []
tracks = self.client.show_episodes(id)
items = tracks["items"]
return list(map(self.__map_tracks, items))
except Exception as e:
pass

def get_liked_tracks(self):
try:
self.auth()
Expand Down Expand Up @@ -205,11 +216,25 @@ def repeat(self, state):
except Exception as e:
pass

def __search_all(self, className, query):
plural = className + 's'
tracks = self.__extract_results(self.client.search(
query, 50, 0, className), plural)
return tracks

def __extract_results(self, results, key):
tracks = results[key]
items = tracks['items']
return items

def __map_tracks(self, item):
track = item["track"] if "track" in item else item
return {
"name": track["name"],
"artist": track["artists"][0]["name"],
"name": '(' + track['type'] + ') ' + track["name"] if track['type'] != 'track' else track['name'],
"artist": track["artists"][0]["name"] if track['type'] == 'track' else track['publisher'] if track['type'] == 'show' else ' ',
# "artist": 'james',
"type": track['type'],
"id": track["id"] if track['id'] else False,
"uri": track["uri"],
}

Expand Down

0 comments on commit 22c2b1c

Please sign in to comment.