def load_from_id(klass, song_id, sid = None): if sid is not None: d = db.c.fetch_row("SELECT * FROM r4_songs JOIN r4_song_sid USING (song_id) WHERE r4_songs.song_id = %s AND r4_song_sid.sid = %s", (song_id, sid)) else: d = db.c.fetch_row("SELECT * FROM r4_songs WHERE song_id = %s", (song_id,)) if not d: raise SongNonExistent try: s = klass() s.id = song_id s.sid = sid s.filename = d['song_filename'] s.verified = d['song_verified'] s.replay_gain = d['song_replay_gain'] s.data['sids'] = db.c.fetch_list("SELECT sid FROM r4_song_sid WHERE song_id = %s", (song_id,)) s.data['sid'] = sid s.data['rank'] = None s._assign_from_dict(d) if 'album_id' in d and d['album_id']: if sid is not None: s.albums = [ Album.load_from_id_sid(d['album_id'], s.sid) ] else: s.albums = [ Album.load_from_id(d['album_id']) ] s.artists = Artist.load_list_from_song_id(song_id) s.groups = SongGroup.load_list_from_song_id(song_id) except Exception as e: log.exception("song", "Song ID %s failed to load, sid %s." % (song_id, sid), e) s.disable() raise return s
def load_from_id(klass, song_id, sid = None): if sid: d = db.c.fetch_row("SELECT * FROM r4_songs JOIN r4_song_sid USING (song_id) WHERE r4_songs.song_id = %s AND r4_song_sid.sid = %s", (song_id, sid)) else: d = db.c.fetch_row("SELECT * FROM r4_songs WHERE song_id = %s", (song_id,)) if not d: raise SongNonExistent try: s = klass() s.id = song_id s.sid = sid s.filename = d['song_filename'] s.verified = d['song_verified'] s.replay_gain = d['song_replay_gain'] s.data['sids'] = db.c.fetch_list("SELECT sid FROM r4_song_sid WHERE song_id = %s", (song_id,)) s.data['sid'] = sid s.data['rank'] = None s._assign_from_dict(d) if 'album_id' in d and d['album_id']: if sid: s.albums = [ Album.load_from_id_sid(d['album_id'], s.sid) ] else: s.albums = [ Album.load_from_id(d['album_id']) ] s.artists = Artist.load_list_from_song_id(song_id) s.groups = SongGroup.load_list_from_song_id(song_id) except Exception as e: log.exception("song", "Song failed to load.", e) db.c.update("UPDATE r4_songs SET song_verified = FALSE WHERE song_id = song_id") raise return s
from libs import config from libs import db from libs import cache from rainwave.playlist_objects.album import Album from rainwave.playlist_objects.album import clear_updated_albums if __name__ == "__main__": parser = argparse.ArgumentParser(description="Rainwave DB migration script for using dates from ID3 tags.") parser.add_argument("--config", default=None) args = parser.parse_args() config.load(args.config) for sid in config.station_ids: clear_updated_albums(sid) db.connect() cache.connect() print "Adding columns to database..." db.c.update("ALTER TABLE r4_albums ADD album_year SMALLINT") db.c.update("ALTER TABLE r4_songs ADD song_track_number SMALLINT") db.c.update("ALTER TABLE r4_songs ADD song_disc_number SMALLINT") db.c.update("ALTER TABLE r4_songs ADD song_year SMALLINT") for album_id in db.c.fetch_list("SELECT album_id FROM r4_albums ORDER BY album_id"): a = Album.load_from_id(album_id) # Will update the album year a.reconcile_sids() print "Done"