My music library. see library.tsv and any folder for playlists. synced from spotify via ugly autohotkey scripts
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 221 lines 8.0 kB view raw
1#!/usr/bin/env python3 2 3import argparse 4import codecs 5import http.client 6import http.server 7import json 8import logging 9import re 10import sys 11import time 12import urllib.error 13import urllib.parse 14import urllib.request 15import webbrowser 16 17logging.basicConfig(level=20, datefmt='%I:%M:%S', format='[%(asctime)s] %(message)s') 18 19 20class SpotifyAPI: 21 22 # Requires an OAuth token. 23 def __init__(self, auth): 24 self._auth = auth 25 26 # Gets a resource from the Spotify API and returns the object. 27 def get(self, url, params={}, tries=3): 28 # Construct the correct URL. 29 if not url.startswith('https://api.spotify.com/v1/'): 30 url = 'https://api.spotify.com/v1/' + url 31 if params: 32 url += ('&' if '?' in url else '?') + urllib.parse.urlencode(params) 33 34 # Try the sending off the request a specified number of times before giving up. 35 for _ in range(tries): 36 try: 37 req = urllib.request.Request(url) 38 req.add_header('Authorization', 'Bearer ' + self._auth) 39 res = urllib.request.urlopen(req) 40 reader = codecs.getreader('utf-8') 41 return json.load(reader(res)) 42 except Exception as err: 43 logging.info('Couldn\'t load URL: {} ({})'.format(url, err)) 44 time.sleep(2) 45 logging.info('Trying again...') 46 sys.exit(1) 47 48 # The Spotify API breaks long lists into multiple pages. This method automatically 49 # fetches all pages and joins them, returning in a single list of objects. 50 def list(self, url, params={}): 51 last_log_time = time.time() 52 response = self.get(url, params) 53 items = response['items'] 54 55 while response['next']: 56 if time.time() > last_log_time + 15: 57 last_log_time = time.time() 58 logging.info(f"Loaded {len(items)}/{response['total']} items") 59 60 response = self.get(response['next']) 61 items += response['items'] 62 return items 63 64 # Pops open a browser window for a user to log in and authorize API access. 65 @staticmethod 66 def authorize(client_id, scope): 67 url = 'https://accounts.spotify.com/authorize?' + urllib.parse.urlencode({ 68 'response_type': 'token', 69 'client_id': client_id, 70 'scope': scope, 71 'redirect_uri': 'http://127.0.0.1:{}/redirect'.format(SpotifyAPI._SERVER_PORT) 72 }) 73 logging.info(f'Logging in (click if it doesn\'t open automatically): {url}') 74 webbrowser.open(url) 75 76 # Start a simple, local HTTP server to listen for the authorization token... (i.e. a hack). 77 server = SpotifyAPI._AuthorizationServer('127.0.0.1', SpotifyAPI._SERVER_PORT) 78 try: 79 while True: 80 server.handle_request() 81 except SpotifyAPI._Authorization as auth: 82 return SpotifyAPI(auth.access_token) 83 84 # The port that the local server listens on. Don't change this, 85 # as Spotify only will redirect to certain predefined URLs. 86 _SERVER_PORT = 43019 87 88 class _AuthorizationServer(http.server.HTTPServer): 89 def __init__(self, host, port): 90 http.server.HTTPServer.__init__(self, (host, port), SpotifyAPI._AuthorizationHandler) 91 92 # Disable the default error handling. 93 def handle_error(self, request, client_address): 94 raise 95 96 class _AuthorizationHandler(http.server.BaseHTTPRequestHandler): 97 def do_GET(self): 98 # The Spotify API has redirected here, but access_token is hidden in the URL fragment. 99 # Read it using JavaScript and send it to /token as an actual query string... 100 if self.path.startswith('/redirect'): 101 self.send_response(200) 102 self.send_header('Content-Type', 'text/html') 103 self.end_headers() 104 self.wfile.write(b'<script>location.replace("token?" + location.hash.slice(1));</script>') 105 106 # Read access_token and use an exception to kill the server listening... 107 elif self.path.startswith('/token?'): 108 self.send_response(200) 109 self.send_header('Content-Type', 'text/html') 110 self.end_headers() 111 self.wfile.write(b'<script>close()</script>Thanks! You may now close this window.') 112 113 access_token = re.search('access_token=([^&]*)', self.path).group(1) 114 logging.info(f'Received access token from Spotify: {access_token}') 115 raise SpotifyAPI._Authorization(access_token) 116 117 else: 118 self.send_error(404) 119 120 # Disable the default logging. 121 def log_message(self, format, *args): 122 pass 123 124 class _Authorization(Exception): 125 def __init__(self, access_token): 126 self.access_token = access_token 127 128 129def main(): 130 # Parse arguments. 131 parser = argparse.ArgumentParser(description='Exports your Spotify playlists. By default, opens a browser window ' 132 + 'to authorize the Spotify Web API, but you can also manually specify' 133 + ' an OAuth token with the --token option.') 134 parser.add_argument('--token', metavar='OAUTH_TOKEN', help='use a Spotify OAuth token (requires the ' 135 + '`playlist-read-private` permission)') 136 parser.add_argument('--dump', default='playlists', choices=['liked,playlists', 'playlists,liked', 'playlists', 'liked'], 137 help='dump playlists or liked songs, or both (default: playlists)') 138 parser.add_argument('--format', default='txt', choices=['json', 'txt'], help='output format (default: txt)') 139 parser.add_argument('file', help='output filename', nargs='?') 140 args = parser.parse_args() 141 142 # If they didn't give a filename, then just prompt them. (They probably just double-clicked.) 143 while not args.file: 144 args.file = input('Enter a file name (e.g. playlists.txt): ') 145 args.format = args.file.split('.')[-1] 146 147 # Log into the Spotify API. 148 if args.token: 149 spotify = SpotifyAPI(args.token) 150 else: 151 spotify = SpotifyAPI.authorize(client_id='5c098bcc800e45d49e476265bc9b6934', 152 scope='playlist-read-private playlist-read-collaborative user-library-read') 153 154 # Get the ID of the logged in user. 155 logging.info('Loading user info...') 156 me = spotify.get('me') 157 logging.info('Logged in as {display_name} ({id})'.format(**me)) 158 159 playlists = [] 160 liked_albums = [] 161 162 # List liked albums and songs 163 if 'liked' in args.dump: 164 logging.info('Loading liked albums and songs...') 165 liked_tracks = spotify.list('me/tracks', {'limit': 50}) 166 liked_albums = spotify.list('me/albums', {'limit': 50}) 167 playlists += [{'name': 'Liked Songs', 'tracks': liked_tracks}] 168 169 # List all playlists and the tracks in each playlist 170 if 'playlists' in args.dump: 171 logging.info('Loading playlists...') 172 playlist_data = spotify.list('users/{user_id}/playlists'.format(user_id=me['id']), {'limit': 50}) 173 logging.info(f'Found {len(playlist_data)} playlists') 174 175 # List all tracks in each playlist 176 for playlist in playlist_data: 177 logging.info('Loading playlist: {name} ({tracks[total]} songs)'.format(**playlist)) 178 playlist['tracks'] = spotify.list(playlist['tracks']['href'], {'limit': 100}) 179 playlists += playlist_data 180 181 # Write the file. 182 logging.info('Writing files...') 183 with open(args.file, 'w', encoding='utf-8') as f: 184 # JSON file. 185 if args.format == 'json': 186 json.dump({ 187 'playlists': playlists, 188 'albums': liked_albums 189 }, f) 190 191 # Tab-separated file. 192 else: 193 f.write('Playlists: \r\n\r\n') 194 for playlist in playlists: 195 f.write(playlist['name'] + '\r\n') 196 for track in playlist['tracks']: 197 if track['track'] is None: 198 continue 199 f.write('{name}\t{artists}\t{album}\t{uri}\t{release_date}\r\n'.format( 200 uri=track['track']['uri'], 201 name=track['track']['name'], 202 artists=', '.join([artist['name'] for artist in track['track']['artists']]), 203 album=track['track']['album']['name'], 204 release_date=track['track']['album']['release_date'] 205 )) 206 f.write('\r\n') 207 if len(liked_albums) > 0: 208 f.write('Liked Albums: \r\n\r\n') 209 for album in liked_albums: 210 uri = album['album']['uri'] 211 name = album['album']['name'] 212 artists = ', '.join([artist['name'] for artist in album['album']['artists']]) 213 release_date = album['album']['release_date'] 214 album = f'{artists} - {name}' 215 216 f.write(f'{name}\t{artists}\t-\t{uri}\t{release_date}\r\n') 217 218 logging.info('Wrote file: ' + args.file) 219 220if __name__ == '__main__': 221 main()