Skip to content

Commit

Permalink
add oauth authentification
Browse files Browse the repository at this point in the history
  • Loading branch information
webns authored and sigma67 committed May 29, 2023
1 parent 334b21f commit 80ef9a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions spotify_to_ytmusic/settings.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ user_id =
[spotify]
client_id = id_from_developer_console
client_secret = secret_from_developer_console
use_oauth = no
1 change: 1 addition & 0 deletions spotify_to_ytmusic/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def setup_spotify():
"Paste your client id from the Spotify developer dashboard:"
),
"client_secret": input("Paste your client secret from the Spotify developer dashboard:"),
"use_oauth": input("Use OAuth method for authorization (required for transfer liked songs) yes/no:"),
}
settings["spotify"].update(credentials)
settings.save()
Expand Down
14 changes: 10 additions & 4 deletions spotify_to_ytmusic/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from spotipy.oauth2 import SpotifyOAuth

from spotify_to_ytmusic.settings import Settings

Expand All @@ -22,10 +23,15 @@ def __init__(self):
string.hexdigits
), f"Spotify client_secret not set or invalid: {client_secret}"

client_credentials_manager = SpotifyClientCredentials(
client_id=client_id, client_secret=client_secret
)
self.api = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
use_oauth = conf["use_oauth"] == "y" or conf["use_oauth"] == "yes"
if use_oauth:
auth = SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri="http://localhost", scope="user-library-read")
self.api = spotipy.Spotify(auth_manager=auth)
else:
client_credentials_manager = SpotifyClientCredentials(
client_id=client_id, client_secret=client_secret
)
self.api = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

def getSpotifyPlaylist(self, url):
playlistId = get_id_from_url(url)
Expand Down

0 comments on commit 80ef9a9

Please sign in to comment.