Exemplo n.º 1
0
def search_players(request, search_request):
    if len(search_request) < 2:
        return None
    dajax = Dajax()
    result_set = SteamPlayer.filter_by_name(name=search_request, profileurl=search_request, communityid=search_request)
    result_string = render_to_string('ajax/search_results_players.html', dictionary=dict({'results': result_set}))
    dajax.add_data(result_string, 'render_players')
    return dajax.json()
Exemplo n.º 2
0
def GetPlayerNames(player_ids):
    """Loads player_ids into Steam Account cache.
    This will take in a list of account ids and load the Steam Accounts into db cache from the Steam API.
    
    Args:
        player_ids (list): List of SteamID64s to lookup.
        
    Returns:
        dict. Returns dict of player_ids to persona_names.
    """
    return_dict = dict({4294967295: 'PRIVATE',})
    try:
        request_list = [] # IDs to request from SteamAPI.
        save_list = dict() # IDs to `save` instead of `create`. (To not invalidate FK constraints)
        if type(player_ids) is not list: # Always make sure player_ids is a list.
            player_ids = [player_ids,]
        for player_id in player_ids:
            if player_id == 4294967295 or player_id == None: # Invalid playerid, assume private
                continue
            try:
                player = SteamPlayer.objects.get(pk=player_id)
                if timezone.now() - player.last_refresh > PLAYER_FRESHNESS:
                    request_list.append(player.pk)
                    save_list[str(player_id)] = player
            except ObjectDoesNotExist:
                request_list.append(player_id)
        if len(request_list) != 0: # Only allowed 100 users per request. Must split it up.
            do_this = True
            while do_this == True:
                sliced_list = request_list[:100] # Get first 100 elements
                if len(request_list) > 100:
                    request_list = request_list[100:] # Get everything but first 100 elements.
                else:
                    do_this = False # No longer over 100. Don't need to do this again.
                response = urllib2.urlopen(STEAM_USER_NAMES + '?key=' + API_KEY + '&steamids=' + ','.join(str(req) for req in sliced_list))
                json_data = json.loads(response.read())['response']
                bulk_create = []
                for player in json_data['players']:
                    steamplayer = SteamPlayer.from_json_response(player)
                    if player['steamid'] in save_list: # If object should be saved instead, save now. Bulk_create will raise IntegrityErrors.
                        steamplayer.save()
                    else:
                        bulk_create.append(steamplayer)
                SteamPlayer.objects.bulk_create(bulk_create)
                response.close()
    except urllib2.HTTPError, e:
        if e.code == 400:
            raise SteamAPIError("Malformed API request.")
        elif e.code == 401:
            raise SteamAPIError("Unauthorized API access. Please recheck your API key.")
        elif e.code == 503:
            raise SteamAPIError("The Steam servers are currently overloaded.")
        else:
            raise SteamAPIError("Unknown API error" + str(e.code))
        raise
Exemplo n.º 3
0
def search_players(request, search_request):
    if len(search_request) < 2:
        return None
    dajax = Dajax()
    result_set = SteamPlayer.filter_by_name(name=search_request,
                                            profileurl=search_request,
                                            communityid=search_request)
    result_string = render_to_string('ajax/search_results_players.html',
                                     dictionary=dict({'results': result_set}))
    dajax.add_data(result_string, 'render_players')
    return dajax.json()
Exemplo n.º 4
0
def poll_steamplayers_queue():
    """Celery task that handles the constant background refreshing of SteamPlayers.
    
    This task will take up to 100 old SteamPlayers and update them.
    
    Returns True if work was handled; None if no work to be done.
    """
    account_list = []
    accounts = SteamPlayer.get_refresh()
    for account in accounts:
        account_list.append(account.pk)
    if len(account_list) > 0:
        GetPlayerNames(account_list)
        return True
    return None
Exemplo n.º 5
0
def poll_steamplayers_queue():
    """Celery task that handles the constant background refreshing of SteamPlayers.
    
    This task will take up to 100 old SteamPlayers and update them.
    
    Returns True if work was handled; None if no work to be done.
    """
    account_list = []
    accounts = SteamPlayer.get_refresh()
    for account in accounts:
        account_list.append(account.pk)
    if len(account_list) > 0:
        GetPlayerNames(account_list)
        return True
    return None
Exemplo n.º 6
0
def GetPlayerNames(player_ids):
    """Loads player_ids into Steam Account cache.
    This will take in a list of account ids and load the Steam Accounts into db cache from the Steam API.
    
    Args:
        player_ids (list): List of SteamID64s to lookup.
        
    Returns:
        dict. Returns dict of player_ids to persona_names.
    """
    return_dict = dict({
        4294967295: 'PRIVATE',
    })
    try:
        request_list = []  # IDs to request from SteamAPI.
        save_list = dict(
        )  # IDs to `save` instead of `create`. (To not invalidate FK constraints)
        if type(player_ids
                ) is not list:  # Always make sure player_ids is a list.
            player_ids = [
                player_ids,
            ]
        for player_id in player_ids:
            if player_id == 4294967295 or player_id == None:  # Invalid playerid, assume private
                continue
            try:
                player = SteamPlayer.objects.get(pk=player_id)
                if timezone.now() - player.last_refresh > PLAYER_FRESHNESS:
                    request_list.append(player.pk)
                    save_list[str(player_id)] = player
            except ObjectDoesNotExist:
                request_list.append(player_id)
        if len(request_list
               ) != 0:  # Only allowed 100 users per request. Must split it up.
            do_this = True
            while do_this == True:
                sliced_list = request_list[:100]  # Get first 100 elements
                if len(request_list) > 100:
                    request_list = request_list[
                        100:]  # Get everything but first 100 elements.
                else:
                    do_this = False  # No longer over 100. Don't need to do this again.
                response = urllib2.urlopen(STEAM_USER_NAMES + '?key=' +
                                           API_KEY + '&steamids=' + ','.join(
                                               str(req)
                                               for req in sliced_list))
                json_data = json.loads(response.read())['response']
                bulk_create = []
                for player in json_data['players']:
                    steamplayer = SteamPlayer.from_json_response(player)
                    if player[
                            'steamid'] in save_list:  # If object should be saved instead, save now. Bulk_create will raise IntegrityErrors.
                        steamplayer.save()
                    else:
                        bulk_create.append(steamplayer)
                SteamPlayer.objects.bulk_create(bulk_create)
                response.close()
    except urllib2.HTTPError, e:
        if e.code == 400:
            raise SteamAPIError("Malformed API request.")
        elif e.code == 401:
            raise SteamAPIError(
                "Unauthorized API access. Please recheck your API key.")
        elif e.code == 503:
            raise SteamAPIError("The Steam servers are currently overloaded.")
        else:
            raise SteamAPIError("Unknown API error" + str(e.code))
        raise