예제 #1
0
def process_friend_page(user, resp):
    print 'processing friend page for %s' % user
    '''user is a UserProfile object
    resp is of this format:
    {u'friends': {u'@attr': {u'for': u'havok07',
                         u'page': u'1',
                         u'perPage': u'50',
                         u'total': u'22',
                         u'totalPages': u'1'},
              u'user': [{u'image': [{u'#text': u'http://userserve-ak.last.fm/serve/34/57061383.jpg',
                                     u'size': u'small'},
                                    {u'#text': u'http://userserve-ak.last.fm/serve/64/57061383.jpg',
                                     u'size': u'medium'},
                                    {u'#text': u'http://userserve-ak.last.fm/serve/126/57061383.jpg',
                                     u'size': u'large'},
                                    {u'#text': u'http://userserve-ak.last.fm/serve/252/57061383.jpg',
                                     u'size': u'extralarge'}],
                         u'name': u'Lauren_Jaye',
                         u'realname': u'Lauren Miller',
                         u'url': u'http://www.last.fm/user/Lauren_Jaye'},
                        {u'image': [{u'#text': u'', u'size': u'small'},
                                    {u'#text': u'', u'size': u'medium'},
                                    {u'#text': u'', u'size': u'large'},
                                    {u'#text': u'', u'size': u'extralarge'}],
                         u'name': u'sysm',
                         u'realname': u'',
                         u'url': u'http://www.last.fm/user/sysm'}
                        ]}}'''
    for friend_data in resp.get('friends').get('user'):
        try:
            friend = UserProfile.objects.get(
                lfm_username=friend_data.get('name'))
        except ObjectDoesNotExist:
            friend = UserProfile(lfm_username=friend_data.get('name'))
            friend.url = friend_data.get('url')
            friend.name = friend_data.get('realname')

            image = None
            if friend_data.get('image')[0].get('#text'):
                image = Image(url=friend_data.get('image')[0].get('#text'))

            friend.image = image
            friend.save()

            get_for_user(friend.lfm_username)
        user.friends.add(friend)

    if resp.get('friends').get('@attr').get('totalPages') == resp.get(
            'friends').get('@attr').get('page'):
        get_friends_data(user)
예제 #2
0
def process_friend_page(user, resp):
    print 'processing friend page for %s'%user
    '''user is a UserProfile object
    resp is of this format:
    {u'friends': {u'@attr': {u'for': u'havok07',
                         u'page': u'1',
                         u'perPage': u'50',
                         u'total': u'22',
                         u'totalPages': u'1'},
              u'user': [{u'image': [{u'#text': u'http://userserve-ak.last.fm/serve/34/57061383.jpg',
                                     u'size': u'small'},
                                    {u'#text': u'http://userserve-ak.last.fm/serve/64/57061383.jpg',
                                     u'size': u'medium'},
                                    {u'#text': u'http://userserve-ak.last.fm/serve/126/57061383.jpg',
                                     u'size': u'large'},
                                    {u'#text': u'http://userserve-ak.last.fm/serve/252/57061383.jpg',
                                     u'size': u'extralarge'}],
                         u'name': u'Lauren_Jaye',
                         u'realname': u'Lauren Miller',
                         u'url': u'http://www.last.fm/user/Lauren_Jaye'},
                        {u'image': [{u'#text': u'', u'size': u'small'},
                                    {u'#text': u'', u'size': u'medium'},
                                    {u'#text': u'', u'size': u'large'},
                                    {u'#text': u'', u'size': u'extralarge'}],
                         u'name': u'sysm',
                         u'realname': u'',
                         u'url': u'http://www.last.fm/user/sysm'}
                        ]}}'''
    for friend_data in resp.get('friends').get('user'):
        try:
            friend = UserProfile.objects.get(lfm_username=friend_data.get('name'))
        except ObjectDoesNotExist:
            friend = UserProfile(lfm_username=friend_data.get('name'))
            friend.url = friend_data.get('url')
            friend.name = friend_data.get('realname')
            
            image = None
            if friend_data.get('image')[0].get('#text'):
                image = Image(url=friend_data.get('image')[0].get('#text'))
            
            friend.image = image
            friend.save()
            
            get_for_user(friend.lfm_username)
        user.friends.add(friend)
        
    if resp.get('friends').get('@attr').get('totalPages') == resp.get('friends').get('@attr').get('page'):
        get_friends_data(user)
