From bee35885bb845ea77aa4586bca33da3e54b92ed2 Mon Sep 17 00:00:00 2001 From: Edgars Supe Date: Thu, 9 Sep 2021 22:30:01 +0300 Subject: [PATCH] Add `albumtypes` plugin --- beetsplug/albumtypes.py | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 beetsplug/albumtypes.py diff --git a/beetsplug/albumtypes.py b/beetsplug/albumtypes.py new file mode 100644 index 000000000..2f8c475ba --- /dev/null +++ b/beetsplug/albumtypes.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- + +# This file is part of beets. +# Copyright 2021, Edgars Supe. +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. + +"""Adds an album template field for formatted album types.""" + +from __future__ import division, absolute_import, print_function + +from beets.autotag.mb import VARIOUS_ARTISTS_ID +from beets.library import Album +from beets.plugins import BeetsPlugin + + +class AlbumTypesPlugin(BeetsPlugin): + """Adds an album template field for formatted album types.""" + + def __init__(self): + """Init AlbumTypesPlugin.""" + super(AlbumTypesPlugin, self).__init__() + self.album_template_fields['atypes'] = self._atypes + + def _atypes(self, item: Album): + self.config.add({ + 'types': [], + 'ignore_va': [], + 'brackets': '[]' + }) + types = self.config['types'].as_pairs() + ignore_va = self.config['ignore_va'].as_str_seq() + bracket = self.config['bracket'].as_str() + + # Assign a left and right bracket or leave blank if argument is empty. + if len(bracket) == 2: + bracket_l = bracket[0] + bracket_r = bracket[1] + else: + bracket_l = u'' + bracket_r = u'' + + res = '' + albumtypes = item.albumtypes.split('; ') + is_va = item.mb_albumartistid == VARIOUS_ARTISTS_ID + for type in types: + if type[0] in albumtypes and type[1]: + if not is_va or (not type[0] in ignore_va and is_va): + res += bracket_l + type[1] + bracket_r + + return res