Skip to content

Commit

Permalink
all-user-followed-artists
Browse files Browse the repository at this point in the history
  • Loading branch information
xnetcat committed Sep 1, 2023
1 parent ce2bce6 commit f9f110f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
37 changes: 37 additions & 0 deletions spotdl/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ def get_simple_songs(
lists.append(Saved.from_url(request, fetch_songs=False))
elif request == "all-user-playlists":
lists.extend(get_all_user_playlists())
elif request == "all-user-followed-artists":
lists.extend(get_user_followed_artists())
elif request.endswith(".spotdl"):
with open(request, "r", encoding="utf-8") as save_file:
for track in json.load(save_file):
Expand All @@ -268,6 +270,7 @@ def get_simple_songs(
)

for index, song in enumerate(song_list.songs):
print(index)
song_data = song.json
song_data["list_name"] = song_list.name
song_data["list_url"] = song_list.url
Expand Down Expand Up @@ -344,6 +347,40 @@ def get_all_user_playlists() -> List[Playlist]:
]


def get_user_followed_artists() -> List[Artist]:
"""
Get all user playlists
### Returns
- List of all user playlists
"""

spotify_client = SpotifyClient()
if spotify_client.user_auth is False: # type: ignore
raise SpotifyError("You must be logged in to use this function")

user_followed_response = spotify_client.current_user_followed_artists()
if user_followed_response is None:
raise SpotifyError("Couldn't get user followed artists")

user_followed_response = user_followed_response["artists"]
user_followed = user_followed_response["items"]

# Fetch all artists
while user_followed_response and user_followed_response["next"]:
response = spotify_client.next(user_followed_response)
if response is None:
break

user_followed_response = response["artists"]
user_followed.extend(user_followed_response["items"])

return [
Artist.from_url(followed_artist["external_urls"]["spotify"], fetch_songs=False)
for followed_artist in user_followed
]


def reinit_song(song: Song) -> Song:
"""
Update song object with new data
Expand Down
4 changes: 2 additions & 2 deletions spotdl/utils/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def init( # pylint: disable=bad-mcs-method-argument
client_id=client_id,
client_secret=client_secret,
redirect_uri="http://127.0.0.1:8080/",
scope="user-library-read",
scope="user-library-read,user-follow-read",
cache_handler=cache_handler,
open_browser=not headless,
)
Expand Down Expand Up @@ -200,7 +200,7 @@ def _get(self, url, args=None, payload=None, **kwargs):
raise exc

if use_cache and cache_key is not None:
logger.debug("Adding song to cache... (%s)", cache_key)
logger.debug("Adding response to cache (%s)", url)
self.cache[cache_key] = response

return response
Expand Down

0 comments on commit f9f110f

Please sign in to comment.