o
    >{Fe:D                     @   s@  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ ejdk r8eZneZG dd deZd	ej Zd
ZdZee ZeeeeZeeeeZdZG dd deZ dddZ!dd Z"dd Z#e	j$fddZ%e&e%e _%e	j$fddZ'e&e'e _'dddZ(dddZ)e&e)e _)dS )     N)SteamIntEnum)EType	EUniverseEInstanceFlag)md5_hash)make_requests_session)   c                   @   s\   e Zd ZejZejZejZ	ej
ZejZejZejZejZejZejZejZejZdd ZdS )	ETypeCharc                 C      | j S N)nameself r   8/usr/local/lib/python3.10/dist-packages/steam/steamid.py__str__   s   zETypeChar.__str__N)__name__
__module____qualname__r   InvalidI
IndividualU	MultiseatM
GameServerGAnonGameServerAPendingPContentServerCClangChatTLcAnonUserar   r   r   r   r   r	      s    r	    0123456789abcdefbcdfghjkmnpqrtvw ABCDEFGHJKLMNPQRSTUVWXYZ23456789c                       s   e Zd ZdZeZeZeZ fddZdd Zdd Z	dd	 Z
ed
d Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zed"d# Zed$d% Zd&d' Z  ZS )(SteamIDa  
    Object for converting steamID to its' various representations

    .. code:: python

        SteamID()  # invalid steamid
        SteamID(12345)  # accountid
        SteamID('12345')
        SteamID(id=12345, type='Invalid', universe='Invalid', instance=0)
        SteamID(103582791429521412)  # steam64
        SteamID('103582791429521412')
        SteamID('STEAM_1:0:2')  # steam2
        SteamID('[g:1:4]')  # steam3
    c                    s    t |i |}tt| | |S r   )make_steam64superr/   __new__)clsargskwargssteam64	__class__r   r   r2   >   s   zSteamID.__new__c                 O   s   d S r   r   )r   r4   r5   r   r   r   __init__B   s   zSteamID.__init__c                 C   s   t t| S r   )strintr   r   r   r   r   E      zSteamID.__str__c                 C   s*   d| j j| jt| jjt| jj| jf S )Nz,%s(id=%s, type=%s, universe=%s, instance=%s))r8   r   idreprtyper   universeinstancer   r   r   r   __repr__H   s   

zSteamID.__repr__c                 C      t | d@ S B
        :return: account id
        :rtype: :class:`int`
            r;   r   r   r   r   r=   Q      z
SteamID.idc                 C   rC   rD   rG   r   r   r   r   
account_idY   rH   zSteamID.account_idc                 C   s   t | d? d@ S )z&
        :rtype: :class:`int`
             rG   r   r   r   r   rA   a   s   zSteamID.instancec                 C      t t| d? d@ S )z6
        :rtype: :py:class:`steam.enum.EType`
        4      )r   r;   r   r   r   r   r?   h      zSteamID.typec                 C   rL   )z:
        :rtype: :py:class:`steam.enum.EUniverse`
        8      )r   r;   r   r   r   r   r@   o   rO   zSteamID.universec                 C   r
   )rE   )r=   r   r   r   r   as_32v   s   zSteamID.as_32c                 C   s   t | S )zF
        :return: steam64 format
        :rtype: :class:`int`
        rG   r   r   r   r   as_64~   s   zSteamID.as_64c                 C   s    dt | j| jd | jd? f S )a|  
        :return: steam2 format (e.g ``STEAM_1:0:1234``)
        :rtype: :class:`str`

        .. note::
            ``STEAM_X:Y:Z``. The value of ``X`` should represent the universe, or ``1``
            for ``Public``. However, there was a bug in GoldSrc and Orange Box games
            and ``X`` was ``0``. If you need that format use :attr:`SteamID.as_steam2_zero`


        zSTEAM_%d:%d:%d      )r;   r@   r=   r   r   r   r   	as_steam2   s
   zSteamID.as_steam2c                 C   s   | j ddS )z
        For GoldSrc and Orange Box games.
        See :attr:`SteamID.as_steam2`

        :return: steam2 format (e.g ``STEAM_0:0:1234``)
        :rtype: :class:`str`
        _1_0)rV   replacer   r   r   r   as_steam2_zero   s   	zSteamID.as_steam2_zeroc                 C   s   t t| j}d}| jtjtjfv r| j}n)| jtjkr%| jdkr$| j}n| jtjkr?| jt	j
