o
    e+                  	   @   sl  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 d dl	m
Z
mZ d dlZd dlmZmZ d dlmZ d dlmZ d dlmZmZ d	d
 ZdedededefddZdededefddZdedefddZdedefddZedfdedededefddZedfdedededefd d!Zd"ed#edefd$d%Z d&edefd'd(Z!d)ed*edefd+d,Z"d-ed.ed*edefd/d0Z#d1ee d*edefd2d3Z$d4edefd5d6Z%d7edefd8d9Z&d4edefd:d;Z'd7edefd<d=Z(d4edefd>d?Z)d@edefdAdBZ*dUdDedEedFe+defdGdHZ,dIdJ Z-G dKdL dLZ.dMefdNdOZ/dPedQedRedefdSdTZ0dS )V    N)List)Decimal)urlparseparse_qs)BeautifulSoupTag)CaseInsensitiveDict)GameOptions)ProxyConnectionErrorLoginRequiredc                    s    fdd}|S )Nc                    s$   | j std | g|R i |S )NzUse login method first)was_login_executedr   )selfargskwargsfunc 8/usr/local/lib/python3.10/dist-packages/steampy/utils.pyfunc_wrapper   s   z$login_required.<locals>.func_wrapperr   )r   r   r   r   r   login_required   s   r   textbeginendreturnc                 C   s*   |  |t| }|  ||}| || S N)indexlen)r   r   r   startr   r   r   text_between   s   r   c                 c   sR    d}	 z|  ||t| }|  ||}| || V  W n
 ty'   Y d S w qNr   )r   r   
ValueError)r   r   r   stopr   r   r   r   texts_between"   s   r"   
account_idc                 C   s8   t | jddd}djddd}ttd|| d S )N   big	byteorderi z>Qr   )intto_bytesstrstructunpack)r#   first_bytes
last_bytesr   r   r   account_id_to_steam_id-   s   r/   steam_idc                 C   s*   t tdt| jddddd  d S )Nz>L   r%   r&   r$   r   )r*   r+   r,   r(   r)   )r0   r   r   r   steam_id_to_account_id3   s   *r2   z0.05	price_netpublisher_fee	steam_feec                 C   sP   | d9 } t tt| | d}t tt| | d}| | | }t|d S )a  Calculate the price including the publisher's fee and the Steam fee.

    Arguments:
        price_net (Decimal): The amount that the seller receives after a market transaction.
        publisher_fee (Decimal): The Publisher Fee is a game specific fee that is determined and collected by the game
            publisher. Most publishers have a `10%` fee - `Decimal('0.10')` with a minimum fee of `$0.01`.
        steam_fee (Decimal): The Steam Transaction Fee is collected by Steam and is used to protect against nominal
            fraud incidents and cover the cost of development of this and future Steam economy features. The fee is
            currently `5%` (with a minimum fee of `$0.01`). This fee may be increased or decreased by Steam in the
            future.
    Returns:
        Decimal: Gross price (including fees) - the amount that the buyer pays during a market transaction
    d      )r(   mathfloormaxr   )r3   r4   r5   steam_fee_amountpublisher_fee_amountprice_grossr   r   r   calculate_gross_price7   s
   r>   r=   c                 C   s   | d9 } t t| || d  }t|d ||d }d}d}|| krR|dk rR|| kr6|r1	 |d S |d8 }nd}|d7 }t|d ||d }|d7 }|| krR|dk s&|d S )a|  Calculate the price without the publisher's fee and the Steam fee.

    Arguments:
        price_gross (Decimal): The amount that the buyer pays during a market transaction.
        publisher_fee (Decimal): The Publisher Fee is a game specific fee that is determined and collected by the game
            publisher. Most publishers have a `10%` fee - `Decimal('0.10')` with a minimum fee of `$0.01`.
        steam_fee (Decimal): The Steam Transaction Fee is collected by Steam and is used to protect against nominal
            fraud incidents and cover the cost of development of this and future Steam economy features. The fee is
            currently `5%` (with a minimum fee of `$0.01`). This fee may be increased or decreased by Steam in the
            future.
    Returns:
        Decimal: Net price (without fees) - the amount that the seller receives after a market transaction.
    r6   r7   r   F
   T)r   r(   r>   )r=   r4   r5   estimated_net_priceestimated_gross_price
iterationsever_undershotr   r   r   calculate_net_priceL   s"   
rD   inventory_responsegamec                 C   s6   |  dg }|s
i S dd | d D }t|||jdS )Nassetsc                 S      i | ]}t ||qS r   get_description_key).0descriptionr   r   r   
<dictcomp>s       z@merge_items_with_descriptions_from_inventory.<locals>.<dictcomp>descriptions)
context_id)getmerge_itemsrP   )rE   rF   	inventoryrO   r   r   r   ,merge_items_with_descriptions_from_inventoryo   s
   rT   offers_responsec                    sz   dd | d  dg D  | d  dg }| d  dg }tt fdd|| d d< tt fd	d|| d d< | S )
