Add script to copy playlists from spotify to tidal
This commit is contained in:
52
auth.py
Normal file
52
auth.py
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import spotipy
|
||||
import tidalapi
|
||||
import webbrowser
|
||||
import yaml
|
||||
|
||||
def open_spotify_session(config):
|
||||
credentials_manager = spotipy.SpotifyOAuth(username=config['username'],
|
||||
scope='playlist-read-private',
|
||||
client_id=config['client_id'],
|
||||
client_secret=config['client_secret'],
|
||||
redirect_uri=config['redirect_uri'])
|
||||
try:
|
||||
credentials_manager.get_access_token(as_dict=False)
|
||||
except spotipy.SpotifyOauthError:
|
||||
sys.exit("Error opening Spotify sesion; could not get token for username: ".format(config['username']))
|
||||
|
||||
return spotipy.Spotify(oauth_manager=credentials_manager)
|
||||
|
||||
def open_tidal_session():
|
||||
try:
|
||||
with open('.session.yml', 'r') as session_file:
|
||||
previous_session = yaml.safe_load(session_file)
|
||||
except OSError:
|
||||
previous_session = None
|
||||
|
||||
session = tidalapi.Session()
|
||||
if previous_session:
|
||||
success = session.load_oauth_session(previous_session['session_id'],
|
||||
previous_session['token_type'],
|
||||
previous_session['access_token'],
|
||||
previous_session['refresh_token'] )
|
||||
if success:
|
||||
return session
|
||||
|
||||
login, future = session.login_oauth()
|
||||
print('Login with the webbrowser: ' + login.verification_uri_complete)
|
||||
url = login.verification_uri_complete
|
||||
if not url.startswith('https://'):
|
||||
url = 'https://' + url
|
||||
webbrowser.open(url)
|
||||
future.result()
|
||||
with open('.session.yml', 'w') as f:
|
||||
yaml.dump( {'session_id': session.session_id,
|
||||
'token_type': session.token_type,
|
||||
'access_token': session.access_token,
|
||||
'refresh_token': session.refresh_token}, f )
|
||||
return session
|
||||
|
||||
|
||||
Reference in New Issue
Block a user