o
    jEi                     @   s~   d dl Z d dlmZ d dlmZ e je jd e eZ	ddddd	d
dddddddZ
defddZdefddZdd ZdS )    N)fetch_seller_data)TokenManager)levelzNew, unopenedzNew, open boxzUsed - Like NewzUsed - Very GoodzUsed - GoodzUsed - AcceptablezUsed - RefurbishedzCollectible - Like NewzCollectible - Very GoodzCollectible - GoodzCollectible - Acceptablez
New, other)r                           	   
      token_managerc                 C   s   t | \}}}}|std| d d i S td| d d| d d}|| t||g\}}	}	}
||
 |rH|drH|d S td| d d| d	 i S )
z
    Finds the lowest-priced used offer for a single product, fetches data
    for only that seller, and returns a cache containing their info.
    This is a major optimization to reduce token consumption.
    zASIN asinz4: Could not determine the lowest-priced used seller.z: Found lowest-priced seller: z. Fetching their data.r   sellersz": Failed to fetch data for seller .)get_used_product_infologgerwarninggetinforequest_permission_for_callr   update_after_call)productapi_keyr   	price_now	seller_idis_fbacondition_codeestimated_costseller_data_tokens_left r%   2/var/www/agentarbitrage/keepa_deals/seller_info.pyget_seller_info_for_single_deal   s   

r'   c           
      C   s   t d t }| D ]}d|v r$|d dur$|d D ]	}|r#|| qq
|s.t d i S t|}t dt| d t|}||dt| d t|}|	|}	|
|	d	|  |	rl|	d
rl|	d
 S t d i S )z
    DEPRECATED METHOD.
    Collects all unique seller IDs from a list of products, fetches their data
    in a single batch call, and returns a dictionary mapping seller IDs to their info.
    z<Using deprecated 'get_all_seller_info'. This is inefficient.	sellerIdsNz(No seller IDs found in the product list.zFound z% unique sellers. Fetching their data.z	Fetching z sellers
tokensLeftr   z+Failed to fetch seller data from Keepa API.)r   r   setupdater   listlenr   KeepaAPIget_seller_infor   r   get_tokens_left)
product_listr   r   unique_seller_idsr   seller_id_listseller_ids_listr!   keepar"   r%   r%   r&   get_all_seller_info6   s,   




r6   c                 C   sl  |  d}|s	dS td}d}h d}|D ]x}zO| d}t|tr'| dn|}||v re| dg }t|d	k rItd
|  d d|  W q|d }	|d dkrW|d nd}
|	|
 }||k re|}|}W q ttt	t
fy } ztd|  d d| d|  W Y d}~qd}~ww |r|}| d}| dd}| d}t|tr| dn|}||||fS dS )a   
    Extracts information about the lowest-priced used offer from a product's data
    by correctly parsing the `offerCSV` field from live offer data.

    Returns a tuple: (price_in_cents, seller_id, is_fba, condition_code)
    or (None, None, None, None) if no used offer is found.
    offers)NNNNinfN>   r   r   r   r	   	conditionvalueofferCSVr   zMalformed offerCSV for ASIN r   z: r   zMalformed offer found for ASIN z	. Error: sellerIdisFBAF)r   float
isinstancedictr-   r   r   AttributeErrorKeyError	TypeError
IndexErrorerror)r   r7   	min_price
best_offerused_condition_codesoffercondition_valr    	offer_csvpriceshipping_costtotal_priceer   r   r   r%   r%   r&   r   ]   sF   

"

r   )loggingkeepa_deals.keepa_apir   keepa_deals.token_managerr   basicConfigINFO	getLogger__name__r   CONDITION_CODE_MAPr'   r6   r   r%   r%   r%   r&   <module>   s*    
'