From 37f4c812901ebd50aaa3a3103f27009c018d1723 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 13 Sep 2021 23:43:04 -0400 Subject: [PATCH] Support library export --- spotify_actions/playlist.py | 12 --------- spotify_actions/user.py | 50 +++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/spotify_actions/playlist.py b/spotify_actions/playlist.py index 402affd..7ac8bda 100644 --- a/spotify_actions/playlist.py +++ b/spotify_actions/playlist.py @@ -20,18 +20,6 @@ def playlist_current_user_create(client: Spotify, user_id: str, name: str) -> Si return SimplifiedPlaylist(**client.user_playlist_create(user_id, name)) -def playlist_current_user_all(client: Spotify) -> Iterable[SimplifiedPlaylist]: - """ - Get all playlists that belong to the current user - """ - - def _playlists(limit: int, offset: int) -> Paging: - return Paging(**client.current_user_playlists(limit, offset)) - - for playlist in exhaust(_playlists): - yield SimplifiedPlaylist(**playlist) - - def playlist_current_user_find(playlists: Iterable[SimplifiedPlaylist], name: str) -> Iterable[str]: """ Find all playlists for the current user that match a specific name diff --git a/spotify_actions/user.py b/spotify_actions/user.py index 7b1a30c..6ee1979 100644 --- a/spotify_actions/user.py +++ b/spotify_actions/user.py @@ -4,10 +4,18 @@ Actions related to user information from argparse import ArgumentParser from typing import Iterable, Optional -from spotify_model import Artist, CursorPaging, PublicUser +from spotify_model import ( + Artist, + CursorPaging, + Paging, + PublicUser, + SavedAlbum, + SavedTrack, + SimplifiedPlaylist, +) from spotipy import Spotify -from .util import exhaust_cursor, read_credentials_oauth +from .util import exhaust, exhaust_cursor, read_credentials_oauth def current_user(client: Spotify) -> PublicUser: @@ -25,6 +33,44 @@ def current_user(client: Spotify) -> PublicUser: return PublicUser(**client.current_user()) +def current_user_library_albums(client: Spotify) -> Iterable[SavedAlbum]: + """ + Get the albums a user has saved to their library + + https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-users-saved-albums + """ + + def _library_albums(limit: int, offset: int) -> Paging: + return Paging(**client.current_user_saved_albums(limit, offset)) + + for album in exhaust(_library_albums): + yield SavedAlbum(**album) + + +def current_user_library_tracks(client: Spotify) -> Iterable[SavedTrack]: + """ + https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-users-saved-tracks + """ + + def _library_tracks(limit: int, offset: int) -> Paging: + return Paging(**client.current_user_saved_tracks(limit, offset)) + + for track in exhaust(_library_tracks): + yield SavedTrack(**track) + + +def current_user_playlists(client: Spotify) -> Iterable[SimplifiedPlaylist]: + """ + Get all playlists that belong to the current user + """ + + def _playlists(limit: int, offset: int) -> Paging: + return Paging(**client.current_user_playlists(limit, offset)) + + for playlist in exhaust(_playlists): + yield SimplifiedPlaylist(**playlist) + + def current_user_followed_artists(client: Spotify) -> Iterable[Artist]: """ Required scopes: