def sync_data( replace_existing: bool = False, query: str = "", fields=["ArtistPeople", "Title", "Album"], ): start = time.time() print(datetime.now().time()) paths = get_paths(query=query, fields=fields) with click.progressbar(paths) as click_paths: for idx, path in enumerate(click_paths): song = get_song(path) try: song_logic.add( song, return_existing=True, update_existing=True, replace_existing_tags=replace_existing, ) except Exception as ex: print(ex) print(f"{song.title} - {song.artist}") logger.error(f"MB: {song.artist} - {song.title}") if idx % 500 == 0: db.commit() print(time.time() - start)
def test_add_song_multiple_artists(): song_logic.add( SongIn( title="title", length=1, album="album", album_artist="artist", artist="artist1, artist2", tags=[TagIn(tag_type="type", value="tag")], ) ) assert orm.count(s for s in SongDb) == 1 assert orm.count(t for t in TagDb) == 1 assert orm.count(a for a in ArtistDb) == 3 assert orm.count(a for a in AlbumDb) == 1
def scrobble(scrobble: ScrobbleIn) -> ScrobbleDb: """Add scrobbles to the database. uses the logic/song.add() method to add songs with `return_existing=True` and `update_existing=True` ## Arguments: - `scrobble`: `ScrobbleIn`: - The scrobble ## Returns: - `ScrobbleDb`: - The created scrobble """ from app.logic import song as song_logic query = orm.select(s for s in ScrobbleDb) query = query.filter(lambda s: s.title == scrobble.title.lower()) query = query.filter(lambda s: s.artist == scrobble.artist.lower()) query = query.filter(lambda s: s.album == scrobble.album.lower()) db_song = query.first() if db_song is None: song = song_logic.add( SongIn(**scrobble.dict()), return_existing=True, update_existing=True ) else: song = db_song.song return ScrobbleDb( song=song, title=scrobble.title, artist=scrobble.artist, album=scrobble.album, date=scrobble.date, )
def test_add_song_existing_tag(): tag = mixer.blend(TagDb) song_logic.add( SongIn( title="title", length=1, album="album", album_artist="artist", artist="artist1", tags=[TagIn(tag_type=tag.tag_type, value=tag.value)], ) ) assert orm.count(s for s in SongDb) == 1 assert orm.count(t for t in TagDb) == 1 assert orm.count(a for a in ArtistDb) == 2 assert orm.count(a for a in AlbumDb) == 1
def test_add_song_existing_album(): album = mixer.blend(AlbumDb, album_artist=mixer.blend(ArtistDb)) song_logic.add( SongIn( title="title", length=1, album=album.name, album_artist=album.album_artist.name, artist="artist1", tags=[TagIn(tag_type="type", value="tag")], ) ) assert orm.count(s for s in SongDb) == 1 assert orm.count(t for t in TagDb) == 1 assert orm.count(a for a in ArtistDb) == 2 assert orm.count(a for a in AlbumDb) == 1
def test_add_song_existing_with_replace_exisiting_tags(): db_song = mixer.blend( SongDb, title="title", albums=mixer.blend(AlbumDb, name="album"), artists=mixer.blend(ArtistDb, name="artist"), tags=mixer.blend(TagDb, tag_type="type", value="value"), ) song = song_logic.add( SongIn( title="title", length=1, album="album2", album_artist="artist2", artist="artist", tags=[TagIn(tag_type="type2", value="value2")], ), return_existing=True, update_existing=True, replace_existing_tags=True, ) db.flush() assert db_song.id == song.id assert len(song.albums) == 2 assert len(song.tags) == 1 assert orm.count(s for s in SongDb) == 1 assert orm.count(t for t in TagDb) == 2 assert orm.count(a for a in ArtistDb) == 2 assert orm.count(a for a in AlbumDb) == 2
def test_get_song_multiple_artists_case_difference(): db_song = song_logic.add( SongIn(title="Title", album="Album", length=180, artist="Artist1, Artist2") ) artists = [a.name.lower() for a in db_song.artists] song = song_logic.get(title="title", artists=artists) assert song is not None assert song.title == db_song.title assert len(song.artists) == 2
def test_add_song_existing(): mixer.blend( SongDb, title="title", albums=mixer.blend(AlbumDb, name="album"), artists=mixer.blend(ArtistDb, name="artist"), tags=mixer.blend(TagDb, tag_type="type", value="value"), ) with pytest.raises(IntegrityError): song_logic.add( SongIn( title="title", length=1, album="album", album_artist="artist", artist="artist", tags=[TagIn(tag_type="type", value="value")], ) )
def test_add_song_existing_add_length(): db_song = mixer.blend( SongDb, length=None, title="title", artists=mixer.blend(ArtistDb, name="artist") ) song = song_logic.add( SongIn(title="title", artist="artist", length=250, album="album2"), return_existing=True, update_existing=True, ) db.flush() assert db_song.id == song.id assert orm.count(s for s in SongDb) == 1 assert db_song.length == song.length
def test_add_song_cased_alt_title(): song = song_logic.add( SongIn( title="Title", length=1, album="album", album_artist="artist", artist="artist1", tags=[TagIn(tag_type="type", value="tag")], ) ) assert orm.count(s for s in SongDb) == 1 assert orm.count(t for t in TagDb) == 1 assert orm.count(a for a in ArtistDb) == 2 assert orm.count(a for a in AlbumDb) == 1 assert song.title == "title" assert song.title_alt == "Title"
def test_add_song_existing_with_return_existing_cleaned_artist(): db_song = mixer.blend( SongDb, title="title", albums=mixer.blend(AlbumDb, name="album"), artists=mixer.blend(ArtistDb, name="artist"), tags=mixer.blend(TagDb, tag_type="type", value="value"), ) song = song_logic.add( SongIn( title="title", length=1, album="album", album_artist="artist", artist="artist (cv. hallo)", tags=[TagIn(tag_type="type", value="value")], ), return_existing=True, ) assert db_song.id == song.id