o
    Lei/                  
   @   sn  d dl mZ ddlmZmZmZmZ ddlm	Z	m
Z
mZmZmZmZmZ ddlmZmZ ddlmZmZ ddlmZmZmZmZmZ ddlmZmZmZ dd	l m!Z! d d
l"Z"d d
l#Z#ee$Z%e#j&'e#j&(e)dZ*ze+e*dZ,e"-e,Z.W d
   n1 sw   Y  W n e/y Z0 ze%1de* de0  g Z.W Y d
Z0[0nd
Z0[0ww dd Z2dd Z3dd Z4dd Z5d
S )    )	getLogger   )calculate_all_in_costcalculate_profit_and_margincalculate_min_listing_priceload_settings)get_1yr_avg_sale_priceget_percent_discount	get_trendanalyze_sales_rank_trendsget_offer_count_trendget_offer_count_trend_180get_offer_count_trend_365)classify_seasonalityget_sells_period)get_used_product_infoCONDITION_CODE_MAP)analyze_sales_performancerecent_inferred_sale_priceinfer_sale_eventscalculate_seller_quality_scoreget_expected_trough_price)sales_rank_drops_last_30_dayssales_rank_drops_last_180_daysamazon_current)FUNCTION_LISTNzheaders.jsonrzFailed to load headers from : c                 C   sd   t | ttfrt| S t | tr|  dv rdS zt|  ddddW S  ty1   Y dS w )N-N/A         $r!   ,
isinstanceintfloatstrstripreplace
ValueError	value_str r/   1/var/www/agentarbitrage/keepa_deals/processing.py_parse_price   s    r1   c                 C   s\   t | ttfrt| S t | tr|  dv rdS zt|  ddW S  ty-   Y dS w )Nr   r"   %r!   r%   r-   r/   r/   r0   _parse_percent"   s   r3   c           4      C   s   |  d}|s	d S d|i}|  dr| d |d< |  dr$|  d|d< zt| }|r1|d d u r=td| d	 W d S |\}}}}	|d
 |d< ||d< ||d< t |	d|d< |r|r||v r|| }
|
 d|
 d||d< |
 d|d< |
 d|d< |
 d}|d u r|
 dd}t|tr|r|d }|
 d}|d u r|
 dd}t|tr|r|d }|d ur|d urt|ttfrt|ttfrt|d
 | }t	|||d< nd|d< nd|d< d|d< d|d< W n! t
