def _save_track(self, spotify_track):
        album_id = spotify_track['album']['id']
        album = self.spotify_client.client.album(album_id)
        saved_album = self._save_album(album)

        # Save the track (with the album)
        saved_track, was_created = Track.get_or_create(
            id=spotify_track['id'],
            defaults={
                'name': spotify_track['name'],
                'track_number ': spotify_track['track_number'],
                'duration_ms ': spotify_track['duration_ms'],
                'explicit': spotify_track['explicit'],
                'popularity ': spotify_track['popularity'],
                'href': spotify_track['href'],
                'spotify_url': spotify_track['external_urls']['spotify'],
                'preview_url ': spotify_track['preview_url'],
                'uri': spotify_track['uri'],
                'album': saved_album
            })

        if was_created:
            saved_artists = []
            for track_artist in spotify_track['artists']:
                artist_id = track_artist['id']
                artist = self.spotify_client.client.artist(artist_id)
                saved_artist = self._save_artist(artist)
                saved_artists.append(saved_artist)
            # Set the artists to the album
            saved_track.artists = saved_artists
            saved_track.save()
            Logger.log_db_operation(Logger.DBOperation.CREATE, saved_track)
        return saved_track
    def _save_album(self, spotify_album):
        image = spotify_album['images'][0]['url'] if len(
            spotify_album['images']) > 0 else ''

        saved_album, was_created = Album.get_or_create(
            id=spotify_album['id'],
            defaults={
                'name': spotify_album['name'],
                'label': spotify_album['label'],
                'image': image,
                'popularity': spotify_album['popularity'],
                'href': spotify_album['href'],
                'spotify_url': spotify_album['external_urls']['spotify'],
                'uri': spotify_album['uri']
            })

        if was_created:
            saved_artists = []
            for album_artist in spotify_album['artists']:
                artist_id = album_artist['id']
                artist = self.spotify_client.client.artist(artist_id)
                saved_artist = self._save_artist(artist)
                saved_artists.append(saved_artist)
            # Set the artists to the album
            saved_album.artists = saved_artist
            saved_album.save()

            saved_genres = self._save_genres(spotify_album['genres'])
            saved_album.genres = saved_genres
            saved_album.save()
            Logger.log_db_operation(Logger.DBOperation.CREATE, saved_album)
        return saved_album
 def _save_genres(self, genres):
     saved_genres = []
     for genre in genres:
         saved_genre, was_created = Genre.get_or_create(name=genre)
         saved_genres.append(saved_genre)
         if was_created:
             Logger.log_db_operation(Logger.DBOperation.CREATE, saved_genre)
     return saved_genres
 def _save_user(self):
     # Create or get the user that sent the link
     user, was_created = User.get_or_create(
         id=self.update.message.from_user.id,
         defaults={
             'username': self.update.message.from_user.username,
             'first_name': self.update.message.from_user.first_name
         })
     if was_created:
         Logger.log_db_operation(Logger.DBOperation.CREATE, user)
     return user
 def _save_chat(self):
     # Create or get the chat where the link was sent
     chat, was_created = Chat.get_or_create(
         id=self.update.message.chat_id,
         defaults={
             'name':
             self.update.message.chat.title
             or self.update.message.chat.username
             or self.update.message.chat.first_name
         })
     if was_created:
         Logger.log_db_operation(Logger.DBOperation.CREATE, chat)
     return chat
    def _save_link(self, cleaned_url, link_type, user, chat):
        # Update the link if it exists for a chat, create if it doesn't exist
        link = Link.get_or_none((Link.url == cleaned_url)
                                & (Link.chat == chat))
        was_updated = False
        if link is not None:
            # If link already exists, set updated_at and last_update_user to current
            link.apply_update(user)
            link.save()
            was_updated = True
            Logger.log_db_operation(Logger.DBOperation.UPDATE, link)
        else:
            link = Link.create(url=cleaned_url,
                               link_type=link_type.value,
                               created_at=datetime.datetime.now(),
                               user=user,
                               chat=chat)
            Logger.log_db_operation(Logger.DBOperation.CREATE, link)

        return link, was_updated
    def _save_artist(self, spotify_artist):
        image = spotify_artist['images'][0]['url'] if len(
            spotify_artist['images']) > 0 else ''

        saved_artist, was_created = Artist.get_or_create(
            id=spotify_artist['id'],
            defaults={
                'name': spotify_artist['name'],
                'image': image,
                'popularity': spotify_artist['popularity'],
                'href': spotify_artist['href'],
                'spotify_url': spotify_artist['external_urls']['spotify'],
                'uri': spotify_artist['uri']
            })

        # Save or retrieve the genres
        if was_created:
            saved_genres = self._save_genres(spotify_artist['genres'])
            saved_artist.genres = saved_genres
            saved_artist.save()
            Logger.log_db_operation(Logger.DBOperation.CREATE, saved_artist)
        return saved_artist