From 72d07d354cc636d215ae970f9d708d2e3617cdfc Mon Sep 17 00:00:00 2001 From: Jacob Pavlock Date: Tue, 11 Oct 2022 14:52:51 -0700 Subject: [PATCH] feat: new album field - barcode --- .../eb8c7e20a080_new_field_barcode.py | 26 +++++++++++++++++++ docs/fields.rst | 1 + moe/library/album.py | 3 +++ moe/library/track.py | 1 + moe/plugins/musicbrainz/mb_core.py | 1 + moe/plugins/write.py | 1 + moe/util/core/match.py | 1 + tests/library/test_track.py | 2 ++ .../musicbrainz/resources/full_release.py | 1 + tests/plugins/test_write.py | 3 +++ 10 files changed, 40 insertions(+) create mode 100644 alembic/versions/eb8c7e20a080_new_field_barcode.py diff --git a/alembic/versions/eb8c7e20a080_new_field_barcode.py b/alembic/versions/eb8c7e20a080_new_field_barcode.py new file mode 100644 index 00000000..9dc267fa --- /dev/null +++ b/alembic/versions/eb8c7e20a080_new_field_barcode.py @@ -0,0 +1,26 @@ +"""new field barcode. + +Revision ID: eb8c7e20a080 +Revises: db3a470892c6 +Create Date: 2022-10-11 14:51:44.788096 + +""" +import sqlalchemy as sa + +from alembic import op + +# revision identifiers, used by Alembic. +revision = "eb8c7e20a080" +down_revision = "db3a470892c6" +branch_labels = None +depends_on = None + + +def upgrade(): + with op.batch_alter_table("album", schema=None) as batch_op: + batch_op.add_column(sa.Column("barcode", sa.String(), nullable=True)) + + +def downgrade(): + with op.batch_alter_table("album", schema=None) as batch_op: + batch_op.drop_column("barcode") diff --git a/docs/fields.rst b/docs/fields.rst index 45902df9..e059af48 100644 --- a/docs/fields.rst +++ b/docs/fields.rst @@ -34,6 +34,7 @@ Album Fields :width: 100% "artist", "Album artist", "" + "barcode", "UPC barcode", "" "country", "Country the album was released in (two character identifier)", "" "date", "Album release date", "2" "disc_total", "Number of discs in the album", "" diff --git a/moe/library/album.py b/moe/library/album.py index 88022ac0..e278fc42 100644 --- a/moe/library/album.py +++ b/moe/library/album.py @@ -85,6 +85,7 @@ class Album(LibItem, SABase): Attributes: artist (str): AKA albumartist. + barcode (str): UPC barcode. country (str): Country the album was released in (two character identifier). date (datetime.date): Album release date. disc_total (int): Number of discs in the album. @@ -103,6 +104,7 @@ class Album(LibItem, SABase): _id: int = cast(int, Column(Integer, primary_key=True)) artist: str = cast(str, Column(String, nullable=False)) + barcode: str = cast(str, Column(String, nullable=True)) country: str = cast(str, Column(String, nullable=True)) date: datetime.date = cast(datetime.date, Column(Date, nullable=False)) disc_total: int = cast(int, Column(Integer, nullable=False, default=1)) @@ -215,6 +217,7 @@ def fields(self) -> set[str]: """Returns any editable album fields.""" return { "artist", + "barcode", "country", "date", "disc_total", diff --git a/moe/library/track.py b/moe/library/track.py index cf6f7efd..ae47e548 100644 --- a/moe/library/track.py +++ b/moe/library/track.py @@ -111,6 +111,7 @@ def read_custom_tags( audio_file = mediafile.MediaFile(track_path) album_fields["artist"] = audio_file.albumartist or audio_file.artist + album_fields["barcode"] = audio_file.barcode album_fields["country"] = audio_file.country album_fields["date"] = audio_file.date album_fields["disc_total"] = audio_file.disctotal diff --git a/moe/plugins/musicbrainz/mb_core.py b/moe/plugins/musicbrainz/mb_core.py index 3260f9cb..7bdb2b8f 100644 --- a/moe/plugins/musicbrainz/mb_core.py +++ b/moe/plugins/musicbrainz/mb_core.py @@ -397,6 +397,7 @@ def _create_album(release: dict) -> Album: album = Album( artist=_flatten_artist_credit(release["artist-credit"]), + barcode=release.get("barcode"), country=release.get("country"), date=_parse_date(release["date"]), disc_total=int(release["medium-count"]), diff --git a/moe/plugins/write.py b/moe/plugins/write.py index 2e0c6916..9293f55a 100644 --- a/moe/plugins/write.py +++ b/moe/plugins/write.py @@ -77,6 +77,7 @@ def write_custom_tags(track: Track): audio_file.albumartist = track.albumartist audio_file.artist = track.artist audio_file.artists = track.artists + audio_file.barcode = track.album_obj.barcode audio_file.country = track.album_obj.country audio_file.date = track.album_obj.date audio_file.disc = track.disc diff --git a/moe/util/core/match.py b/moe/util/core/match.py index b0c2abdd..831faa5f 100644 --- a/moe/util/core/match.py +++ b/moe/util/core/match.py @@ -17,6 +17,7 @@ MATCH_ALBUM_FIELD_WEIGHTS = { "artist": 0.8, + "barcode": 1.0, "country": 0.3, "date": 0.2, "disc_total": 0.8, diff --git a/tests/library/test_track.py b/tests/library/test_track.py index c1bd7709..3fc05569 100644 --- a/tests/library/test_track.py +++ b/tests/library/test_track.py @@ -126,6 +126,7 @@ def test_read_tags(self, tmp_config): track.title = "Full" track.track_num = 1 + album.barcode = "1234" album.country = "US" album.date = datetime.date(2020, 1, 12) album.disc_total = 2 @@ -146,6 +147,7 @@ def test_read_tags(self, tmp_config): assert new_track.title == track.title assert new_track.track_num == track.track_num + assert new_album.barcode == album.barcode assert new_album.country == album.country assert new_album.disc_total == album.disc_total assert new_album.date == album.date diff --git a/tests/plugins/musicbrainz/resources/full_release.py b/tests/plugins/musicbrainz/resources/full_release.py index 8959681a..68c6740f 100644 --- a/tests/plugins/musicbrainz/resources/full_release.py +++ b/tests/plugins/musicbrainz/resources/full_release.py @@ -1681,6 +1681,7 @@ def album() -> Album: return Album( artist="Kanye West", title="My Beautiful Dark Twisted Fantasy", + barcode="602527474465", country="US", date=datetime.date(2010, 11, 22), label="Roc‐A‐Fella Records", diff --git a/tests/plugins/test_write.py b/tests/plugins/test_write.py index 8d2f4f66..a89ed17a 100644 --- a/tests/plugins/test_write.py +++ b/tests/plugins/test_write.py @@ -66,6 +66,7 @@ def test_write_tags(self, tmp_config): albumartist = "4 Non Blondes" artist = "4 Non Blondes" artists = {"4 Non Blondes", "Me"} + barcode = "1234" country = "US" date = datetime.date(1996, 10, 13) disc = 2 @@ -81,6 +82,7 @@ def test_write_tags(self, tmp_config): track.albumartist = albumartist track.artist = artist track.artists = artists + track.album_obj.barcode = barcode track.album_obj.country = country track.album_obj.date = date track.album_obj.original_date = original_date @@ -106,6 +108,7 @@ def test_write_tags(self, tmp_config): assert new_track.title == title assert new_track.track_num == track_num + assert new_album.barcode == barcode assert new_album.country == country assert new_album.date == date assert new_album.disc_total == disc_total