'Print a listing of albums missing from each artist in the library
matching query.'

def _missing_albums(self, lib, query):

    total = self.config['total'].get()
    albums = lib.albums(query)
    albums_by_artist = defaultdict(list)
    for alb in albums:
        artist = (alb['albumartist'], alb['mb_albumartistid'])
        albums_by_artist[artist].append(alb)
    total_missing = 0
    for (artist, albums) in albums_by_artist.items():
        if ((artist[1] is None) or (artist[1] == '')):
            albs_no_mbid = [((u"'" + a['album']) + u"'") for a in albums]
            self._log.info(u"No    musicbrainz    ID    for    artist    '{}'    found    in    album(s)    {};    skipping", artist[0], u',    '.join(albs_no_mbid))
            continue
        try:
            resp = musicbrainzngs.browse_release_groups(artist=artist[1])
            release_groups = resp['release-group-list']
        except MusicBrainzError as err:
            self._log.info(u"Couldn't    fetch    info    for    artist    '{}'    ({})    -    '{}'", artist[0], artist[1], err)
            continue
        missing = []
        present = []
        for rg in release_groups:
            missing.append(rg)
            for alb in albums:
                if (alb['mb_releasegroupid'] == rg['id']):
                    missing.remove(rg)
                    present.append(rg)
                    break
        total_missing += len(missing)
        if total:
            continue
        missing_titles = {rg['title'] for rg in missing}
        for release_title in missing_titles:
            print_(u'{}    -    {}'.format(artist[0], release_title))
    if total:
        print(total_missing)