Nc                 S   rH   r   rI   )rK   offerr   r   r   rM   x   rN   z=merge_items_with_descriptions_from_offers.<locals>.<dictcomp>responserO   trade_offers_receivedtrade_offers_sentc                    
   t |  S r   (merge_items_with_descriptions_from_offerrV   rO   r   r   <lambda>|      
 z;merge_items_with_descriptions_from_offers.<locals>.<lambda>c                    rZ   r   r[   r]   r^   r   r   r_      r`   )rQ   listmap)rU   received_offerssent_offersr   r^   r   )merge_items_with_descriptions_from_offersw   s   re   rV   rO   c                 C   s8   t | dg |}t | dg |}|| d< || d< | S )Nitems_to_giveitems_to_receive)rR   rQ   )rV   rO   merged_items_to_givemerged_items_to_receiver   r   r   r\      s
   r\   listingsids_to_assets_addressc                 C   sH   |  d D ]\}}|| }||d  |d  |d  }||d< q| S )Nsell_listingsr   r7      rL   )rQ   items)rj   rk   rO   
listing_idlistingasset_addressrL   r   r   r   *merge_items_with_descriptions_from_listing   s
   
rr   rn   c                 K   sl   i }| D ]/}t |}t|| }|dp|d }|dp"|d |d< ||d< |d |d< |||< q|S )Nidassetid	contextidrP   amount)rJ   copyrQ   )rn   rO   r   merged_itemsitemdescription_keyrL   item_idr   r   r   rR      s   
rR   htmlc                 C   s   t | d}|dd dddi}i }i }|D ]/}d|jv r$t|}qd|jv r>t|}| D ]}d	|d
< q1|| qd|jv rGt|}q||dS )Nhtml.parserzdiv[id=myListings]r   divclassmarket_home_listing_tablezMy sell listingsz!My listings awaiting confirmationTneed_confirmationzMy buy orders)
buy_ordersrl   )r   selectfindAllr   get_sell_listings_from_nodevaluesupdateget_buy_orders_from_node)r|   documentnodessell_listings_dictbuy_orders_dictnodesell_listings_awaiting_confrp   r   r   r   get_market_listings_from_html   s    






r   r   c              	   C   s   |  ddtdi}i }|D ]5}|d}|jd dd|d j |d j dd	 | dd
did j dd}|||d < q|S )Nr~   rs   zmylisting_\d+zspan[title]
mylisting_ r   r7   r   market_listing_listed_dateF)ro   	buyer_payyou_receive
created_onr   ro   )r   recompiler   attrsreplacer   strip)r   sell_listings_rawr   listing_rawspansrp   r   r   r   r      s   
r   c                 C   s   t | d}t|}d|iS )Nr}   rl   )r   r   )r|   r   r   r   r   r   !get_market_sell_listings_from_api   s   
r   c              	   C   s   |  ddtdi}i }|D ]C}|dd jd}|jd ddt|d 	 |d	 	 |j
j|d
d jd ddd |dd jd}|||d < q|S )Nr~   rs   zmybuyorder_\d+z span[class=market_listing_price]r   @mybuyorder_r   r7   z"img[class=market_listing_item_img]src/rm   z$span[class=market_listing_game_name])order_idquantityprice	item_nameicon_url	game_namer   )r   r   r   r   r   splitr   r   r(   r   arsplit)r   buy_orders_rawr   orderqnt_price_rawr   r   r   r      s   
r   c                 C   s@   i }d}t || D ]}t|d |d |d g||d < q
|S )NziCreateItemHoverFromContainer\( [\w]+, 'mylisting_([\d]+)_[\w]+', ([\d]+), '([\d]+)', '([\d]+)', [\d]+ \);r7   rm      r   )r   findallr*   )r|   listing_id_to_assets_addressregexmatchr   r   r   *get_listing_id_to_assets_address_from_html   s
   $r   ry   c                 C   s   | d  d| d  S )Nclassid_
instanceidr   )ry   r   r   r   rJ      s   rJ   Turlkeycase_sensitivec                 C   s2   t | j}|rt|| d S tt|| d S r   )r   queryr   r   )r   r   r   paramsr   r   r   get_key_value_from_url   s   
(r   c                  C   sV   t jt jt} t|  dd}dd |D W  d    S 1 s$w   Y  d S )Nz/../secrets/credentials.pwdrc                 S   s0   g | ]}t | d  | d | d qS )r   r7   rm   )Credentialsr   )rK   liner   r   r   
<listcomp>   s   0 z$load_credentials.<locals>.<listcomp>)ospathdirnameabspath__file__open)r   fr   r   r   load_credentials   s   $r   c                   @   s"   e Zd ZdededefddZdS )r   loginpasswordapi_keyc                 C   s   || _ || _|| _d S r   )r   r   r   )r   r   r   r   r   r   r   __init__   s   
zCredentials.__init__N)__name__
__module____qualname__r*   r   r   r   r   r   r      s    r   proxiesc                 C   s,   z
t jd| d W dS  ty   tdw )Nzhttps://steamcommunity.com/r   Tz(Proxy not working for steamcommunity.com)requestsrQ   	Exceptionr
   r   r   r   r   
ping_proxy  s   r   namecookiedomainc                 C   s   | ||dS )N)r   valuer   r   )r   r   r   r   r   r   create_cookie
  s   r   )T)1r   r   rw   r8   r+   typingr   decimalr   urllib.parser   r   r   bs4r   r   requests.structuresr   steampy.modelsr	   steampy.exceptionsr
   r   r   r*   r   r"   r/   r2   r>   rD   dictrT   re   r\   rr   rR   r   r   r   r   r   rJ   boolr   r   r   r   r   r   r   r   r   <module>   sH    
""#
