Skip to main content

A module for cleaning up artist, album, and song names.

Project description

music-metadata-filter Test PYPI

A module for cleaning up artist, album and song names.

Installation

The music-metadata-filter library can be found on PyPI, and is installable with pip:

pip install music-metadata-filter

Usage

Basic example

from music_metadata_filter.functions import remove_remastered

print(remove_remastered("Track Title 1 - Remastered 2015"))
# outputs "Track Title 1 "
print(remove_remastered("Track Title 2 (2011 Remaster)"))
# outputs "Track Title 2 "

Single filter functions

You can call filter functions for basic, one-line filter functionality. These filter functions are intended to be used on a single field, such as an artist, album or track.

However, it is possible (not officially supported) to use some of these on combined fields ("Artist - Song", "Artist - Album"), as in the third example below.

import music_metadata_filter.functions as functions

print(functions.remove_remastered("Jane Doe (Remastered)"))
# outputs "Jane Doe "
print(functions.remove_version("Get Lucky (Album Version)"))
# outputs "Get Lucky "
print(functions.youtube("Car Bomb - Scattered Sprites (Official Music Video)"))
# outputs "Car Bomb - Scattered Sprites"

See functions.py for more details.

Multiple filters

You can also use multiple filters on a string at once by creating a MetadataFilter object which combines multiple functions from above, or by using one of the predefined filter objects.

First, create a filter set. This is a set of rules for artists, albums, tracks, and albumArtists.

import music_metadata_filter.functions as functions
from music_metadata_filter.filter import FilterSet

filter_set: FilterSet = {
    "track": (
        functions.remove_remastered,
        functions.fix_track_suffix,
        functions.remove_live,
        functions.remove_version,
    ),
    "album": (
        functions.remove_remastered,
        functions.fix_track_suffix,
        functions.remove_live,
    ),
}

Then, construct a MetadataFilter using this filter set.

from music_metadata_filter.filter import MetadataFilter

filter = MetadataFilter(filter_set)
print(filter.filter_field("album", "Nevermind (Remastered)"))
# outputs "Nevermind "
print(filter.filter_field("track", "In Bloom - Nevermind Version"))
# outputs "In Bloom (Nevermind Version)"
print(filter.filter_field("track", "Won't Get Fooled Again - Album Version"))
# outputs "Won't Get Fooled Again "

Predefined filters

There are also predefined filters available for easy access. For example, the above filter set is actually the predefined Spotify filter:

from music_metadata_filter.filters import make_spotify_filter

filter = make_spotify_filter()

See filters.py for more details.

Extending filters

Finally, you can take existing MetadataFilter objects and extend them with another filter. This is done by providing the .extend() method with another MetadataFilter object.

from music_metadata_filter.filters import make_spotify_filter, make_amazon_filter

filter = make_spotify_filter()
filter.extend(make_amazon_filter())

print(filter.filter_field("track", "Seasons in the Abyss (Album Version)"))
# outputs "Seasons in the Abyss "

As an alternative, you can use the .append() method to apply a filter set to an existing MetadataFilter object.

filter = make_spotify_filter()
filter.append({"album": lambda x: f"{x} Album"})

Since these methods return a MetadataFilter instance, you can chain method calls.

filter = make_spotify_filter().append({
    "artist": lambda x: f"{x} The Artist",
})

Development

# Initialise a virtualenv
> python3 -m venv .
> source bin/activate

# Install dev dependencies
> pip install -r requirements-dev.txt

# Run tests
> pytest

# Run black formatter
> black music_metadata_filter tests

# Run flake8 linter
> flake8 music_metadata_filter tests

# Run mypy type checker
> mypy music_metadata_filter tests

# Regenerate regular expressions and test fixtures from upstream
> ./regen.sh

License

Licensed under the MIT License.

Acknowledgements

This library is a (mostly) direct port of the original JS library metadata-filter. Some of the code in this library, including regular expressions and test fixtures, are taken directly from the upstream repository.

I can't thank the web-scrobbler team enough for creating such a fantastic piece of software, and for collaborating with me in the creation of this port to Python.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

music-metadata-filter-1.0.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

music_metadata_filter-1.0.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file music-metadata-filter-1.0.0.tar.gz.

File metadata

  • Download URL: music-metadata-filter-1.0.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.6

File hashes

Hashes for music-metadata-filter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1daba23c247b2bcf062d15e06172a5499df4f427bfa3cec060ef26f3db1f8a02
MD5 cfa573a11d28850b1e6d34b71c6d8269
BLAKE2b-256 d48d5e3829a90b8b09f66deba492fe05b5ffe8f388015f139b94d0ed1a4130a0

See more details on using hashes here.

File details

Details for the file music_metadata_filter-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: music_metadata_filter-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.6

File hashes

Hashes for music_metadata_filter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ffbf592b23418a4a2a97f4e73887d3224be78d148d849bfe5fca9e7136221848
MD5 744fc48d52cf563ca6015ed8c7d3fe02
BLAKE2b-256 3f19b470c4be5f69e4be471f67caf8b0d5867b77cbae270bcfe54533af4c23e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page