@ r4d}n| jt	j@ r=d}nd}|t| j| jg}|durQ|| ddtt | S )zZ
        :return: steam3 format (e.g ``[U:1:1234]``)
        :rtype: :class:`str`
        NrU   r(   r'   r&   z[%s]:)r:   r	   r?   r   r   r   rA   r   r%   r   r#   Lobbyr;   r@   r=   appendjoinmap)r   typecharrA   partsr   r   r   	as_steam3   s$   

zSteamID.as_steam3c                 C   sp   | j tjkr4|  r6dd }tdt d |d| j }t|d }|r2|d| d ||d  }|S dS dS )	zc
        :return: s.team invite code format (e.g. ``cv-dgb``)
        :rtype: :class:`str`
        c                 S      t |   S r   )
_icode_mapgroupxr   r   r   repl_mapper   r<   z+SteamID.as_invite_code.<locals>.repl_mapper[]z%xrT   N-)	r?   r   r   is_validresub
_icode_hexr=   len)r   rh   invite_code	split_idxr   r   r   as_invite_code   s   zSteamID.as_invite_codec           	      C   s  | j tjks
|  sdS dtd| j }tdt|ddd dd \}| j	}d}t
dD ]*}||d ? d	@ }||? d
@ }|d> |B }|d? d> |B }|d? d> |d
> |B B }q0tdtd|\}d}t
dD ]}|dv rv|d7 }|t|d@  7 }|d? }ql|dd S )z_
        :return: CS:GO Friend code (e.g. ``AEBJA-ABDC``)
        :rtype: :class:`str`
        Ns   CSGOz>Lz<L   r      rN   rU      rJ      <Q>Qr+      )ru   	   rk      )r?   r   r   rl   structpackrI   unpackr   rS   range_csgofrcode_chars)	r   hsteamidresultiid_nibhash_nibr*   coder   r   r   as_csgo_friend_code   s(   $
zSteamID.as_csgo_friend_codec                 C   s   | j }|r	d| S dS )zW
        :return: e.g ``https://s.team/p/cv-dgb``
        :rtype: :class:`str`
        zhttps://s.team/p/N)rs   )r   r   r   r   r   
invite_url   s   zSteamID.invite_urlc                 C   s6   t jdt jdi}| j|v rd|| j  }|| j S dS )zi
        :return: e.g https://steamcommunity.com/profiles/123456789
        :rtype: :class:`str`
        zprofiles/%szgid/%szhttps://steamcommunity.com/%sN)r   r   r#   r?   rS   )r   suffixurlr   r   r   community_url   s   

zSteamID.community_urlc                 C   s   | j tjks| j tjkrdS | jtjks| jtjkrdS | j tjkr.| jdks,| jdkr.dS | j tj	kr@| jdks>| jdkr@dS | j tj
krM| jdkrMdS | j tjkr_| jdkr_| jdkr_dS dS )zW
        Check whether this SteamID is valid

        :rtype: :py:class:`bool`
        Fr   ru   T)r?   r   r   Maxr@   r   r   r=   rA   r#   r   r   r   r   r   r   rl     s"   
zSteamID.is_valid)r   r   r   __doc__r   r   r   r2   r9   r   rB   propertyr=   rI   rA   r?   r@   rR   rS   rV   rZ   rb   rs   r   r   r   rl   __classcell__r   r   r7   r   r/   +   sL    	












!
	
r/   c           
      O   s  | }t j}tj}d}t|dkrqt|dkrqt|}| r]t|}d|  k r,dk r7n n	|}t j}tj}ne|dk rZt|d@ }t|d? d@ }t|d? d	@ }t|d
? d@ }nBd}n?t	|pdt
|}|rn|\}}}}n.d}n+t|dkrt|}	|	dkr|\}n|	dkr|\}}n|	dkr|\}}}ntd|	 t|dkr|d|}|d|}|d|}t|tt frt |nt | }t|ttfrt|nt| }|du r|t jt jfv rdnd}|dksJ d|d
> |d> B |d> B |B S )a  
    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
    Nr           l            rF   rJ   rK   rM   rN   rP   rQ   rU   rT   r   z$Takes at most 4 arguments (%d given)r?   r@   rA   zinstance larger than 20bits)r   r   r   rp   r:   isdigitr;   r   Publicsteam2_to_tuplesteam3_to_tuple	TypeErrorget
isinstancer   )
r=   r4   r5   	accountidetyper@   rA   valuer   lengthr   r   r   r0   .  sf   


