
    |h                     R    d dl Z d dlZd dlmZmZmZ d ZddZd Zd Z	d Z
dd
ZdS )    N)urlparseparse_qs	urlencodec           	          	 t          j        | dd          }|j        }t          j        d|  d|            |S # t
          $ r3}t          j        d|  dt          |                      | cY d}~S d}~ww xY w)	z=
    Expand short URLs (like amzn.to) to their full URLs
    T
   )allow_redirectstimeoutzExpanded URL: z -> zError expanding URL : N)requestsgeturllogginginfo	Exceptionerrorstr)	short_urlresponse	final_urles       +F:\Savad\Price\PriceTrackerBot\url_utils.pyexpand_short_urlr      s    	<	4LLLL	@i@@Y@@AAA   BYBB#a&&BBCCCs   9< 
A9(A4.A94A9anrdcomm-21c           	      h   	 t          |           }d|j        v rt          |j                  }|                    dg           }|r&|d         |k    rt          j        d| d           | S d|v r)|d         |k    rt          j        d|d                     |g|d<   t          |d          }|                    |	          	                                }t          j        d
| d           |S | S # t          $ r3}t          j        d|  dt          |                      | cY d}~S d}~ww xY w)z5
    Add or replace affiliate tag in Amazon URLs
    amazontagr   zAffiliate tag 'z#' already present, no change neededzRemoving existing tag: T)doseq)queryzAdded affiliate tag 'z' to Amazon URLz"Error adding affiliate tag to URL r
   N)r   netlocr   r   r   r   r   r   _replacegeturlr   r   r   )r   affiliate_tag
parsed_urlquery_paramsexisting_tag	new_queryaffiliate_urlr   s           r   add_affiliate_tag_to_amazon_urlr(      s{   c]]
 z(((#J$455L (++E266L Q= @ @a}aaabbb
 $$aM)I)IL|E7JLLMMM $1/L ",d;;;I&//i/@@GGIIMLOOOOPPP   
   J3JJ#a&&JJKKK





s+   A)C4 ,BC4 2C4 4
D1>(D,&D1,D1c                 2    t          |           }d|j        v S )z<
    Check if the URL is an Amazon short link (amzn.to)
    zamzn.tor   r   r   r#   s     r   is_amazon_short_linkr,   9        #J
)))    c                 2    t          |           }d|j        v S )z=
    Check if the URL is a Flipkart short link (fkrt.it)
    zfkrt.itr*   r+   s     r   is_flipkart_short_linkr0   @   r-   r.   c                 2    t          |           }d|j        v S )z+
    Check if the URL is an Amazon URL
    r   r*   r+   s     r   is_amazon_urlr2   G   s     #Jz(((r.   Tc                     | }t          |           st          |           rt          |           }|rt          |          rt	          ||          }|S )zH
    Expand e-commerce short URLs and optionally add affiliate tags
    )r,   r0   r   r2   r(   )r   add_affiliate_tagr"   expanded_urls       r   expand_ecommerce_urlr6   N   sh     L C   -$:3$?$? -',,  T]<88 T6|]SSr.   )r   )Tr   )r   r   urllib.parser   r   r   r   r(   r,   r0   r2   r6    r.   r   <module>r9      s      6 6 6 6 6 6 6 6 6 6  # # # #J* * ** * *) ) )     r.   