def get_features(auth, tracks): conf = read_config() url = f'{conf.base_url}/audio-features' headers = {'Authorization': f'Bearer {auth.access_token}'} # tracks may have more than 100 items tracks_list = [tracks[i * 100:(i + 1) * 100] for i in range((len(tracks) + 99) // 100)] results = [] for sub_list in tracks_list: params = {'ids': ','.join(sub_list)} response = requests.get(url, headers=headers, params=params) if response.status_code != 200: print(f'status code is: {response.status_code}') print(response.text) raise BadRequestError() results.append(json.loads(response.text)) return results
def _authorization_code_request(auth_code): config = read_config() auth_key = get_auth_key(config.client_id, config.client_secret) headers = { 'Authorization': f'Basic {auth_key}', } options = { 'code': auth_code, 'redirect_uri': 'http://localhost:3000/callback', 'grant_type': 'authorization_code', 'json': True } response = requests.post(config.access_token_url, headers=headers, data=options) content = json.loads(response.content.decode('utf-8')) if response.status_code == 400: error_description = content.get('error_description', '') raise BadRequestError(error_description) access_token = content.get('access_token', None) token_type = content.get('token_type', None) expires_in = content.get('expires_in', None) scope = content.get('scope', None) refresh_token = content.get('refresh_token', None) return Authorization(access_token, token_type, expires_in, scope, refresh_token)
def __init__(self, date): self._conf = read_config() self._auth = authenticate(self._conf) self.dict = defaultdict(int) self.date = date self.artists = defaultdict(int) self.meta_data = () self.total_plays = 0
def delete_playlist(auth, playlist_id): conf = read_config() url = f'{conf.base_url}/playlists/{playlist_id}/followers' headers = {'Authorization': f'Bearer {auth.access_token}'} response = requests.delete(url, headers=headers) if response.status_code != 200: raise BadRequestError()
def populate_playlist(auth, playlistid, tracks): conf = read_config() url = f'{conf.base_url}/playlists/{playlistid}/tracks' headers = {'Authorization': f'Bearer {auth.access_token}'} data = {'uris': tracks} response = requests.post(url, headers=headers, json=data) content = json.loads(response.content.decode('utf-8')) if response.status_code != 201: error_description = content.get('error_description', None) raise BadRequestError(error_description)
def get_tracks_from_playlist(auth, playlist_id, offset=0): conf = read_config() url = f'{conf.base_url}/playlists/{playlist_id}/tracks' headers = {'Authorization': f'Bearer {auth.access_token}'} params = {'offset': offset} response = requests.get(url, headers=headers, params=params) if response.status_code != 200: print(response.status_code) print(response.text) raise BadRequestError() results = json.loads(response.text) return results
def create_playlist(auth, userid, playlist_name, desc): conf = read_config() url = f'{conf.base_url}/users/{userid}/playlists' headers = {'Authorization': f'Bearer {auth.access_token}', 'Content-Type': 'application/json'} data = {'name': playlist_name, 'public': False, 'description': desc} response = requests.post(url, headers=headers, json=data) if response.status_code != 200 and response.status_code != 201: raise BadRequestError() content = json.loads(response.text) return content["id"]
def home(): config = read_config() params = { 'client_id': config.client_id, 'response_type': 'code', 'redirect_uri': 'http://localhost:3000/callback', 'scope': 'user-read-private playlist-modify-private user-top-read ' 'user-read-recently-played', } enc_params = urlencode(params) url = f'{config.auth_url}?{enc_params}' return render_template('index.html', link=url)
def find_track(auth, track, artist): conf = read_config() url = f'{conf.base_url}/search?q="{artist}"%20{track}&type=track' headers = {'Authorization': f'Bearer {auth.access_token}'} response = requests.get(url, headers=headers) results = json.loads(response.text) # find all important information result_track = (results["tracks"]["items"][0]["name"]) result_artist = (results["tracks"]["items"][0]["artists"][0]["name"]) result_uri = (results["tracks"]["items"][0]["uri"]) # if the first result ain't it, it's probably the second result if artist != result_artist or track.lower() != result_track.lower(): result_track = (results["tracks"]["items"][1]["name"]) result_artist = (results["tracks"]["items"][1]["artists"][0]["name"]) result_uri = (results["tracks"]["items"][1]["uri"]) return result_track, result_artist, result_uri
def find_top_tracks(auth): conf = read_config() url = f'{conf.base_url}/me/top/tracks' headers = {'Authorization': f'Bearer {auth.access_token}'} data = {'limit': 50, 'time_range': 'short_term'} response = requests.get(url, headers=headers, params=data) if response.status_code != 200: print(response.status_code) raise BadRequestError() result = json.loads(response.text) tracks_list = [] for i in range(50): uri = result["items"][i]["uri"] tracks_list.append(uri) return tracks_list
def get_all_playlists(auth): conf = read_config() url = f'{conf.base_url}/me/playlists' headers = {'Authorization': f'Bearer {auth.access_token}'} # max 50 playlists params = {'limit': 50} response = requests.get(url, headers=headers, params=params) if response.status_code != 200: print(response.status_code) print(response.text) raise BadRequestError() with open(f'./monthlify/data/playlists.json', mode='w', encoding='utf-8') as file: file.write(response.text) results = json.loads(response.text) return results
def get_recently_played(auth, last_scraped_ms=0): conf = read_config() url = f'{conf.base_url}/me/player/recently-played' headers = {'Authorization': f'Bearer {auth.access_token}'} print(f'last scraped ms: {last_scraped_ms}') params = {'limit': 50, 'after': last_scraped_ms} response = requests.get(url, headers=headers, params=params) if response.status_code != 200: print(response.status_code) print(response.text) raise BadRequestError() now = datetime.datetime.now() - datetime.timedelta(hours=-4) with open(f'./play_log/raw/{now}.json', mode='w', encoding='utf-8') as file: file.write(response.text) print("raw file written") return f'{now}.json'
def __init__(self): self._conf = read_config() self._auth = authenticate(self._conf)
def __init__(self, username): self._conf = read_config() self._auth = authenticate(self._conf) self.user_name = username