r0   c                 C   sb   t d| }|s
dS t|dd> t|dB }t|d}|dkr'd}|tdt|dfS )a  
    :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`
    z9^STEAM_(?P<universe>\d+):(?P<reminder>[0-1]):(?P<id>\d+)$Nr=   rU   reminderr@   r   )rm   matchr;   re   r   r   )r   r   steam32r@   r   r   r   r     s   
 r   c                 C   s   t dt | }|sdS t|d}tt|d}|ddd}tt| }|d}|d	v r7d
}n&|dur@t|}n|dkrHt	j
}n|dkrPt	j}n|tjtjfv r[d}nd
}t|}||||fS )z
    :param value: steam3 (e.g. ``[U:1:1234]``)
    :type value: :class:`str`
    :return: (accountid, type, universe, instance)
    :rtype: :class:`tuple` or :class:`None`
    zO^\[(?P<type>[i%s]):(?P<universe>[0-4]):(?P<id>\d{1,10})(:(?P<instance>\d+))?\]$Nr=   r@   r?   r   r   rA   gTr   r'   r(   rU   )rm   r   
ETypeCharsr;   re   r   rY   r   r	   r   r\   r#   r   r   )r   r   r   r@   r`   r   rA   r   r   r   r     s2   

r   c                 C   s   | sdS t dt d t d | }|sdS |dp|ddd} d	d
 }tt dt d || d}d|  k r@dk rMn dS t|t	j
t|dS dS )a|  
    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`
    Nz!(https?://s\.team/p/(?P<code1>[\-z]+))|(?P<code2>[\-z]+$)code1code2rk   r+   c                 S   rc   r   )_icode_map_invre   rf   r   r   r   rh     r<   z%from_invite_code.<locals>.repl_mapperri   rj      r   r   rU   )rm   r   _icode_all_validre   rY   r;   rn   _icode_customr/   r   r   r   )r   r@   mrh   r   r   r   r   from_invite_code  s"   r   c                 C   s   t dt d | sdS d|  dd} d}tdD ]}t| | }|d	kr* dS ||d
| > B }qtdtd|\}d}tdD ]}|d? }|d@ }|d? }|d> |B }qDt	|t
jt|dS )a8  
    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`
    z^[z\-]{10}$NzAAAA-rk   r+   r   r{   rt   r}   ry   rz   rv   rU   rN   ru   )rm   r   r   rY   r   findr~   r   r   r/   r   r   r   )r   r@   r   r   indexr   r   r   r   r   from_csgo_friend_code  s"   r      c                 C   s   t d| }|s
dS t }zJ|ddv r:|j|d|dj}t d|}|r7t|d}t	|d	 W S W dS |j|d|dj}t d
|}|rUt	|d	W S W dS  t
jjyc   Y dS w )a  
    Takes a Steam Community url and returns steam64 or None

    .. warning::
        Each call makes a http request to ``steamcommunity.com``

    .. note::
        For a reliable resolving of vanity urls use ``ISteamUser.ResolveVanityURL`` web api

    :param url: steam community url
    :type url: :class:`str`
    :param http_timeout: how long to wait on http request before turning ``None``
    :type http_timeout: :class:`int`
    :return: steam64, or ``None`` if ``steamcommunity.com`` is down
    :rtype: :class:`int` or :class:`None`

    Example URLs::

        https://steamcommunity.com/gid/[g:1:4]
        https://steamcommunity.com/gid/103582791429521412
        https://steamcommunity.com/groups/Valve
        https://steamcommunity.com/profiles/[U:1:12]
        https://steamcommunity.com/profiles/76561197960265740
        https://steamcommunity.com/id/johnc
        https://steamcommunity.com/user/cv-dgb/
    zn^(?P<clean_url>https?://steamcommunity.com/(?P<type>profiles|id|gid|groups|user)/(?P<value>.*?))(?:/(?:.*)?)?$Nr?   )r=   profilesuser	clean_url)timeoutz)g_rgProfileData = (?P<json>{.*?});[ 	]*
jsonr   z#OpenGroupChat\( *'(?P<steamid>\d+)')rm   r   r   re   r   textsearchr   loadsr;   requests
exceptionsRequestException)r   http_timeoutr   webr   
data_matchdatar   r   r   steam64_from_url  s,   r   c                 C   s   t | |}|rt|S dS )a  
    Takes Steam community url and returns a SteamID instance or ``None``

    See :py:func:`steam64_from_url` for details

    :param url: steam community url
    :type url: :class:`str`
    :param http_timeout: how long to wait on http request before turning ``None``
    :type http_timeout: :class:`int`
    :return: `SteamID` instance
    :rtype: :py:class:`steam.SteamID` or :class:`None`

    N)r   r/   )r   r   r6   r   r   r   from_urlI  s   
r   )r   )r   )*r~   r   sysrm   r   steam.enums.baser   steam.enumsr   r   r   steam.core.cryptor   steam.utils.webr   version_infolongintBaser;   r	   r^   __members__keysr   ro   r   r   dictziprd   r   r   r/   r0   r   r   r   r   staticmethodr   r   r   r   r   r   r   <module>   sB    
  
Z)

"

8