y } ztjd| d| dd W Y d }~d S d }~ww zit}ttD ]`\}}|rsz*|| }t|tr4|r1tt| nd }n|}|t|k rC|||| < W q t
yr } z td| d|t|k r_|| nd d |  W Y d }~qd }~ww qW n  t
y } ztjd| d!| dd W Y d }~nd }~ww | d"}|r|d#ks|dkrtd| d$ d S t }t| \}}zt| |}|d u rtd| d% W d S || d&|v r|d& dkrd'|d& d( d)|d*< nd |d*< t| d"d+}| dd}|  d,pi }| d-}|d u s|dk rd.}|d
 }|  d/}|d u s0|dk r2d0}d1}d2| v ro| d2 ro| d2 D ],} |  d3g }!t|!d4krm|!d |!d5 dkr_|!d5 nd |krm|!d5 dk} nqBt||||||}"t||"}#t|"|}$||"|#d6 |#d7 |$d8 W n  t
y } ztjd| d9| dd W Y d }~nd }~ww zOt| }%|%r||% | d:d u rtd| d; W d S t| }&|&r||& t| d:| d}'|'r||' |t |  |t!|  W n  t
y! } ztjd| d<| dd W Y d }~nd }~ww z<| dd=}(d>"d?d@ |  dAg D })| dd=}*| dBd#}+| dCd#},t#|(|)|*|+|,|dD}-|-|dE< t$|-|dF< W n  t
y~ } ztjd| dG| dd W Y d }~nd }~ww z_t%| }.|.r|dH|. dId#i t&| }/|/r||/ t'| }0|0r||0 t(| }1|1r||1 t)| }2|2r|2 dJnd#}3|3r|3d#kr|3dkrdK|dL< W |S d=|dL< W |S  t
y } ztjd| dM| dd W Y d }~|S d }~ww )NNasinASINtitleTitlemanufacturerManufacturerr   zASIN z*: No used offer found. Halting processing.g      Y@z	Price NowSellerFBAr    	Condition
sellerNamenameratingzSeller RatingratingCountzSeller Review CountcurrentRatingcurrentRatingCountSeller_Quality_Scorer"   z(: Failed to get live price/seller info: T)exc_infoz: Error extracting Unknownr   z%: Error in generic field extraction: zList atr   z]: Excluding deal because 'List at' is missing (Price validation failed or insufficient data).z0: Could not analyze sales performance. Skipping.expected_trough_price_centsr#   d   z.2fzExpected Trough Price0fbaFeespickAndPackFeei&  referralFeePercentageg      .@FoffersofferCSV   r   profitmargin)zAll-in CostProfitMarginzMin. Listing Pricez : Failed business calculations: z	1yr. Avg.zJ: Excluding deal because '1yr. Avg.' is missing (insufficient sales data).z!: Failed analytics calculations: r!   z, c                 S   s   g | ]}|d  qS )r>   r/   ).0catr/   r/   r0   
<listcomp>   s    z(_process_single_deal.<locals>.<listcomp>categoryTreezPeak Sales MonthzTrough Sales Month)xai_api_keyDetailed_SeasonalitySellsz%: Failed seasonality classification: DropszSales Rank - Drops last 30 dayszAmazon - Currentu   ⚠️AMZz!: Failed new columns extraction: )*getr   loggerinfor   r&   listr'   r(   r   	ExceptionerrorHEADERS	enumerater   dictnextitervalueslenwarningbusiness_load_settingsr   r   updater1   r   r   r   r   r
   r	   r   r   joinr   r   r   r   r   r   r   )4product_dataseller_data_cacherX   r4   row_dataused_product_info	price_now	seller_idis_fbacondition_codeseller_detailsrating_percentrating_countpositive_ratingseheadersifuncresvalval_listbusiness_settingssale_events_
sales_perflist_at_price	now_pricefba_fees_objpick_and_packfba_feereferral_percentshipping_included_flagoffer	offer_csvall_in_costprofit_marginmin_listingyr_avg_info
trend_infodiscount_infor6   
categoriesr8   peak_season_strtrough_season_strdetailed_season
drops_dataoffers_dataoffers_data_180offers_data_365amz_dataamz_valr/   r/   r0   _process_single_deal(   s\  




,
@&





6

&


&&




$r   c              
      s  |   D ]}\ }|du st|tsq| dddd}d v s&d v r>zt||  < W q ttfy=   d|  < Y qw d|v r^zt|dd|  < W q ttfy]   d|  < Y qw t	 fdd	d
D rzt||  < W q ttfy   d|  < Y qw q| S )zV
    Cleans and converts numeric string values in the row data to actual numbers.
    Nr#   r!   r$   RankCountr2   c                 3   s    | ]}| v V  qd S )Nr/   )rT   kkeyr/   r0   	<genexpr>  s    z'clean_numeric_values.<locals>.<genexpr>)PriceCostFeerR   rS   )
itemsr&   r)   r*   r+   r'   r,   	TypeErrorr(   any)rp   valuecleaned_valuer/   r   r0   clean_numeric_values   s   r   )6loggingr   business_calculationsr   r   r   r   rk   new_analyticsr   r	   r
   r   r   r   r   seasonality_classifierr   r   seller_infor   r   stable_calculationsr   r   r   r   r   stable_productsr   r   r   field_mappingsr   jsonos__name__r^   pathrm   dirname__file__HEADERS_PATHopenfloadrc   ra   rz   rb   r1   r3   r   r   r/   r/   r/   r0   <module>   s6    $ W