def append_song(self, song_id, mbid=None, *, connection=None): assert song_id, "song_id must be used" if not mbid: print(f'Obtaining MBID for song id {song_id}') mbid = MusicDatabase.getRecordingMBID(song_id) item = (song_id, mbid) self.songs.append(item) if self.id and self.store_songs_in_db: pos = len(self.songs) - 1 c = connection or MusicDatabase.getCursor() entry = { 'playlist_id': self.id, 'song_id': song_id, 'recording_mbid': mbid, 'pos': pos, } pls = table('playlist_songs') MusicDatabase.insert_or_update(pls, entry, and_(pls.c.playlist_id == self.id, pls.c.pos == pos), connection=c) if not connection: c.commit()
def insert_song(self, position, song_id=None, mbid=None, *, connection=None): assert song_id, "song_id must be used" if position > len(self.songs): position = len(self.songs) if not mbid: mbid = MusicDatabase.getRecordingMBID(song_id) item = (song_id, mbid) self.songs.insert(position, item) if self.id: c = connection or MusicDatabase.getCursor() self._update_positions_in_db(len(self.songs) - 1, position, 1, c) entry = { 'playlist_id': self.id, 'pos': position, 'song_id': song_id, 'recording_mbid': mbid, } pls = table('playlist_songs') MusicDatabase.insert_or_update(pls, entry, and_(pls.c.playlist_id == self.id, pls.c.pos == position), connection=c) if not connection: c.commit()