Example #1
0
 async def update_radio(self, item_id: int, radio: Radio):
     """Update a radio record in the database."""
     async with aiosqlite.connect(self._dbfile, timeout=120) as db_conn:
         db_conn.row_factory = aiosqlite.Row
         cur_item = Radio.from_db_row(await self.__execute_fetchone(
             "SELECT * FROM radios WHERE item_id=?;", (item_id, ), db_conn))
         metadata = merge_dict(cur_item.metadata, radio.metadata)
         provider_ids = merge_list(cur_item.provider_ids,
                                   radio.provider_ids)
         sql_query = """UPDATE radios
             SET name=?,
                 sort_name=?,
                 metadata=?,
                 provider_ids=?
             WHERE item_id=?;"""
         await db_conn.execute(
             sql_query,
             (
                 radio.name,
                 radio.sort_name,
                 json_serializer(metadata),
                 json_serializer(provider_ids),
                 item_id,
             ),
         )
         await self._add_prov_ids(item_id,
                                  MediaType.Radio,
                                  radio.provider_ids,
                                  db_conn=db_conn)
         LOGGER.debug("updated radio %s in database: %s", radio.name,
                      item_id)
         await db_conn.commit()
     # return updated object
     return await self.get_radio(item_id)
Example #2
0
 async def update_artist(self, item_id: int, artist: Artist):
     """Update a artist record in the database."""
     async with aiosqlite.connect(self._dbfile, timeout=120) as db_conn:
         db_conn.row_factory = aiosqlite.Row
         db_row = await self.__execute_fetchone(
             "SELECT * FROM artists WHERE item_id=?;", (item_id, ), db_conn)
         cur_item = Artist.from_db_row(db_row)
         metadata = merge_dict(cur_item.metadata, artist.metadata)
         provider_ids = merge_list(cur_item.provider_ids,
                                   artist.provider_ids)
         sql_query = """UPDATE artists
             SET musicbrainz_id=?,
                 metadata=?,
                 provider_ids=?
             WHERE item_id=?;"""
         await db_conn.execute(
             sql_query,
             (
                 artist.musicbrainz_id or cur_item.musicbrainz_id,
                 json_serializer(metadata),
                 json_serializer(provider_ids),
                 item_id,
             ),
         )
         await self._add_prov_ids(item_id,
                                  MediaType.Artist,
                                  artist.provider_ids,
                                  db_conn=db_conn)
         LOGGER.debug("updated artist %s in database: %s", artist.name,
                      item_id)
         await db_conn.commit()
         # return updated object
         return await self.get_artist(item_id)
Example #3
0
 async def update_album(self, item_id: int, album: Album):
     """Update a album record in the database."""
     async with aiosqlite.connect(self._dbfile, timeout=120) as db_conn:
         db_conn.row_factory = aiosqlite.Row
         cur_item = await self.get_album(item_id)
         album_artist = ItemMapping.from_item(
             await self.get_artist_by_prov_id(cur_item.artist.provider,
                                              cur_item.artist.item_id)
             or await self.get_artist_by_prov_id(album.artist.provider,
                                                 album.artist.item_id)
             or cur_item.artist)
         metadata = merge_dict(cur_item.metadata, album.metadata)
         provider_ids = merge_list(cur_item.provider_ids,
                                   album.provider_ids)
         if cur_item.album_type == AlbumType.Unknown:
             album_type = album.album_type
         else:
             album_type = cur_item.album_type
         sql_query = """UPDATE albums
             SET upc=?,
                 artist=?,
                 metadata=?,
                 provider_ids=?,
                 album_type=?
             WHERE item_id=?;"""
         await db_conn.execute(
             sql_query,
             (
                 album.upc or cur_item.upc,
                 json_serializer(album_artist),
                 json_serializer(metadata),
                 json_serializer(provider_ids),
                 album_type.value,
                 item_id,
             ),
         )
         await self._add_prov_ids(item_id,
                                  MediaType.Album,
                                  album.provider_ids,
                                  db_conn=db_conn)
         LOGGER.debug("updated album %s in database: %s", album.name,
                      item_id)
         await db_conn.commit()
         # return updated object
         return await self.get_album(item_id)
Example #4
0
    async def update_track(self, item_id: int, track: Track):
        """Update a track record in the database."""
        async with aiosqlite.connect(self._dbfile, timeout=120) as db_conn:
            db_conn.row_factory = aiosqlite.Row
            cur_item = await self.get_track(item_id)

            # we store a mapping to artists and albums on the track for easier access/listings
            track_artists = await self._get_track_artists(
                track, cur_item.artists)
            track_albums = await self._get_track_albums(track, cur_item.albums)
            # merge metadata and provider id's
            metadata = merge_dict(cur_item.metadata, track.metadata)
            provider_ids = merge_list(cur_item.provider_ids,
                                      track.provider_ids)
            sql_query = """UPDATE tracks
                SET isrc=?,
                    metadata=?,
                    provider_ids=?,
                    artists=?,
                    albums=?
                WHERE item_id=?;"""
            await db_conn.execute(
                sql_query,
                (
                    track.isrc or cur_item.isrc,
                    json_serializer(metadata),
                    json_serializer(provider_ids),
                    json_serializer(track_artists),
                    json_serializer(track_albums),
                    item_id,
                ),
            )
            await self._add_prov_ids(item_id,
                                     MediaType.Track,
                                     track.provider_ids,
                                     db_conn=db_conn)
            LOGGER.debug("updated track %s in database: %s", track.name,
                         item_id)
            await db_conn.commit()
            # return updated object
            return await self.get_track(item_id)
Example #5
0
 async def update_playlist(self, item_id: int, playlist: Playlist):
     """Update a playlist record in the database."""
     async with aiosqlite.connect(self._dbfile, timeout=120) as db_conn:
         db_conn.row_factory = aiosqlite.Row
         cur_item = Playlist.from_db_row(await self.__execute_fetchone(
             "SELECT * FROM playlists WHERE item_id=?;", (item_id, ),
             db_conn))
         metadata = merge_dict(cur_item.metadata, playlist.metadata)
         provider_ids = merge_list(cur_item.provider_ids,
                                   playlist.provider_ids)
         sql_query = """UPDATE playlists
             SET name=?,
                 sort_name=?,
                 owner=?,
                 is_editable=?,
                 checksum=?,
                 metadata=?,
                 provider_ids=?
             WHERE item_id=?;"""
         await db_conn.execute(
             sql_query,
             (
                 playlist.name,
                 playlist.sort_name,
                 playlist.owner,
                 playlist.is_editable,
                 playlist.checksum,
                 json_serializer(metadata),
                 json_serializer(provider_ids),
                 item_id,
             ),
         )
         await self._add_prov_ids(item_id,
                                  MediaType.Playlist,
                                  playlist.provider_ids,
                                  db_conn=db_conn)
         LOGGER.debug("updated playlist %s in database: %s", playlist.name,
                      item_id)
         await db_conn.commit()
     # return updated object
     return await self.get_playlist(item_id)