How to compare two releases in a MusicBrainz Picard plugin?
Question: I have been trying to write a Picard plugin. My idea is for it to
automatically insert transliterated track listings as comments for releases
with track titles written in non-Latin scripts. MusicBrainz contains these
transliterations as pseudo-releases existing in parallel with the actual
releases. For [a given
release](https://musicbrainz.org/release/1492ce2b-a9ee-4aa3-b9e2-b18ad093bc51),
for example, I would have to first read the relation for the ID of the related
release. I would then have to transfer the titles of those tracks into the
comment field of my files. In pseudocode:

    
    
    pseudo_release = actual_release.getTransliteration() # As indicated in a relationship
    i = 1
    while i <= actual_release.numTracks():
        actual_release.getTrack(i).setComment(pseudo_release.getTrack(i).getTitle())
        i++
    

The first line is what I'm not sure how to do. Since the relation is not
available as a Picard variable, it can't be accessed by any way indicated in
the API.

I've been looking through the source of Picard and also [the standalone web
service](http://python-musicbrainzngs.readthedocs.org/en/latest/#) but haven't
been able to find anything. Is this at all possible, and if so how do I do it?


Answer: The information about relationships, including transliterated tracklistings,
is available in Picard if you write a metadata processor. A simple plugin
iterating over all relationships of a release would look like this:

    
    
    PLUGIN_NAME = "Find transliterated tracklisting relationships"
    PLUGIN_AUTHOR = "Wieland Hoffmann"
    PLUGIN_DESCRIPTION = "I'm too lazy"
    PLUGIN_VERSION = "0.1"
    PLUGIN_API_VERSIONS = ["1.0"]
    
    from picard.metadata import register_album_metadata_processor
    from picard import log
    
    
    # The relationship type id for transliterations from
    # https://musicbrainz.org/relationship/fc399d47-23a7-4c28-bfcf-0607a562b644
    TRANS_REL_UUID = "fc399d47-23a7-4c28-bfcf-0607a562b644"
    
    
    @register_album_metadata_processor
    def find_transliteration_relationship(album, metadata, release):
        if "relation_list" in release.children:
            for rel in release.relation_list:
                if rel.relation[0].type_id == TRANS_REL_UUID:
                    log.info("Found a transliterated tracklisting relationship")
                    for release in rel.relation[0].release:
                        log.info("Its target is https://musicbrainz.org/release/%s",
                                 release.id)
    

The `release` argument that gets passed to the processor is an instance of
Picards
[XmlNode](https://github.com/musicbrainz/picard/blob/master/picard/webservice.py#L72)
class and its structure (including its child objects) resembles the XML you
get by asking the MusicBrainz server about this release via the web service
([this](https://beta.musicbrainz.org/ws/2/release/1492ce2b-a9ee-4aa3-b9e2-b18ad093bc51?inc=release-
rels) is what it returns for your example release if you only ask it about
relationships). Now that you have the MBID of the relationships target, you
can use the
[get](https://github.com/musicbrainz/picard/blob/master/picard/webservice.py#L292)
method of Picards webservice module (the `album`s `tagger.xmlws` attribute is
an instance of the XmlWebService class) to send another request to the
MusicBrainz website asking for data about that release (don't forget to in-
and decrement the `album`s `_requests` attribute so it doesn't complete its
loading steps until after you've changed its data).

Some other plugins that use this to request and process further data are [the
album artist
website](https://github.com/musicbrainz/picard/blob/master/contrib/plugins/albumartist_website.py)
and
[Last.FM.Plus](https://github.com/musicbrainz/picard/blob/master/contrib/plugins/lastfmplus/__init__.py)
plugins.

/edit: I've just been informed that there's already [a
ticket](http://tickets.musicbrainz.org/browse/PICARD-145) for improving how
Picard handles pseudoreleases which has a link to [a
plugin](https://github.com/96187/picard-plugins/blob/master/use-pseudo-
releases.py) doing what you want to be doing.

