def steam3_to_tuple(value): """ :param value: steam3 (e.g. ``[U:1:1234]``) :type value: :class:`str` :return: (accountid, type, universe, instance) :rtype: :class:`tuple` or :class:`None` """ match = re.match(r"^\[" r"(?P<type>[%s]):" # type char r"(?P<universe>\d+):" # universe r"(?P<id>\d+)" # accountid r"(:(?P<instance>\d+))?" # instance r"\]$" % ETypeChars, value ) if not match: return None steam32 = int(match.group('id')) universe = EUniverse(int(match.group('universe'))) etype = ETypeChar[match.group('type')] instance = match.group('instance') if instance is None: if etype in (EType.Individual, EType.GameServer): instance = 1 else: instance = 0 else: instance = int(instance) return (steam32, etype, universe, instance)
def invite_code_to_tuple(code, universe=EUniverse.Public): """ Invites urls can be generated at https://steamcommunity.com/my/friends/add :param code: invite code (e.g. ``https://s.team/p/cv-dgb``, ``cv-dgb``) :type code: :class:`str` :param universe: Steam universe (default: ``Public``) :type universe: :class:`EType` :return: (accountid, type, universe, instance) :rtype: :class:`tuple` or :class:`None` """ if not code: return None m = re.match( r'(https?://s\.team/p/(?P<code1>[\-' + _icode_all_valid + ']+))' r'|(?P<code2>[\-' + _icode_all_valid + ']+$)', code) if not m: return None code = (m.group('code1') or m.group('code2')).replace('-', '') def repl_mapper(x): return _icode_map_inv[x.group()] accountid = int(re.sub("[" + _icode_custom + "]", repl_mapper, code), 16) if 0 < accountid < 2**32: return (accountid, EType(1), EUniverse(universe), 1)
def steam2_to_tuple(value): """ :param value: steam2 (e.g. ``STEAM_1:0:1234``) :type value: :class:`str` :return: (accountid, type, universe, instance) :rtype: :class:`tuple` or :class:`None` .. note:: The universe will be always set to ``1``. See :attr:`SteamID.as_steam2` """ match = re.match( r"^STEAM_(?P<universe>\d+)" r":(?P<reminder>[0-1])" r":(?P<id>\d+)$", value) if not match: return None steam32 = (int(match.group('id')) << 1) | int(match.group('reminder')) universe = int(match.group('universe')) # Games before orange box used to incorrectly display universe as 0, we support that if universe == 0: universe = 1 return (steam32, EType(1), EUniverse(universe), 1)
def from_csgo_friend_code(code, universe=EUniverse.Public): """ Takes CS:GO friend code and returns SteamID :param code: CS:GO friend code (e.g. ``AEBJA-ABDC``) :type code: :class:`str` :param universe: Steam universe (default: ``Public``) :type universe: :class:`EType` :return: SteamID instance :rtype: :class:`.SteamID` or :class:`None` """ if not re.match(r'^['+_csgofrcode_chars+'\-]{10}$', code): return None code = ('AAAA-' + code).replace('-', '') result = 0 for i in range(13): index = _csgofrcode_chars.find(code[i]) if index == -1: return None result = result | (index << 5 * i) result, = struct.unpack('<Q', struct.pack('>Q', result)) accountid = 0 for i in range(8): result = result >> 1 id_nib = result & 0xF result = result >> 4 accountid = (accountid << 4) | id_nib return SteamID(accountid, EType.Individual, EUniverse(universe), 1)
def load(self, data): ( self.protocolVersion, universe, ) = struct.unpack_from("<II", data) self.universe = EUniverse(universe) if len(data) > 8: self.challenge = data[8:]
def steam2_to_tuple(value): """ :param value: steam2 (e.g. ``STEAM_1:0:1234``) :type value: :class:`str` :return: (accountid, type, universe, instance) :rtype: :class:`tuple` or :class:`None` .. note:: The universe will be always set to ``1``. See :attr:`SteamID.as_steam2` """ match = re.match(r"^STEAM_(?P<universe>[01])" r":(?P<reminder>[0-1])" r":(?P<id>\d+)$", value ) if not match: return None steam32 = (int(match.group('id')) << 1) | int(match.group('reminder')) return (steam32, EType(1), EUniverse(1), 1)
def steam3_to_tuple(value): """ :param value: steam3 (e.g. ``[U:1:1234]``) :type value: :class:`str` :return: (accountid, type, universe, instance) :rtype: :class:`tuple` or :class:`None` """ match = re.match( r"^\[" r"(?P<type>[i%s]):" # type char r"(?P<universe>[0-4]):" # universe r"(?P<id>\d{1,10})" # accountid r"(:(?P<instance>\d+))?" # instance r"\]$" % ETypeChars, value) if not match: return None steam32 = int(match.group('id')) universe = EUniverse(int(match.group('universe'))) typechar = match.group('type').replace('i', 'I') etype = EType(ETypeChar[typechar]) instance = match.group('instance') if typechar in 'gT': instance = 0 elif instance is not None: instance = int(instance) elif typechar == 'L': instance = EInstanceFlag.Lobby elif typechar == 'c': instance = EInstanceFlag.Clan elif etype in (EType.Individual, EType.GameServer): instance = 1 else: instance = 0 instance = int(instance) return (steam32, etype, universe, instance)
def universe(self): """ :rtype: :py:class:`steam.enum.EUniverse` """ return EUniverse((int(self) >> 56) & 0xFF)
def make_steam64(id=0, *args, **kwargs): """ Returns steam64 from various other representations. .. code:: python make_steam64() # invalid steamid make_steam64(12345) # accountid make_steam64('12345') make_steam64(id=12345, type='Invalid', universe='Invalid', instance=0) make_steam64(103582791429521412) # steam64 make_steam64('103582791429521412') make_steam64('STEAM_1:0:2') # steam2 make_steam64('[g:1:4]') # steam3 """ accountid = id etype = EType.Invalid universe = EUniverse.Invalid instance = None if len(args) == 0 and len(kwargs) == 0: value = str(accountid) # numeric input if value.isdigit(): value = int(value) # 32 bit account id if 0 < value < 2**32: accountid = value etype = EType.Individual universe = EUniverse.Public # 64 bit elif value < 2**64: return value # textual input e.g. [g:1:4] else: result = steam2_to_tuple(value) or steam3_to_tuple(value) if result: (accountid, etype, universe, instance, ) = result else: accountid = 0 elif len(args) > 0: length = len(args) if length == 1: etype, = args elif length == 2: etype, universe = args elif length == 3: etype, universe, instance = args else: raise TypeError("Takes at most 4 arguments (%d given)" % length) if len(kwargs) > 0: etype = kwargs.get('type', etype) universe = kwargs.get('universe', universe) instance = kwargs.get('instance', instance) etype = (EType(etype) if isinstance(etype, (int, EType)) else EType[etype] ) universe = (EUniverse(universe) if isinstance(universe, (int, EUniverse)) else EUniverse[universe] ) if instance is None: instance = 1 if etype in (EType.Individual, EType.GameServer) else 0 assert instance <= 0xffffF, "instance larger than 20bits" return (universe << 56) | (etype << 52) | (instance << 32) | accountid