Home > Back-end >  "List index out of range" for Django Steam API
"List index out of range" for Django Steam API

Time:10-31

I'm working on an app in Django that lets you see stuff about a steam user, when typing in their steam ID.

I'm using the Django Steam API found at this link: https://pypi.org/project/django-steam-api/ which, unfortunately, doesn't provide some documentation / enough help on how to properly set it up.

I followed the steps from there. Installed the pip package, added django-steam-api to the INSTALLED_APPS tuple, generated a STEAM_API_KEY from their official site (on "localhost", because my web app isn't hosted at this moment) and also updated the database by applying the current migrations.

I'm having a view in backend/views.py, (backend being my app name created with the django-admin startapp backend command), that takes data from a form that contains a textbox called steam_user_id using the POST method.

I'm trying to get everything from Steam about the user with the id steam_user_id, but when I call, inside the view, the method:

Player.objects.steam_create(steam_user_id)

(...) I receive the following error on my page:

IndexError at {{ /link_to_my_view/ - replaced it manually here to avoid complications }} list index out of range Request Method: POST Request URL: http://127.0.0.1:8000/profile/edit/ Django Version: 3.2.7 Exception Type: IndexError Exception Value: list index out of range Exception Location: C:\Users\matea\AppData\Local\Programs\Python\Python39\lib\site-packages\django_steam_api\managers.py, line 21, in steam_create Python Executable: C:\Users\matea\AppData\Local\Programs\Python\Python39\python.exe Python Version: 3.9.7 Python Path: ['E:\git\gameprofile', 'C:\Users\matea\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\matea\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\matea\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\matea\AppData\Local\Programs\Python\Python39', 'C:\Users\matea\AppData\Local\Programs\Python\Python39\lib\site-packages'] Server time: Sat, 30 Oct 2021 22:09:37 0000

The Traceback leads to the line of code that contains Player.objects.steam_create(list(steam_user_id)) .

What did I do wrong? I also tried converting steam_user_id to a list (list(steam_user_id)), but it still didn't work.

Any opinions or solutions? Help would be greatly appreciated. Thank you.

CodePudding user response:

I would suggest you to not to use this package, there can be issues espicially if you are the only user(which probably you are) second if you absolutely need to use this one maybe opening an issue in github would be more appropriate If you check the source code you will see whats wrong with this Django Steam API. Method that giving you error is here https://github.com/voblivion/django-steam-api/blob/master/django_steam_api/managers.py

This is really unnecessary package in my opinion STEAM_API_KEY variable might be the issue

def steam_query(self, url, params={}):
    params['key'] = settings.STEAM_API_KEY
    raw_data = requests.get(url, params)
    data = raw_data.json()
    return data['response']

Does this code works when you try to run it via shell? By the way just read all the package files it is actually really small, best would be to implement and fix issues in your project.

CodePudding user response:

The exception is raised in the django-steam-api only, where get request is returning the response like {"players": []} and on top of that api is checking the players data at zero index which raise the below exception.

On below line :- https://github.com/voblivion/django-steam-api/blob/master/django_steam_api/managers.py#L21

If you want to test it locally you can fix the code like :-

def steam_create(self, steam_id):
    raw_data = self.steam_query(self.player_url, {'steamids': steam_id})
    # add a small check here.
    if 'players' in raw_data and raw_data['players']:
        raw_data = raw_data['players'][0]
        data = {
            'id': raw_data['steamid'],
            'username': raw_data['personaname'],
            'profile': raw_data['profileurl'],
            'avatar_small': raw_data['avatar'],
            'avatar_medium': raw_data['avatarmedium'],
            'avatar_large': raw_data['avatarfull'],
            'state': raw_data['personastate'],
            'is_public': raw_data['communityvisibilitystate'] == 3,
        }
        return self.create(**data)
  • Related