-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
68 lines (45 loc) · 1.95 KB
/
setup.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import argparse
from utils.constants import ARG_DESCS, SCOPE, REDIRECT_URI, TOKENS_FILENAME
from utils.logging import get_logger, enable_debug_logging
from utils.auth import authorize
from utils.cache import CacheArgHandler
from utils.validation import assert_valid_client_id, assert_valid_client_secret
SCRIPT_NAME = 'setup.py'
SCRIPT_DESC = 'Grant access to your Spotify account and save the access and refresh tokens to a file.'
logger = get_logger()
# Save tokens to a file
def save_tokens_to_file(token_info):
if not token_info:
raise ValueError('Something went wrong. Expected token info to be defined but it was not')
try:
file = open(TOKENS_FILENAME, 'w')
file.write(f'SPOTIFY_ACCESS_TOKEN: {token_info["access_token"]}\nSPOTIFY_REFRESH_TOKEN: {token_info["refresh_token"]}')
file.close()
except IOError:
logger.warning(f'Couldn\'t write tokens to {TOKENS_FILENAME}')
logger.info(f'Tokens were saved to {TOKENS_FILENAME}. Do not push this file to GitHub')
# Parse command line arguments
def parse_args():
logger.debug('Parsing arguments')
parser = argparse.ArgumentParser(prog=SCRIPT_NAME, description=SCRIPT_DESC)
parser.add_argument('client_id', help=ARG_DESCS['client_id'])
parser.add_argument('client_secret', help=ARG_DESCS['client_secret'])
parser.add_argument('--debug', '-d', help=ARG_DESCS['debug'], default=False, action='store_true')
args = parser.parse_args()
if args.debug:
enable_debug_logging(logger)
assert_valid_client_id(args.client_id)
assert_valid_client_secret(args.client_secret)
return args
# Script entry point
def main():
logger.debug('Script started')
args = parse_args()
cache_handler = CacheArgHandler(SCOPE)
logger.info('Opening browser to authorize with Spotify')
sp = authorize(SCOPE, args.client_id, args.client_secret, REDIRECT_URI, cache_handler, True)
# Dummy API call to force token refresh
sp.me()['id']
save_tokens_to_file(cache_handler.get_cached_token())
logger.debug('Script finished')
main()