def update_artists(artists): artist_ids = list( set( filter( lambda id: not document_exists(mongo.db.artists, {'id': id}), [artist['id'] for artist in artists]))) new_artists = [] if len(artist_ids) > 0: new_artists = fetch_artists(artist_ids) try: mongo.db.artists.insert(new_artists) except DuplicateKeyError as e: print('ATTEMPTED TO INSERT DUPLICATE ARTIST') return new_artists
def update_albums(albums): album_ids = list( set( filter(lambda id: not document_exists(mongo.db.albums, {'id': id}), [album['id'] for album in albums]))) new_albums = [] if len(album_ids) > 0: new_albums = fetch_albums(album_ids) try: mongo.db.albums.insert(new_albums) except DuplicateKeyError as e: print('ATTEMPTED TO INSERT DUPLICATE ALBUM') update_artists( reduce(lambda art, alb: art + alb['artists'], new_albums, [])) return new_albums
def update_tracks(tracks): track_ids = list( set( filter(lambda id: not document_exists(mongo.db.tracks, {'id': id}), [track['id'] for track in tracks]))) new_tracks = [] if len(track_ids) > 0: new_tracks = fetch_tracks(track_ids) try: mongo.db.tracks.insert(new_tracks) except DuplicateKeyError as e: print('ATTEMPTED TO INSERT DUPLICATE TRACK') update_albums([track['album'] for track in new_tracks]) update_artists( reduce(lambda art, trk: art + trk['artists'], new_tracks, [])) return new_tracks
def create_user(token_info): user_info = fetch_user(token_info) user = { 'token_info': token_info, 'recently_played': { 'active': True, 'last_checked': None }, 'playlists': { 'watched': [] }, 'user': user_info } user_id = user_info['id'] if not document_exists(mongo.db.users, {'user.id': user_id}): try: mongo.db.users.insert_one(user) except DuplicateKeyError as e: print('ATTEMPTED TO INSERT DUPLICATE USER') return user
def create_playlist(playlist_id, created_for=None): playlist_info = fetch_playlist(playlist_id) created_at = None if len(playlist_info['tracks']) > 0: created_at = min( [track['added_at'] for track in playlist_info['tracks']]) playlist = { 'created_at': created_at, 'created_for': created_for, 'history': [], 'last_checked': None, 'last_modified': None, 'playlist': playlist_info } if not document_exists(mongo.db.playlists, {'playlist.id': playlist_id}): try: mongo.db.playlists.insert_one(playlist) except DuplicateKeyError as e: print('ATTEMPTED TO INSERT DUPLICATE PLAYLIST') return playlist