예제 #3
0
def get_top100(request):
    colors = COLORS[:]
    print 'getting top 100'
    retval = None
    username = request.GET.get('username')
    try:
        user = UserProfile.objects.get(lfm_username=username)
    except ObjectDoesNotExist:
        user = UserProfile(lfm_username=username)
        user.visited = True
        user.save()
        return HttpResponse(json.dumps({'msg':'sorry, loading, come back in 5m'}))
        # send_task()
    # if user.updating_track_week:
    #     retval = json.dumps({'status':'wait'})
    #     
    # elif user.updated_friends_at is None:
    #     job_data = {'uname': username, 'page': 1}
    #     CLIENT.call('lfm.process_friends_page', json.dumps(job_data))
    #     
    # if user.tracks_week_updated_at == None or datetime.datetime.now() - user.tracks_week_updated_at > datetime.timedelta(7):
    #     job_data = {'username': username}
    #     CLIENT.call('lfm.get_user_track_week', json.dumps(job_data))
    #     
    #     
    retval = cache.get('testing')
    # 
    if retval is None:
        (resp,listeners) = helper(username)
    
        resp = resp[:100]
        user_list = []
        user_hash = {}
        for user in listeners:
            user_hash[user.lfm_username] = colors.pop()
            user_list.append(user.lfm_username)

        for artist_dict in resp:
            for listener_dict in artist_dict.get('listeners'):
                listener_dict['listens'].sort(key=lambda x:x.track.duration * x.personal_playcount, reverse=True)
                listener_dict['listens'] = [{'name': k.track.name, 
                            'playcount': k.personal_playcount, 
                            'duration': k.track.duration * k.personal_playcount,
                            'id': k.track.id,
                            } for k in listener_dict['listens']]
                listener_dict['user'] = listener_dict['user'].lfm_username
            
            tracks = []
            for track_key in artist_dict.get('tracks'):
                track_dict = artist_dict['tracks'][track_key]
                tracks.append({'name':track_key, 
                        'duration':track_dict.get('sum_duration'), 
                        'playcount':track_dict.get('sum_playcount'),
                        'listens': [{'user':listen.user_profile.lfm_username, 'playcount':listen.personal_playcount, 'id':listen.track.id} for listen in track_dict.get('listens')],
                        'id': track_dict.get('id'),
                        })
            tracks.sort(key=lambda x:x.get('duration'), reverse=True)
            artist_dict['tracks'] = tracks
            artist_dict['listens'] = []

        retval = json.dumps({'lfmData':resp, 'userHash':user_hash, 'userList':user_list})
        cache.set('testing', retval, 60*60*4)

    return HttpResponse(retval)
예제 #4
0
def get_top100(request):
    colors = COLORS[:]
    print 'getting top 100'
    retval = None
    username = request.GET.get('username')
    try:
        user = UserProfile.objects.get(lfm_username=username)
    except ObjectDoesNotExist:
        user = UserProfile(lfm_username=username)
        user.visited = True
        user.save()
        return HttpResponse(
            json.dumps({'msg': 'sorry, loading, come back in 5m'}))
        # send_task()
    # if user.updating_track_week:
    #     retval = json.dumps({'status':'wait'})
    #
    # elif user.updated_friends_at is None:
    #     job_data = {'uname': username, 'page': 1}
    #     CLIENT.call('lfm.process_friends_page', json.dumps(job_data))
    #
    # if user.tracks_week_updated_at == None or datetime.datetime.now() - user.tracks_week_updated_at > datetime.timedelta(7):
    #     job_data = {'username': username}
    #     CLIENT.call('lfm.get_user_track_week', json.dumps(job_data))
    #
    #
    retval = cache.get('testing')
    #
    if retval is None:
        (resp, listeners) = helper(username)

        resp = resp[:100]
        user_list = []
        user_hash = {}
        for user in listeners:
            user_hash[user.lfm_username] = colors.pop()
            user_list.append(user.lfm_username)

        for artist_dict in resp:
            for listener_dict in artist_dict.get('listeners'):
                listener_dict['listens'].sort(
                    key=lambda x: x.track.duration * x.personal_playcount,
                    reverse=True)
                listener_dict['listens'] = [{
                    'name': k.track.name,
                    'playcount': k.personal_playcount,
                    'duration': k.track.duration * k.personal_playcount,
                    'id': k.track.id,
                } for k in listener_dict['listens']]
                listener_dict['user'] = listener_dict['user'].lfm_username

            tracks = []
            for track_key in artist_dict.get('tracks'):
                track_dict = artist_dict['tracks'][track_key]
                tracks.append({
                    'name':
                    track_key,
                    'duration':
                    track_dict.get('sum_duration'),
                    'playcount':
                    track_dict.get('sum_playcount'),
                    'listens': [{
                        'user': listen.user_profile.lfm_username,
                        'playcount': listen.personal_playcount,
                        'id': listen.track.id
                    } for listen in track_dict.get('listens')],
                    'id':
                    track_dict.get('id'),
                })
            tracks.sort(key=lambda x: x.get('duration'), reverse=True)
            artist_dict['tracks'] = tracks
            artist_dict['listens'] = []

        retval = json.dumps({
            'lfmData': resp,
            'userHash': user_hash,
            'userList': user_list
        })
        cache.set('testing', retval, 60 * 60 * 4)

    return HttpResponse(retval)