def post(self): result = [] header = get_auth_header() r = requests.get('https://api.spotify.com/v1/browse/new-releases', headers=header) if r.status_code == 401: header = get_auth_header() r = requests.get('https://api.spotify.com/v1/browse/new-releases', headers=header) if r.status_code == 200: try: data = r.json()['albums']['items'] result = get_albums(data) while r.json()['albums']['next']: header = get_auth_header() r = requests.get(r.json()['albums']['next'], headers=header) data = r.json()['albums']['items'] result.extend(get_albums(data)) back_response = requests.post('https://mastersound-backend.azurewebsites.net/api/albums/new-releases', json=result) if back_response.status_code == 201: return {'msg': 'Success.'}, 201 else: raise AppErrorBaseClass('There was an error sending the json to the backend API.') except Exception as e: print(e) raise AppErrorBaseClass(f'There was an error fetching the albums. Error: {e}') else: return jsonify({'msg': f'Could not get the albums. Try again. Code: {r.status_code}'})
def post(self): try: data = request.get_json() except Exception as e: raise AppErrorBaseClass('Check the json syntax.') error = {'msg': 'Email or password invalid'} try: user = User.simple_filter(email=data['email'])[0] except IndexError as e: return error, 200 authorized = check_password_hash(user.password, data['password']) if not authorized: print('not authorized') return error, 200 expires = timedelta(days=7) access_token = create_access_token(identity=user.user_id, expires_delta=expires) return { 'access_token': access_token, 'token_type': 'Bearer', 'expires_in': str(expires) }, 200
def post(self): data = request.get_json() for _json in data: if Album.simple_filter(spt_album_id=_json['spt_album_id']): print('Already on the database.') continue album = Album(cover_image_url=_json['cover_image_url'], spt_album_id=_json['spt_album_id'], album_name=_json['album_name']) for artist in _json['artists']: try: new_artist = Artist.simple_filter( spt_artist_id=artist['spt_artist_id']) if new_artist: new_artist = new_artist[0] album.artists.append(new_artist) else: new_artist = Artist( spt_artist_id=artist['spt_artist_id'], artist_name=artist['artist_name'], cover_image_url=artist['cover_image_url']) album.artists.append(new_artist) except Exception as e: print(e) raise AppErrorBaseClass(f'Error: {e}') for song in _json['songs']: if Song.simple_filter(spt_song_id=song['spt_song_id']): continue try: new_song = Song(spt_song_id=song['spt_song_id'], song_name=song['song_name'], duration=song['duration'], order_number=song['order_number'], sound_url=song['sound_url']) # Hold album.songs.append(new_song) except Exception as e: print(e) raise AppErrorBaseClass(f'Error saving song. {e}') print(album) try: album.save() except Exception as e: print(e) raise AppErrorBaseClass(e) return {'msg': 'Your albums were saved succesfully.'}, 201
def post(self, playlist_id): playlist = Playlist.get_by_id(playlist_id) if not playlist: raise ObjectNotFound('There was no playlist for the requested id.') data = request.get_json() song = Song.get_by_id(data['song_id']) try: playlist.songs.append(song) playlist.save() except Exception as e: raise AppErrorBaseClass(f'There was an error while saving the playlist. {e}') result = playlist_schema.dump(playlist) return result, 201
def post(self, user_id): user = User.get_by_id(user_id) if not user: raise ObjectNotFound('There was no user for the requested id.') data = request.get_json() playlist = Playlist(**data) try: user.playlists.append(playlist) user.save() except Exception as e: raise AppErrorBaseClass('There was an error while saving the playlist.') result = playlist_schema.dump(playlist) print(playlist_schema.dump(user.playlists, many=True)) return result, 201
def get_token(): BASE_TOKEN_URL = 'https://accounts.spotify.com/api/token' CLIENT_ID = 'aba92b636b61480c992f35aa022405f7' CLIENT_SECRET = '1d4db40d8e304d43bce78d5bea3d9751' client_str = f'{CLIENT_ID}:{CLIENT_SECRET}' client_encode = base64.b64encode(client_str.encode('utf8')).decode() client_encode = str(client_encode, 'utf8') params = {'grant_type': 'client_credentials'} headers = {'Authorization': f'Basic {client_encode}'} r = requests.post(BASE_TOKEN_URL, data=params, headers=headers) if r.status_code == 200: os.environ['SPOTIFY_TOKEN'] = r.json()['access_token'] return else: raise AppErrorBaseClass('There was an error calling Spotify token.')
def post(self, user_id): playlist = Playlist.simple_filter(user_id=user_id, favourite=1)[0] if not playlist: raise ObjectNotFound('There was no user for the requested id.') data = request.get_json() song_id = data['song_id'] song = Song.get_by_id(song_id) if not song: raise ObjectNotFound('There was no song for the requested id.') try: playlist.songs.append(song) playlist.save() except Exception as e: print(e) raise AppErrorBaseClass('There was an error saving the song.') result = playlist_schema.dump(playlist) return result, 201
def post(self): data = request.get_json() data['password'] = generate_password_hash( data['password']).decode('utf8') user_dict = user_schema.load(data) user = User(**user_dict) country = Country.get_by_id(user_dict['country_id']) user.country = country # Error user.playlists.append(Playlist(playlist_name='favourite', favourite=1)) try: user.save() except Exception as e: print(e) raise AppErrorBaseClass('The email is already in use.') try: response = user_schema_no_pswd.dump(user) except Exception as e: print(e) return response, 201