Skip to content

Commit

Permalink
finish spotify impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ancientjpeg committed Mar 24, 2024
1 parent 34bb0e3 commit 14e5221
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/mussty/helpers/paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def get_records(self: Paginator, get_records_list, limit, total) -> list:
task_results.append(task_result)

# apple music does not like being flooded with a bunch of requests at once
await asyncio.sleep(0.01)
await asyncio.sleep(0.05)

except* Exception as e:
print(e.exceptions)
Expand Down
9 changes: 7 additions & 2 deletions src/mussty/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ def add_playlist(self, playlist: Playlist):

def uncache_self(self):
if not self.cachefile.exists():
print(f"No cache exists for service of type {self.__class__.__name__}")
print(f"Cachefile does not exist.")
return False

with open(self.cachefile) as f:
data = json.load(f)
data = data[self.json_tagname]

try:
data = data[self.json_tagname]
except KeyError:
print(f"No cache exists for service of type {self.__class__.__name__}")
return False

try:
self.songs = {
Expand Down
40 changes: 34 additions & 6 deletions src/mussty/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ def __init__(self) -> None:
self.refresh_token: str = ""
self.user_id: str = ""

self.CACHE = False # TODO delete and make this configurable

has_tokens = True
try:
self.access_token = secrets.get()["spotify"]["access_token"]
Expand Down Expand Up @@ -250,14 +248,44 @@ async def get_playlist_page(offset: int, paginator: Paginator):
id = playlist["id"]
title = playlist["name"]

playlists.append(Playlist(id, title, []))
# TODO this empty list way of doing things is hacky, please make it cleaner
playlists.append(
Playlist(id, title, [] * playlist["tracks"]["total"])
)

return playlists

playlists = Paginator(get_playlist_page, limit, total)
print(playlists.records)
def get_songs_for_playlist_by_id(pl: Playlist):
async def get_songs_for_playlist(offset: int, paginator: Paginator):
playlist_tracks_url = self.api_url_base() + f"/playlists/{pl.id}/tracks"

playlist_tracks_page = []
params = {"limit": limit, "offset": offset}
async with paginator.session.get(
playlist_tracks_url, headers=self.auth_headers(), params=params
) as res:
body = await res.json()
for item in body["items"]:
track = item["track"]
isrc = track["external_ids"]["isrc"]
title = track["name"]

playlist_tracks_page.append(Song(isrc, title, ""))

return playlist_tracks_page

return get_songs_for_playlist

playlists_paged = Paginator(get_playlist_page, limit, total)
for playlist in playlists_paged:
track_count = len(playlist.songs)

playlist_tracks_paged = Paginator(
get_songs_for_playlist_by_id(playlist), total=track_count, limit=limit
)

playlist.songs = playlist_tracks_paged.records

for playlist in playlists:
self.add_playlist(playlist)

def auth_headers(self):
Expand Down

0 comments on commit 14e5221

Please sign in to comment.