Using the Steam API with Python to access your data

0

[ad_1]

Get the Geezam newsletter for tech tutorials, howtos and walkthroughs straight to your inbox

Steam is a pioneer in the field of video game digital distribution. The service was launched in 2003 as a way for Valve to provide automatic updates for their games. In 2005 it expanded to distributing and offering third-party game publishers’ titles. Steam has a powerful application user interface or API that developers can access. Let’s dip our toes into some of the basic functionality of using the Steam API with Python to access your own account’s data.

Steam API Key and your Steam ID

Visit the Steam Web API Documentation to read about the Steam API and how to access your API Key. You can find your Steam ID from the profile tab of the Steam application by going to your profile URL. Your Steam ID is a unique 17-digit number that represents your account. Users can use their Steam ID to link other people to their profile or use third-party apps. It is different from your username.

Steam API with Python

This tutorial assumes you already know the basics of the Python programming language, already applied for a free developer account to get access to the Steam API and know the Steam ID for your account. In a new python file, let’s start by importing the requests and random libraries as well as declaring 2 variables with your Steam API Key and Steam User ID.

import requests
import random

steamApiKey = "YOUR-STEAM-API-KEY"
steamID = "YOUR-STEAM-USER-ID"

Next, let’s format the link we will use to make our Get request to the Steam API and save the response to a variable. Convert the response to a JSON and print the information about games owned by the Steam ID sent to the screen in JSON. Use a JSON view/formatter like this one to better understand the data.

#Steam API link formatting for "GetOwnedGames"
slink1 = "https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key="
slink2 = "&steamid=" + steamID + "&include_appinfo=1&format=json"
slink = slink1 + steamApiKey + slink2

#Sent API Get request and save respond to a variable
r = requests.get(slink)

#convert to JSON and save to another variable
steam = r.json()

#JSON output with information about each game owned
print(steam)

You can use the code below to get an integer value of the number of games registered to your account on Steam. Congratulations you have successfully used the Steam API with Python to access your own account’s data for the games owned.

#Getting integer value of total games owned
totalGames = str(steam["response"]["game_count"])

#Output total games owned
print(totalGames)

Experiment with the Steam API and Python

This is just the tip of the iceberg of the Steam API. Feel free to read more about the API and its access points. There are many things you could do for example only getting games with a certain number of hours played. An example of such functionality can be seen in the full code below. The code from this tutorial is available on Github. Follow @Geezam on Twitter and subscribe to our Youtube channel for more tech tutorials, howtos and walkthroughs.

import requests
import random

steamApiKey = "YOUR-STEAM-API-KEY"
steamID = "YOUR-STEAM-USER-ID"

#Steam API link formatting for "GetOwnedGames"
slink1 = "https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key="
slink2 = "&steamid=" + steamID + "&include_appinfo=1&format=json"
slink = slink1 + steamApiKey + slink2

#Sent API Get request and save respond to a variable
r = requests.get(slink)

#convert to JSON and save to another variable
steam = r.json()

#JSON output with information about each game owned
print(steam)
#https://developer.valvesoftware.com/wiki/Steam_Web_API#GetOwnedGames_.28v0001.29

#Getting integer value of total games owned
totalGames = str(steam["response"]["game_count"])

#Output total games owned
print(totalGames)


#Some logic for getting a random game from account with over 10 hours playtime
steamGame = ""
steamGames = []

for num,item in enumerate (steam["response"]["games"]):
    num+=1
    if item["playtime_forever"] > 600:
        steamGame = item["name"]
        steamGames.append(steamGame)
    
steamRec = "".join((random.sample(steamGames, k=1)))

print(steamRec)

More APIs to explore with Python

Looking for more APIs to experiment with using Python. Check out our tutorials on using the Twitter API with Python and Tweepy to get public data on any account and Reverse Geocoding with Python and Geoapify.

Get the Geezam newsletter for tech tutorials, howtos and walkthroughs straight to your inbox



[ad_2]

Source link

Leave A Reply

Your email address will not be published.