Esempio n. 1
0
 def getSimilarArtistsAJAX(self):
     mbid = request.params['mbid']
     artist = Session.query(MBArtist).filter(MBArtist.gid==mbid).one()
     try:
         similarartistmbids = similarartist.get_similar_artists(Session, self.lastfmNetwork, artist)
     except (BadStatusLine, SocketTimeout) as e:
         log.error('[lastfm] down? ' + e.__repr__())
         return '{}'
     similarartists = Session.query(MBArtist, MBArtistName).join(MBArtist.name).filter(MBArtist.gid.in_(similarartistmbids)).all()
     similarmap = {}
     for artist in similarartists:
         similarmap[artist[0].gid] = {'mbid' : artist[0].gid, 'name' : artist[1].name, 'local' : False}
     localsimilarartists = Session.query(Artist).filter(Artist.mbid.in_(similarartistmbids)).all()
     for artist in localsimilarartists:
         similarmap[artist.mbid]['local'] = True
     similarjson = []
     for mbid in similarartistmbids:
         similarjson.append(similarmap[mbid])
     return simplejson.dumps({'similar' : similarjson})
Esempio n. 2
0
 def similarTrackAJAX(self):
     trackid = request.params['id'].split('_')[1]
     artists = Session.query(MBArtist).join(MBArtistCreditName, MBArtistCredit, MBRecording, AudioFile, AudioFile.track).filter(Track.id==trackid).all()
     similarMbids = set([])
     try:
         for artist in artists:
             similarMbids.update(similarartist.get_similar_artists(Session, self.lastfmNetwork, artist))
     except (BadStatusLine, SocketTimeout) as e:
         log.error('[lastfm] down? ' + e.__repr__())
         return ''
     artistMbidsWithAlbums = Session.query(Artist.mbid) \
                                    .join(artist_albums) \
                                    .filter(Artist.mbid.in_(similarMbids)) \
                                    .distinct() \
                                    .subquery()
     randomSimilarArtist = Session.query(Artist) \
                                  .filter(Artist.mbid.in_(artistMbidsWithAlbums)) \
                                  .order_by(random()) \
                                  .first()
     randomAlbum = rand.choice(randomSimilarArtist.albums)
     randomTrack = rand.choice(randomAlbum.tracks)
     return simplejson.dumps([randomTrack.toPlaylistJSON()])