def add_to_database(self, filename, song_info): # Just in case anything is None if song_info.title == None: r_title = '' else: r_title = song_info.title if song_info.artist == None: r_artist_name = '' else: r_artist_name = song_info.artist if song_info.album == None: r_album_name = '' else: r_album_name = song_info.album if song_info.length == None: r_length = 0 else: r_length = song_info.length self.get_logger().error('Adding to database: %s by %s in %s', r_title, r_artist_name, r_album_name) # Ensure artist artist = self.ensure_or_create_artist(r_artist_name) ensure_artist_image(artist.id, r_artist_name) # Ensure album album = self.ensure_or_create_album(filename, r_artist_name, r_album_name) ensure_album_image(album.id, filename, r_album_name, r_artist_name) # Create song in database song = Song(filename, r_title, artist.id, album.id, r_length, song_info.year, song_info.track, song_info.disc_no) db.session.add(song) db.session.commit()
def replace_in_database(self, filename, s, song_info): # Just in case anything is None if song_info.title == None: r_title = '' else: r_title = song_info.title if song_info.artist == None: r_artist_name = '' else: r_artist_name = song_info.artist if song_info.album == None: r_album_name = '' else: r_album_name = song_info.album if song_info.length == None: r_length = 0 else: r_length = song_info.length # Save previous artist and album prev_artist_id = s.artistId prev_album_id = s.albumId # Ensure artist artist = self.ensure_or_create_artist(r_artist_name) ensure_artist_image(artist.id, r_artist_name) # Ensure album album = self.ensure_or_create_album(filename, r_artist_name, r_album_name) ensure_album_image(album.id, filename, r_album_name, r_artist_name) # Change song object s.title = r_title s.artistId = artist.id s.albumId = album.id s.length = r_length s.year = song_info.year s.track = song_info.track s.disc_no = song_info.disc_no db.session.commit() # Delete previous artist and album if not needed self.delete_artist_if_no_assoc(prev_artist_id) self.delete_album_if_no_assoc(prev_album_id)