sigma67 / spotify_to_ytmusic

Clone a Spotify playlist to YouTube Music
MIT License
791 stars 65 forks source link

Support for Liked Songs #27

Closed moritonal closed 1 year ago

moritonal commented 2 years ago

Library is great, but doesn't support user's liked Songs. This is because it's currently using the Client Credentials Flow. Turns out you can support user's liked songs with only a few extra steps.

I'll outline here and try put together a PR sometime, it just requires a few changes.

def getSpotifyLikedTracks(self):

        results = []
        playlists = self.api.current_user_saved_tracks(20)
        while playlists:
            for i, playlist in enumerate(playlists['items']):

                result = build_result(playlist);

                results.append(result)

            if playlists['next']:
                playlists = self.api.next(playlists)
            else:
                playlists = None

        return results

Splitting out build_results into:

def build_results(tracks, album=None):
    results = []
    for track in tracks:

        result = build_result(track, album)

        if result is not None:
            results.append(result)

    return results

def build_result(track, album=None):
    if 'track' in track:
        track = track['track']

    album_name = album if album else track['album']['name']

    return {
        'artist': ' '.join([artist['name'] for artist in track['artists']]),
        'name': track['name'],
        'album': album_name,
        'duration': track['duration_ms']/1000
    }

And then have a func somewhere in main doing the following:

tracks = Spotify().getSpotifyLikedTracks()
videoIds = ytmusic.search_songs(tracks)
playlist_id = ytmusic.create_playlist("Liked Music", "Liked Music", "PRIVATE", videoIds)
sigma67 commented 2 years ago

Sounds reasonable, feel free to submit a PR when you get around to it.

Do you think it's possible to make using SpotifyOAuth optional since it involves an extra step?

kenjibailly commented 2 years ago

Library is great, but doesn't support user's liked Songs. This is because it's currently using the Client Credentials Flow. Turns out you can support user's liked songs with only a few extra steps.

I'll outline here and try put together a PR sometime, it just requires a few changes.

  • Add http://localhost as a redirect URL on Spotify
  • Swap out SpotifyClientCredentials for SpotifyOAuth(client_id=conf['client_id'], client_secret=conf['client_secret'], scope="user-library-read", redirect_uri="http://localhost")
  • When you login, it'll open a browser, ask the user to sign in, then you copy the URL back into console
  • Make a new func like this:
def getSpotifyLikedTracks(self):

        results = []
        playlists = self.api.current_user_saved_tracks(20)
        while playlists:
            for i, playlist in enumerate(playlists['items']):

                result = build_result(playlist);

                results.append(result)

            if playlists['next']:
                playlists = self.api.next(playlists)
            else:
                playlists = None

        return results

Splitting out build_results into:

def build_results(tracks, album=None):
    results = []
    for track in tracks:

        result = build_result(track, album)

        if result is not None:
            results.append(result)

    return results

def build_result(track, album=None):
    if 'track' in track:
        track = track['track']

    album_name = album if album else track['album']['name']

    return {
        'artist': ' '.join([artist['name'] for artist in track['artists']]),
        'name': track['name'],
        'album': album_name,
        'duration': track['duration_ms']/1000
    }

And then have a func somewhere in main doing the following:

tracks = Spotify().getSpotifyLikedTracks()
videoIds = ytmusic.search_songs(tracks)
playlist_id = ytmusic.create_playlist("Liked Music", "Liked Music", "PRIVATE", videoIds)

Any idea when you could submit this? I'm having a few issues

image

Hindook commented 1 year ago

Hello friends, any news?

sigma67 commented 1 year ago

I'm open towards somebody submitting an implementation. The SpotifyOAuth should be optional though and only used where relevant (i.e. for all transfer, liked songs).

orkonung commented 1 year ago

hi. I opened the spotify application on my pc (windows), then I selected all the liked songs with the Ctrl + A button and added them to a new playlist.