-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotipyinit.py
47 lines (38 loc) · 2.15 KB
/
spotipyinit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
import configparser
import logging
import os
import sys
import spotipy
logger = logging.getLogger('root')
def init_spotipy():
# string constant
NOT_SET_VALUE = 'NOT SET'
config_parser = configparser.ConfigParser()
config_parser.read('spotipy-config.ini')
client_id = config_parser.get('Login Parameters', 'client_id', fallback=NOT_SET_VALUE)
client_secret = config_parser.get('Login Parameters', 'client_secret', fallback=NOT_SET_VALUE)
# access_token = config_parser.get('Login Parameters', 'access_token', fallback=NOT_SET_VALUE)
user_name = config_parser.get('Login Parameters', 'user_name', fallback=NOT_SET_VALUE)
if os.environ.get("SPOTIPY_CLIENT_ID", NOT_SET_VALUE) is NOT_SET_VALUE:
if client_id is NOT_SET_VALUE:
print('ERROR: Environment variable is not set and client id not read from config file')
logger.error('Environment variable is not set and client id not read from config file')
sys.exit(1)
os.environ['SPOTIPY_CLIENT_ID'] = client_id
if os.environ.get("SPOTIPY_CLIENT_SECRET", NOT_SET_VALUE) is NOT_SET_VALUE:
if client_secret is NOT_SET_VALUE:
print('ERROR: Environment variable is not set and client secret not read from config file')
logger.error('Environment variable is not set and client secret not read from config file')
sys.exit(1)
os.environ['SPOTIPY_CLIENT_SECRET'] = client_secret
if os.environ.get("SPOTIFY_USER_NAME", NOT_SET_VALUE) is NOT_SET_VALUE:
if user_name is NOT_SET_VALUE:
print('ERROR: Environment variable is not set and user name not read from config file')
logging.error('Environment variable is not set and user name not read from config file')
sys.exit(1)
os.environ['SPOTIFY_USER_NAME'] = user_name
token = spotipy.util.prompt_for_user_token(user_name, 'user-library-read user-read-recently-played playlist-read-private playlist-read-collaborative user-top-read', client_id, client_secret, redirect_uri='https://127.0.0.1:8080')
sp = spotipy.Spotify(auth=token)
logger.debug('spotipy initialized')
return sp