def _link_delta(self, session, src, start, results, links, id): commit = False try: for artist in unpack( self._api("artist", "similar", id=id, start=start, results=results - start), "response", "artists" ): try: nest_artist = session.query(NestArtist).filter(NestArtist.id == artist["id"]).one() for dst in nest_artist.artists: if ( links < self._max_links and dst.name not in EXCLUDE and not session.query(Link).filter(Link.src == src, Link.dst == dst).count() ): debug("linking %s to %s" % (src.name, dst.name)) session.add(Link(src=src, dst=dst)) commit = True links += 1 # count nest artists, not multiple local artists except NoResultFound: pass if commit: session.commit() return links except ValueError as e: warning("%s, so deleting" % e) self._api.delete("artist", "similar", id=id, start=start, results=results - start) return links
def _song_search(self, title, artist=None, results=1): params = {'title': title, 'results': results, 'sort': 'artist_familiarity-desc'} if artist: params['artist'] = artist for song in unpack(self._api('song', 'search', **params), 'response', 'songs'): yield song['artist_id'], song['artist_name'] match = DROP_TRAILING_PARENS.match(title) if match: title = match.group(1) debug(' retrying with %s' % title) for id, name in self._song_search(title, artist, results): yield id, name
def _tags(self, session, nest_artist): for result in unpack( self._api('artist', 'terms', id=nest_artist.id, sort='weight'), 'response', 'terms'): if float(result['weight']) > 0.25: text = result['name'] try: tag = session.query(Tag).filter(Tag.text == text).one() except NoResultFound: tag = Tag(text=text) session.add(tag) yield tag
def _artist_search(self, artist): artist = unpack(self._api('artist', 'search', name=artist, results=1, sort='familiarity-desc'), 'response', 'artists', 0) return artist['id'], artist['name']