Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TODO: update this title (some misc bug fixes) #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/packages/music_api_clients/models/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def __init__(self, name, description, tracks_fetcher, spotify_id=None):
self.description = description
self.tracks_fetcher = tracks_fetcher
self.spotify_id = spotify_id
self.tracks = None
self._tracks = None
self.num_tracks = None

def __key(self):
Expand All @@ -21,15 +21,15 @@ def __eq__(self, other):
return NotImplemented

def get_tracks(self):
if self.tracks is None:
self.tracks = self.tracks_fetcher()
return self.tracks
if self._tracks is None:
self._tracks = self.tracks_fetcher()
return self._tracks

def get_num_tracks(self):
if self.num_tracks is None:
if self.tracks is None:
self.tracks = self.tracks_fetcher()
self.num_tracks = len(self.tracks)
if self._tracks is None:
self._tracks = self.tracks_fetcher()
self.num_tracks = len(self._tracks)
return self.num_tracks

def has_any_tracks_from_album(self, album):
Expand Down
18 changes: 15 additions & 3 deletions src/packages/music_api_clients/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ def get_matching_albums(self, album_name):
albums = self._get_albums_from_ids(matching_album_ids)
return set(albums)

def get_all_user_playlists(self, user_id):
def get_playlist_tracks(spotify_playlist_id):
return lambda: self._get_playlist_tracks(spotify_playlist_id)
results = self.client.user_playlists(user_id)
return [
Playlist.from_spotify_playlist_search_results(
playlist, get_playlist_tracks(playlist['id']))
for playlist in results['items']
]

def get_current_user_playlist_by_name(self, name):
playlist_id = self.find_current_user_playlist(name)
if playlist_id is None:
Expand Down Expand Up @@ -320,13 +330,15 @@ def _fetch_until_all_items_returned(self, fetch_func):
(List): all fetched items.
"""
all_items, finished = fetch_func()
all_items = set(all_items)
# transforming list to a set is losing the order
# TODO: find another way to remove duplicates
#all_items = set(all_items)
offset, batch_size = 0, API_BATCH_SIZE
while not finished:
offset += batch_size
items, finished = fetch_func(batch_size=batch_size, offset=offset)
all_items |= set(items)
return list(all_items)
all_items.extend(items)
return all_items

def _fetch_in_batches(self, items_to_fetch, fetch_items):
"""
Expand Down
31 changes: 31 additions & 0 deletions src/scripts/duplicate_all_user_playlists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# allows me to run:
# $ python scripts/playlist_updater_example.py
import sys

sys.path.extend(['.', '../'])

from packages.music_management.music_util import MusicUtil
from packages.music_management.my_music_lib import MyMusicLib
from packages.music_api_clients.spotify import Spotify

def main():
spotify = Spotify()
playlists = spotify.get_all_user_playlists("2222rxc7a5xacexelkeqbrony")
if (len(playlists) == 0):
print("No playlists found")
return

music_util = MusicUtil(spotify, print)
my_music_lib = MyMusicLib(spotify, music_util, print)
for playlist in playlists:
new_playlist_name = f"Jaime: {playlist.name}"
print(f"Creating playlist: \"{new_playlist_name}\", containing ", len(playlist.get_tracks()), " tracks")
my_music_lib.create_playlist(
new_playlist_name,
playlist.get_tracks(),
playlist.description
)


if __name__ == "__main__":
main()