Page 1 of 1

SteamID conversions

Posted: Mon Aug 22, 2016 8:42 pm
by iPlayer
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
}
]

}
}

Re: SteamID conversions

Posted: Mon Aug 22, 2016 10:45 pm
by decompile
Cool stuff, good to know.

What type of exception will it fire, due the maintenance?

Re: SteamID conversions

Posted: Tue Aug 23, 2016 5:47 am
by iPlayer
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.

Re: SteamID conversions

Posted: Wed Aug 24, 2016 7:01 pm
by Doldol
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.

Re: SteamID conversions

Posted: Wed Aug 24, 2016 7:11 pm
by iPlayer
Thanks, I might use that lib in my Flask application. However, on SP side, I'd stick to built-in features.

Re: SteamID conversions

Posted: Thu Oct 12, 2017 12:27 pm
by battleweaver
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.

Re: SteamID conversions

Posted: Thu Oct 12, 2017 12:38 pm
by iPlayer
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]

Re: SteamID conversions

Posted: Thu Oct 12, 2017 12:39 pm
by L'In20Cible
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.