def store_artwork(artwork_data, mb_albumid): import gio from gtk import gdk artwork_in = gio.memory_input_stream_new_from_data(artwork_data) pixbuf = gdk.pixbuf_new_from_stream(artwork_in, None) artwork = gpod.itdb_artwork_new() gpod.itdb_artwork_set_thumbnail_from_pixbuf(artwork, pixbuf, 0, None) mb_albumid_to_artwork[mb_albumid] = artwork return artwork
def rebuild_artwork(mountpoint, dry_run=True): from collections import defaultdict db = gpod.Database(mountpoint) mbid_album_tracks = defaultdict(set) mbid_album_artwork = dict() non_mbid_album_tracks = defaultdict(set) non_mbid_album_artwork = dict() for track in db: md_hard, md_easy = get_metadata(track.ipod_filename()) mb_albumid = get_first(md_easy, "musicbrainz_albumid", None) artist = get_first(md_easy, "artist", None) album = get_first(md_easy, "album", None) compilation = is_compilation(md_easy) if mb_albumid is not None: album_id = mb_albumid album_tracks = mbid_album_tracks album_artwork = mbid_album_artwork elif artist is not None and album is not None: if compilation: album_id = (("compilation",), album) else: album_id = (artist, album) album_tracks = non_mbid_album_tracks album_artwork = non_mbid_album_artwork else: continue album_tracks[album_id].add(track) if album_id in album_artwork: continue artwork_data = get_any_artwork(md_hard) if artwork_data is None: continue _log.debug("loaded artwork from track %r", track) album_artwork[album_id] = artwork_data for album_tracks, album_artwork in [ (mbid_album_tracks, mbid_album_artwork), (non_mbid_album_tracks, non_mbid_album_artwork), ]: for album_id, artwork_data in album_artwork.iteritems(): import gio from gtk import gdk artwork_in = gio.memory_input_stream_new_from_data(artwork_data) pixbuf = gdk.pixbuf_new_from_stream(artwork_in, None) tracks = album_tracks[album_id] _log.debug("setting artwork on %d tracks of album %r", len(tracks), album_id) for track in tracks: track.set_coverart(pixbuf) if dry_run: _log.info("dry run, quitting") return _log.info("saving itunesdb") db.close() _log.info("done")