SteamID conversions

Post Python examples to help other users.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

SteamID conversions

Postby iPlayer » Mon Aug 22, 2016 8:42 pm

Simple stuff

Syntax: Select all

from steam import SteamID


...

steamid = SteamID.parse(player.steamid) # Parse a string containing one of multiple SteamID text representations
print("SteamID64: {}".format(steamid.to_uint64())) # SteamID64: 76561198026369554 -- also known as CommunityID. Is an int!
print("SteamID2: {}".format(steamid.to_steamid2())) # SteamID2: STEAM_1:0:33051913 -- was STEAM_0:0:33051913 (note first zero) before
print("SteamID3: {}".format(steamid.to_steamid3())) # SteamID3: [U:1:66103826]


Heavy weapons stuff - custom profile URL

Syntax: Select all

import json
from urllib.request import urlopen

from steam import SteamID


STEAM_WEB_API_KEY = "XXXXXXXXXXXXXXXXXXXXXXX" # Get your own @ http://steamcommunity.com/dev/apikey
STEAM_GET_PLAYER_SUMMARIES_URL = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={api_key}&steamids={steamid}"

...

steamid64 = SteamID.parse(player.steamid).to_uint64()

with urlopen(STEAM_GET_PLAYER_SUMMARIES_URL.format(api_key=STEAM_WEB_API_KEY, steamid=steamid64)) as response:
profile_info = json.loads(response.read().decode('utf-8'))['response']['players'][0]

print("Custom URL: {}".format(profile_info['profileurl'])) # Custom URL: http://steamcommunity.com/id/its_iPlayer/

I'd also like to note that trying to work with SteamID64 as an integer in languages like JavaScript will lead to last digits being equal to zeros - that's because JS can't natively handle big numbers.

Oh, and this code will fail every Tuesday due to Steam maintenance.

Bonus. Sample response for GetPlayerSummaries:

Syntax: Select all

{
"response": {
"players": [
{
"steamid": "76561198026369554",
"communityvisibilitystate": 3,
"profilestate": 1,
"personaname": "iPlayer",
"lastlogoff": 1471880022,
"commentpermission": 1,
"profileurl": "http://steamcommunity.com/id/its_iPlayer/",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/13/13aef0f2b873850870d1466efacbe83bbb7664b0.jpg",
"avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/13/13aef0f2b873850870d1466efacbe83bbb7664b0_medium.jpg",
"avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/13/13aef0f2b873850870d1466efacbe83bbb7664b0_full.jpg",
"personastate": 1,
"primaryclanid": "103582791431436014",
"timecreated": 1275902481,
"personastateflags": 0
}
]

}
}
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: SteamID conversions

Postby decompile » Mon Aug 22, 2016 10:45 pm

Cool stuff, good to know.

What type of exception will it fire, due the maintenance?
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: SteamID conversions

Postby iPlayer » Tue Aug 23, 2016 5:47 am

Long story short, OSError. To be more specific, one of the following:
https://docs.python.org/3/library/urllib.error.html
Or probably some socket errors, which are all subclasses of OSError, too.
There was a big set of differect exceptions raised by urlopen back in Python 2 days. Good thing it seems they all subclass one class today.

And, who knows, Steam might also return invalid JSON or JSON object that will not contain "response" or "players" fields or there will be no players. To handle invalid JSON - ValueError, to handle missing fields - KeyError, to handle "no players" case - IndexError.

So your try-except will look like

Syntax: Select all

try:
...
except (OSError, ValueError, KeyError, IndexError):
# do something

Or, even better, split this into multiple try sections which do something useful.

Don't forget to implement threading.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Doldol
Senior Member
Posts: 201
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: SteamID conversions

Postby Doldol » Wed Aug 24, 2016 7:01 pm

I use this library https://pypi.python.org/pypi/steam (more specifically http://steam.readthedocs.io/en/stable/a ... eamid.html) to do SteamID conversions, but it's additionally capable of handling all kinds of interaction with Steam, I highly recommend checking it out.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: SteamID conversions

Postby iPlayer » Wed Aug 24, 2016 7:11 pm

Thanks, I might use that lib in my Flask application. However, on SP side, I'd stick to built-in features.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
battleweaver
Member
Posts: 43
Joined: Mon Oct 02, 2017 7:57 am
Location: Moscow
Contact:

Re: SteamID conversions

Postby battleweaver » Thu Oct 12, 2017 12:27 pm

Greetings.
I noticed in sample you use "player.steamid" property, while in example of response you provided i see players { steamid: ... }

And thank you for your guide!

UPD: now i see my mistake.
Last edited by battleweaver on Thu Oct 12, 2017 12:41 pm, edited 1 time in total.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: SteamID conversions

Postby iPlayer » Thu Oct 12, 2017 12:38 pm

Hello there and welcome to the forums!

player.steamid property is a property of the SP object (it contains a string that is later converted to SteamID64).

It doesn't relate to the format of the response sent back by Steam API. The response contains all available player info, including their SteamID64.

The response field is accessed in this piece of code:

Syntax: Select all

profile_info = json.loads(response.read().decode('utf-8'))['response']['players'][0]
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1534
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: SteamID conversions

Postby L'In20Cible » Thu Oct 12, 2017 12:39 pm

Hey, welcome to the forums!
battleweaver wrote:I noticed in sample you use "player.steamid" property, while in example of response you provided i see players { steamid: ... }
In the sample code, player.steamid is given as an example value of Player.steamid while the other part is the response of the request from api.steampowered.com; they are unrelated. The first code show you how to do the conversion and the other how to request info from steam api.

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 12 guests