Skip to content

Commit

Permalink
add command for transfer liked songs
Browse files Browse the repository at this point in the history
  • Loading branch information
webns authored and sigma67 committed May 29, 2023
1 parent 80ef9a9 commit 0cd6f38
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
19 changes: 19 additions & 0 deletions spotify_to_ytmusic/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ def all(args):
except Exception as ex:
print(f"Could not transfer playlist {p['name']}. {str(ex)}")

def liked(args):
spotify, ytmusic = _init()
tracks = spotify.getLikedTracks()
if len(tracks) == 0:
exit("Could not find any liked tracks for the user.")

print(str(len(tracks)) + " tracks found. Creating playlist...")
try:
videos_ids = ytmusic.search_songs(tracks)
playlist_id = ytmusic.create_playlist(
"Liked songs (Spotify)",
"your liked tracks from spotify",
"PRIVATE",
videos_ids,
)
print(playlist_id)
except Exception as ex:
print("Could not transfer liked songs. " + str(ex))


def create(args):
spotify, ytmusic = _init()
Expand Down
5 changes: 5 additions & 0 deletions spotify_to_ytmusic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def get_args(args=None):
all_parser.add_argument("user", type=str, help="Spotify userid of the specified user.")
all_parser.set_defaults(func=controllers.all)

liked_parser = subparsers.add_parser(
"liked", help="Transfer all liked songs of the user."
)
liked_parser.set_defaults(func=controllers.liked)

return parser.parse_args(args)


Expand Down
9 changes: 9 additions & 0 deletions spotify_to_ytmusic/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def getUserPlaylists(self, user):

return [p for p in pl if p["owner"]["id"] == user and p["tracks"]["total"] > 0]

def getLikedTracks(self):
response = self.api.current_user_saved_tracks(limit=50)
tracks = response["items"]
while response["next"] != None:
response = self.api.current_user_saved_tracks(limit=50, offset=response["offset"] + 50)
tracks.extend(response["items"])

return build_results(tracks)


def build_results(tracks, album=None):
results = []
Expand Down

0 comments on commit 0cd6f38

Please sign in to comment.