如何从 python 中的 steamID 获取 steamid 64

How can I get a steamid 64 from a steamID in python

我有一个如下所示的 Steam IDS 列表:

STEAM_1:0:61759597
STEAM_1:1:20263946
STEAM_1:0:105065707

对于所有对 steam API 的调用,你需要给它一个 steamID64,它们看起来像这样:

76561198083784922
76561198000793621
76561198170397142

Steam 页面显示了如何将 SteamID 转换为 SteamID64 的示例 https://developer.valvesoftware.com/wiki/SteamID

As a 64-bit integer
Given the components of a Steam ID, a Steam ID can be converted to it's 64-bit integer form as follows: 
((Universe << 56) | (Account Type << 52) | (Instance << 32) | Account ID)
Worked Example:
Universe: Public (1)
Account Type: Clan (7)
Instance: 0
Account ID: 4
64-bit integer value: 103582791429521412

我的问题是如何在 python 中实现它。我不明白这里发生了什么。

为了让我的问题更清楚,我想从 STEAM_1:0:61759597 开始解析 SteamID64,即 76561198083784922

我知道这是可能的,因为有很多网站都这样做: https://steamid.io/, http://steamidfinder.com/, https://steamid.co/

这么多问题是:这个算法是做什么的,我如何在 python 中实现它?

更新

这是我现在的代码,没有按预期工作:

steamID = "STEAM_1:0:61759597"
X = int(steamID[6:7])
Y = int(steamID[8:9])
Z = int(steamID[10:])

#x is 1
#y is 0
#z is 61759597

print(X,Y,Z)

print((X << 56) | (Y << 52) | (Z << 32) | 4)
#current output: 265255449329139716
#desired output: 76561198083784922

实现实际上与 Steam wiki 上显示的完全相同:

>>> (1 << 56) | (7 << 52) | (0 << 32) | 4
103582791429521412

<<| 在维基百科上是 bitwise operators, performing a left shift and a bitwise OR, respectively. You can learn a lot more about bitwise operations

就将任意 Steam ID 转换为 64 位系统而言,我发现了这个 gist

def steamid_to_64bit(steamid):
    steam64id = 76561197960265728 # I honestly don't know where
                                    # this came from, but it works...
    id_split = steamid.split(":")
    steam64id += int(id_split[2]) * 2 # again, not sure why multiplying by 2...
    if id_split[1] == "1":
        steam64id += 1
    return steam64id

In [14]: steamid_to_64bit("STEAM_1:0:61759597")
Out[14]: 76561198083784922

In [15]: steamid_to_64bit("STEAM_1:1:20263946")
Out[15]: 76561198000793621

In [16]: steamid_to_64bit("STEAM_1:0:105065707")
Out[16]: 76561198170397142

一个steam forum post中有说明如何转换,我们只关心最后两个数字:

因此,

ID3 是 ID64 基数(76561197960265728)的偏移量,是帐号。 第一次注册账号:(76561197960265728+1,76561197960265728不存在)

    ID64 = 76561197960265728 + (B * 2) + A
    ID3 = (B * 2) + A
    ID32 = STEAM_0:A:B

所以你只需要:

def to_steam64(s):
    return ((b * 2) + a) + 76561197960265728

要走相反的路线,来自 steam64:

def from_steam64(sid):
    y = int(sid) - 76561197960265728
    x = y % 2 
    return "STEAM_0:{}:{}".format(x, (y - x) // 2)

这是一个转换tablehere