Ejemplo n.º 1
0
    def test_match_track(self):
        with mock.patch("musicbrainzngs.search_recordings") as p:
            p.return_value = {"recording-list": [{"title": "foo", "id": "bar", "length": 42}]}
            ti = list(mb.match_track("hello", "there"))[0]

            p.assert_called_with(artist="hello", recording="there", limit=5)
            self.assertEqual(ti.title, "foo")
            self.assertEqual(ti.track_id, "bar")
Ejemplo n.º 2
0
    def item_candidates(self, item):
        last_data = last_match(item.path)
        if not last_data:
            return ()

        # Search MusicBrainz.
        cands = list(mb.match_track(last_data['artist'],
                                    last_data['track']))

        log.debug('Matched last track candidates: %s' %
                  ', '.join([cand.title for cand in cands]))
        return cands
Ejemplo n.º 3
0
def tag_item(item, timid=False, search_artist=None, search_title=None,
             search_id=None):
    """Attempts to find metadata for a single track. Returns a
    `(candidates, recommendation)` pair where `candidates` is a list
    of `(distance, track_info)` pairs. `search_artist` and 
    `search_title` may be used to override the current metadata for
    the purposes of the MusicBrainz title; likewise `search_id`.
    """
    candidates = []

    # First, try matching by MusicBrainz ID.
    trackid = search_id or item.mb_trackid
    if trackid:
        log.debug('Searching for track ID: ' + trackid)
        track_info = mb.track_for_id(trackid)
        if track_info:
            dist = track_distance(item, track_info, incl_artist=True)
            candidates.append((dist, track_info))
            # If this is a good match, then don't keep searching.
            rec = recommendation(candidates)
            if rec == RECOMMEND_STRONG and not timid:
                log.debug('Track ID match.')
                return candidates, rec

    # If we're searching by ID, don't proceed.
    if search_id is not None:
        if candidates:
            return candidates, rec
        else:
            return [], RECOMMEND_NONE
    
    # Search terms.
    if not (search_artist and search_title):
        search_artist, search_title = item.artist, item.title
    log.debug(u'Item search terms: %s - %s' % (search_artist, search_title))

    # Candidate metadata from search.
    for track_info in mb.match_track(search_artist, search_title):
        dist = track_distance(item, track_info, incl_artist=True)
        candidates.append((dist, track_info))

    # Add candidates from plugins.
    for track_info in plugins.item_candidates(item):
        dist = track_distance(item, track_info, incl_artist=True)
        candidates.append((dist, track_info))

    # Sort by distance and return with recommendation.
    log.debug('Found %i candidates.' % len(candidates))
    candidates.sort()
    rec = recommendation(candidates)
    return candidates, rec
Ejemplo n.º 4
0
    def test_match_track(self):
        with mock.patch('musicbrainzngs.search_recordings') as p:
            p.return_value = {
                'recording-list': [{
                    'title': 'foo',
                    'id': 'bar',
                    'length': 42,
                }],
            }
            ti = list(mb.match_track('hello', 'there'))[0]

            p.assert_called_with(artist='hello', recording='there', limit=5)
            self.assertEqual(ti.title, 'foo')
            self.assertEqual(ti.track_id, 'bar')
Ejemplo n.º 5
0
def _item_candidates(item, artist, title):
    """Search for item matches. ``item`` is the Item to be matched.
    ``artist`` and ``title`` are strings and either reflect the item or
    are specified by the user.
    """
    out = []

    # MusicBrainz candidates.
    if artist and title:
        out.extend(mb.match_track(artist, title))

    # Plugin candidates.
    out.extend(plugins.item_candidates(item))

    return out
Ejemplo n.º 6
0
def item_candidates(item, artist, title):
    """Search for item matches. ``item`` is the Item to be matched.
    ``artist`` and ``title`` are strings and either reflect the item or
    are specified by the user.
    """

    # MusicBrainz candidates.
    if artist and title:
        try:
            for candidate in mb.match_track(artist, title):
                yield candidate
        except mb.MusicBrainzAPIError as exc:
            exc.log(log)

    # Plugin candidates.
    for candidate in plugins.item_candidates(item, artist, title):
        yield candidate
Ejemplo n.º 7
0
def item_candidates(item, artist, title):
    """Search for item matches. ``item`` is the Item to be matched.
    ``artist`` and ``title`` are strings and either reflect the item or
    are specified by the user.
    """
    out = []

    # MusicBrainz candidates.
    if artist and title:
        try:
            out.extend(mb.match_track(artist, title))
        except mb.MusicBrainzAPIError as exc:
            exc.log(log)

    # Plugin candidates.
    out.extend(plugins.item_candidates(item, artist, title))

    # Notify subscribed plugins about fetched track info
    for i in out:
        plugins.send('trackinfo_received', info=i)

    return out
Ejemplo n.º 8
0
 def test_match_track_empty(self):
     with mock.patch('musicbrainzngs.search_recordings') as p:
         til = list(mb.match_track(' ', ' '))
         self.assertFalse(p.called)
         self.assertEqual(til, [])
Ejemplo n.º 9
0
 def test_match_track_empty(self):
     with mock.patch('musicbrainzngs.search_recordings') as p:
         til = list(mb.match_track(' ', ' '))
         self.assertFalse(p.called)
         self.assertEqual(til, [])