···11+#!/usr/bin/env python3
22+33+import argparse
44+import codecs
55+import http.client
66+import http.server
77+import json
88+import logging
99+import re
1010+import sys
1111+import time
1212+import urllib.error
1313+import urllib.parse
1414+import urllib.request
1515+import webbrowser
1616+1717+logging.basicConfig(level=20, datefmt='%I:%M:%S', format='[%(asctime)s] %(message)s')
1818+1919+2020+class SpotifyAPI:
2121+2222+ # Requires an OAuth token.
2323+ def __init__(self, auth):
2424+ self._auth = auth
2525+2626+ # Gets a resource from the Spotify API and returns the object.
2727+ def get(self, url, params={}, tries=3):
2828+ # Construct the correct URL.
2929+ if not url.startswith('https://api.spotify.com/v1/'):
3030+ url = 'https://api.spotify.com/v1/' + url
3131+ if params:
3232+ url += ('&' if '?' in url else '?') + urllib.parse.urlencode(params)
3333+3434+ # Try the sending off the request a specified number of times before giving up.
3535+ for _ in range(tries):
3636+ try:
3737+ req = urllib.request.Request(url)
3838+ req.add_header('Authorization', 'Bearer ' + self._auth)
3939+ res = urllib.request.urlopen(req)
4040+ reader = codecs.getreader('utf-8')
4141+ return json.load(reader(res))
4242+ except Exception as err:
4343+ logging.info('Couldn\'t load URL: {} ({})'.format(url, err))
4444+ time.sleep(2)
4545+ logging.info('Trying again...')
4646+ sys.exit(1)
4747+4848+ # The Spotify API breaks long lists into multiple pages. This method automatically
4949+ # fetches all pages and joins them, returning in a single list of objects.
5050+ def list(self, url, params={}):
5151+ last_log_time = time.time()
5252+ response = self.get(url, params)
5353+ items = response['items']
5454+5555+ while response['next']:
5656+ if time.time() > last_log_time + 15:
5757+ last_log_time = time.time()
5858+ logging.info(f"Loaded {len(items)}/{response['total']} items")
5959+6060+ response = self.get(response['next'])
6161+ items += response['items']
6262+ return items
6363+6464+ # Pops open a browser window for a user to log in and authorize API access.
6565+ @staticmethod
6666+ def authorize(client_id, scope):
6767+ url = 'https://accounts.spotify.com/authorize?' + urllib.parse.urlencode({
6868+ 'response_type': 'token',
6969+ 'client_id': client_id,
7070+ 'scope': scope,
7171+ 'redirect_uri': 'http://127.0.0.1:{}/redirect'.format(SpotifyAPI._SERVER_PORT)
7272+ })
7373+ logging.info(f'Logging in (click if it doesn\'t open automatically): {url}')
7474+ webbrowser.open(url)
7575+7676+ # Start a simple, local HTTP server to listen for the authorization token... (i.e. a hack).
7777+ server = SpotifyAPI._AuthorizationServer('127.0.0.1', SpotifyAPI._SERVER_PORT)
7878+ try:
7979+ while True:
8080+ server.handle_request()
8181+ except SpotifyAPI._Authorization as auth:
8282+ return SpotifyAPI(auth.access_token)
8383+8484+ # The port that the local server listens on. Don't change this,
8585+ # as Spotify only will redirect to certain predefined URLs.
8686+ _SERVER_PORT = 43019
8787+8888+ class _AuthorizationServer(http.server.HTTPServer):
8989+ def __init__(self, host, port):
9090+ http.server.HTTPServer.__init__(self, (host, port), SpotifyAPI._AuthorizationHandler)
9191+9292+ # Disable the default error handling.
9393+ def handle_error(self, request, client_address):
9494+ raise
9595+9696+ class _AuthorizationHandler(http.server.BaseHTTPRequestHandler):
9797+ def do_GET(self):
9898+ # The Spotify API has redirected here, but access_token is hidden in the URL fragment.
9999+ # Read it using JavaScript and send it to /token as an actual query string...
100100+ if self.path.startswith('/redirect'):
101101+ self.send_response(200)
102102+ self.send_header('Content-Type', 'text/html')
103103+ self.end_headers()
104104+ self.wfile.write(b'<script>location.replace("token?" + location.hash.slice(1));</script>')
105105+106106+ # Read access_token and use an exception to kill the server listening...
107107+ elif self.path.startswith('/token?'):
108108+ self.send_response(200)
109109+ self.send_header('Content-Type', 'text/html')
110110+ self.end_headers()
111111+ self.wfile.write(b'<script>close()</script>Thanks! You may now close this window.')
112112+113113+ access_token = re.search('access_token=([^&]*)', self.path).group(1)
114114+ logging.info(f'Received access token from Spotify: {access_token}')
115115+ raise SpotifyAPI._Authorization(access_token)
116116+117117+ else:
118118+ self.send_error(404)
119119+120120+ # Disable the default logging.
121121+ def log_message(self, format, *args):
122122+ pass
123123+124124+ class _Authorization(Exception):
125125+ def __init__(self, access_token):
126126+ self.access_token = access_token
127127+128128+129129+def main():
130130+ # Parse arguments.
131131+ parser = argparse.ArgumentParser(description='Exports your Spotify playlists. By default, opens a browser window '
132132+ + 'to authorize the Spotify Web API, but you can also manually specify'
133133+ + ' an OAuth token with the --token option.')
134134+ parser.add_argument('--token', metavar='OAUTH_TOKEN', help='use a Spotify OAuth token (requires the '
135135+ + '`playlist-read-private` permission)')
136136+ parser.add_argument('--dump', default='playlists', choices=['liked,playlists', 'playlists,liked', 'playlists', 'liked'],
137137+ help='dump playlists or liked songs, or both (default: playlists)')
138138+ parser.add_argument('--format', default='txt', choices=['json', 'txt'], help='output format (default: txt)')
139139+ parser.add_argument('file', help='output filename', nargs='?')
140140+ args = parser.parse_args()
141141+142142+ # If they didn't give a filename, then just prompt them. (They probably just double-clicked.)
143143+ while not args.file:
144144+ args.file = input('Enter a file name (e.g. playlists.txt): ')
145145+ args.format = args.file.split('.')[-1]
146146+147147+ # Log into the Spotify API.
148148+ if args.token:
149149+ spotify = SpotifyAPI(args.token)
150150+ else:
151151+ spotify = SpotifyAPI.authorize(client_id='5c098bcc800e45d49e476265bc9b6934',
152152+ scope='playlist-read-private playlist-read-collaborative user-library-read')
153153+154154+ # Get the ID of the logged in user.
155155+ logging.info('Loading user info...')
156156+ me = spotify.get('me')
157157+ logging.info('Logged in as {display_name} ({id})'.format(**me))
158158+159159+ playlists = []
160160+ liked_albums = []
161161+162162+ # List liked albums and songs
163163+ if 'liked' in args.dump:
164164+ logging.info('Loading liked albums and songs...')
165165+ liked_tracks = spotify.list('me/tracks', {'limit': 50})
166166+ liked_albums = spotify.list('me/albums', {'limit': 50})
167167+ playlists += [{'name': 'Liked Songs', 'tracks': liked_tracks}]
168168+169169+ # List all playlists and the tracks in each playlist
170170+ if 'playlists' in args.dump:
171171+ logging.info('Loading playlists...')
172172+ playlist_data = spotify.list('users/{user_id}/playlists'.format(user_id=me['id']), {'limit': 50})
173173+ logging.info(f'Found {len(playlist_data)} playlists')
174174+175175+ # List all tracks in each playlist
176176+ for playlist in playlist_data:
177177+ logging.info('Loading playlist: {name} ({tracks[total]} songs)'.format(**playlist))
178178+ playlist['tracks'] = spotify.list(playlist['tracks']['href'], {'limit': 100})
179179+ playlists += playlist_data
180180+181181+ # Write the file.
182182+ logging.info('Writing files...')
183183+ with open(args.file, 'w', encoding='utf-8') as f:
184184+ # JSON file.
185185+ if args.format == 'json':
186186+ json.dump({
187187+ 'playlists': playlists,
188188+ 'albums': liked_albums
189189+ }, f)
190190+191191+ # Tab-separated file.
192192+ else:
193193+ f.write('Playlists: \r\n\r\n')
194194+ for playlist in playlists:
195195+ f.write(playlist['name'] + '\r\n')
196196+ for track in playlist['tracks']:
197197+ if track['track'] is None:
198198+ continue
199199+ f.write('{name}\t{artists}\t{album}\t{uri}\t{release_date}\r\n'.format(
200200+ uri=track['track']['uri'],
201201+ name=track['track']['name'],
202202+ artists=', '.join([artist['name'] for artist in track['track']['artists']]),
203203+ album=track['track']['album']['name'],
204204+ release_date=track['track']['album']['release_date']
205205+ ))
206206+ f.write('\r\n')
207207+ if len(liked_albums) > 0:
208208+ f.write('Liked Albums: \r\n\r\n')
209209+ for album in liked_albums:
210210+ uri = album['album']['uri']
211211+ name = album['album']['name']
212212+ artists = ', '.join([artist['name'] for artist in album['album']['artists']])
213213+ release_date = album['album']['release_date']
214214+ album = f'{artists} - {name}'
215215+216216+ f.write(f'{name}\t{artists}\t-\t{uri}\t{release_date}\r\n')
217217+218218+ logging.info('Wrote file: ' + args.file)
219219+220220+if __name__ == '__main__':
221221+ main()