Skip to content

Instantly share code, notes, and snippets.

@robrocker7
Created January 24, 2013 16:16
Show Gist options
  • Select an option

  • Save robrocker7/4624178 to your computer and use it in GitHub Desktop.

Select an option

Save robrocker7/4624178 to your computer and use it in GitHub Desktop.
def get_artists(self, profile, limit=10, offset=0):
#logging.debug('get_artists: query playlist')
playlist = memcache.get('station_%s_artist' % self.pk)
if not playlist:
#logging.debug('get_artists: caching playlist')
playlist = list(PlaylistEntry.objects.filter(station_id=self.pk).order_by('rank'))
memcache.add('station_%s_artist' % self.pk, playlist, 60*60*4)
#logging.debug('get_artists: query playlist end')
# maybe query once for a list of tracks and work from this list?
#logging.debug('get_artists: query tracks')
#tracks = memcache.get('station_%s_artist_tracks' % self.pk)
#if not tracks:
# #logging.debug('get_artists: caching tracks')
# tracks = list(Track.objects.filter(pk__in=[item.track_id for item in playlist]))
# tracks.sort(key=operator.attrgetter('popularity'), reverse=True)
# memcache.add('station_%s_artist_tracks' % self.pk, tracks, 60*60*4)
#logging.debug('get_artists: query tracks end')
artists = memcache.get('station_%s_artist_objects' % self.pk)
if not artists:
artists = list(Artist.objects.filter(pk__in=[item.artist_id for item in playlist]).order_by('rank'))
memcache.add('station_%s_artist_objects' % self.pk, artists, 60*60*4)
artist_list = []
rank_tps = []
#logging.debug('get_artist: start profile additions')
if profile:
artist_tps = TasteProfileEntry.objects.filter(user_id=profile.user.id,
station_id=self.pk,
artist_id__gt=0)
ex_artist_ids = [tp.artist_id for tp in artist_tps if tp.action == 'remove']
artists = [a for a in artists if a.artist_id not in ex_artist_ids]
logging.info(artists)
added_artists = list(Artist.objects.filter(pk__in=[tp.artist_id for tp in artist_tps if tp.action == 'add']
).exclude(pk__in=[a.pk for a in artists]))
logging.info(added_artists)
rank_tps = TasteProfileEntry.objects.filter(user_id=profile.user.id,
station_id=self.id,
action="rank",
artist_id__gt=0).exclude(
artist_id__in=ex_artist_ids).order_by('rank')
#logging.debug('get_artist: end profile additions')
#logging.debug('get_artist: apply profile additions to tracks list')
artists = chain(artists, added_artists)
#logging.debug('get_artist: start ranking')
result = self.apply_ranking(artists, rank_tps, "artists")[offset:(limit + offset)]
#logging.debug('get_playlist: json')
results_by_json = [{'rank': r['rank'], 'obj': r['obj'].to_json()} for r in result]
#logging.debug('get_playlist: json end')
return results_by_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment