Get steam wishlist by username

Hello, in this post I extend previously created script to accept also steam username. 

At first, I need to get Steam API token, a token can be requested here 

Next, I create the config class in a separate file to store all configuration there. Here is an example

import os


class Config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'secret_key'
    RESOLVE_VANITY = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001'
    GET_PLAYER_SUMMARIES_API = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v1'
    API_KEY = os.environ.get('API_KEY')

As you can see here is the SECRET_KEY, this is not used in the current script but used in flask website, which will be created in the next posts. RESOLVE_VANITY url is used to get steam id from the username. And GET_PLAYER_SUMMARIES is used to verify if a user exists. After config is created it needs to be imported in my script.

from config import Config

Also, the requests package will be used

import requests

As the next step, I need to create get_steam_id function and do request to RESOLVE_VANITY url to get steam id from the username and raise an error in case something goes wrong, and get response field from request. Also, I want to have the possibility to use steam id too.

def get_steam_id(identificator, api_key):
    steamid = requests.get(
        f'{Config.RESOLVE_VANITY}/?key={api_key}&vanityurl={identificator}'
    )
    steamid.raise_for_status()

    steamid_response = steamid.json()['response']

Next, I am going to add a condition to check the content of steam_id response. 

if (
        'message' in steamid_response) and (
        steamid_response['message'] == 'No match'
):
    steamid = identificator
else:
    steamid = steamid_response['steamid']

What is checking if there message key in steamid_responce and content of message equal Not match python would try to use provided steam username as steam id. Else steamid will be equal to steamid from the response. 

Next, I check if a user with steamid exist using GET_PLAYER_SUMMARIES_API url

test_user_exist = requests.get(
    f'{Config.GET_PLAYER_SUMMARIES_API}/?key={api_key}&steamids={steamid}'
).json()['response']['players']

And based on response verify if a user exists and return None if not exist, and return steamid if a user exists.

if 'player' in test_user_exist and test_user_exist['player'] == [None]:
    return None

return steamid

Function to get a steam wishlist will stay the same as in the previous post.