
    h-E                        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mZ d dl	m
Z
 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mZmZ d d	lmZmZmZ d d
lmZmZm Z m!Z!m"Z" d dl#m$Z$m%Z&  e
              ej'        d          Z' ej(        d          Z) ej(        d          Z* ej(        d          Z+ ede*e+e)          Z,de-de.fdZ%e,/                     ej0        d          ej1        z            defd            Z2e,/                     ej0        d          ej1        z            defd            Z3e,/                     ej0        d          ej1        z            d             Z4e,/                     ej5        d6                    e                              d             Z7e,/                     ej0        d          ej1        z            d             Z8e,/                     ej5        d           ej1        z            d!             Z9e,/                     ej0        d"          ej1        z            d#             Z:e,/                     ej5        d$          ej1        z            d%             Z;e,<                                d&             Z= ej>                    j?        @                    d'          A                    d(           B                    d)           d* ZCd+ ZDeEd,k    r eD             dS dS )-    N)Client)filters)MessageInlineKeyboardMarkupInlineKeyboardButton)load_dotenv)scrape)check_prices)fetch_all_productsadd_new_productfetch_one_product
delete_one)flipkart_url_patternsamazon_url_patternsall_url_patterns)expand_ecommerce_urlis_amazon_short_linkis_flipkart_short_linkis_amazon_urladd_affiliate_tag_to_amazon_url)create_flipkart_affiliate_urlis_flipkart_urlzAsia/Kolkata	BOT_TOKENAPI_IDAPI_HASHPriceTrackerBot)api_idapi_hash	bot_tokenurlreturnc                 D     t           fdt          D                       S )zCheck if URL is a Flipkart URLc              3   B   K   | ]}t          j        |          V  d S Nrematch.0patternr    s     &F:\Savad\Price\PriceTrackerBot\main.py	<genexpr>z"is_flipkart_url.<locals>.<genexpr>"   s/      KK'rx%%KKKKKK    )anyr   )r    s   `r+   r   r       s'    KKKK5JKKKKKKr-   startmessagec                 d   K   d|j         j         d}|                    |d           d {V  d S )NzHello ud  ! 🌟

I'm PriceTrackerBot, your personal assistant for tracking product prices. 💸

To get started, use the /my_trackings command to start tracking a product. Simply send the url:
For example:
I'll keep you updated on any price changes for the products you're tracking. Feel free to ask for help with the /help command at any time. Happy tracking! 🚀Tquote)chatusername
reply_text_r0   texts      r+   r/   r/   %   s]      	]& 	] 	] 	] 	 

T

.
..........r-   helpc                 H   K   d}|                     |d           d {V  d S )Nu  🤖 **Price Tracker Bot Help**

Here are the available commands:
1. `/my_trackings`: View all the products you are currently tracking.
2. `/stop < product_id >`: Stop tracking a specific product. Replace `<product_id>` with the product ID you want to stop tracking.
3. `/product < product_id >`: Get detailed information about a specific product. Replace `<product_id>` with the product ID you want information about.


**How It Works:**

1. Send the product link from Flipkart.
2. The bot will automatically scrape and track the product.
3. If there is a price change, the bot will notify you with the updated information.
Feel free to use the commands and start tracking your favorite products!
Tr2   )r6   r7   s      r+   r:   r:   4   sD      		U 	 

T

.
..........r-   my_trackingsc           	      \  K   	 |j         j        }|                    d           d {V }t          |           d {V }|r'd}g }t	          |d          D ]\  }}|                    d          }	|                    d          }
|                    d          p|                    d          }|                    d	          }|d
| d|
 d| dz  }|d| dz  }|d|	 dz  }|d|	 dz  }|d|	 dz  }|                    t          d| d|	           t          d| d|	           g           t          |          }|	                    |d|           d {V  d S |	                    d           d {V  d S # t          $ r}t          |           Y d }~d S d }~ww xY w)NzFetching Your Products...zYour Tracked Products:

   )r/   
product_idproduct_namedisplay_urlr    priceu   🏷️ **Product z**: [](z)

u   💰 **Current Price**: 
u
   📋 ID: `z`
u   🔍 Details: /product_u   ❌ Stop: /stop_z

u   🔍 Details #details_callback_datau
   ❌ Stop #stop_Tdisable_web_page_previewreply_markupzNo products added yet)r4   idr6   r   	enumerategetappendr   r   edit	Exceptionprint)r8   r0   chat_idr9   productsproducts_messagebuttonsiproduct_idr@   product_urlproduct_pricekeyboardes                  r+   trackr^   E   sq     %,/''(CDDDDDDDD+G44444444 	5; G':::  
7kk,//&{{>::%kk-88NGKK<N<N 'G 4 4 SSSSSSSS  !$P}$P$P$PP  $9$9$9$99  $Ec$E$E$EE  $@s$@$@$@@  ()=!)=)=M]X[M]M]^^^()9a)9)9QTWWW      ,G44H)),tZb)ccccccccccc))344444444444   as   E$F *F 
F+F&&F+|c                 r  K   	 |j                                         }t          |          st          |          rK|                    d           d {V }t          |dd          }|                    d           d {V  |nYt          |          r-|                    d           d {V }t          |d          n||                    d           d {V }t          fd	t          D                       rd
nd}}|dk    rE|                    d           d {V  t                    }|                    d           d {V  t          |           d {V \  }}|r|rt          |j        j        |||           d {V }	t                    rdv rd}
n|dk    r	|k    rd}
nd}
t!          t#          dd|	           t#          dd|	           gg          }|                    d| d| d|                                 |
 d|	 	|           d {V  d S |                    d           d {V  d S # t&          $ rb}t)          d|            	 |                    d           d {V  n"#  |                    d           d {V  Y n	xY wY d }~d S Y d }~d S d }~ww xY w)Nz#Expanding short URL... Please wait!Tzthrissurvarth-21)add_affiliate_tagaffiliate_tagzProcessing expanded URL...z%Processing Amazon URL... Please wait!)rb   z$Adding Your Product... Please Wait!!c              3   B   K   | ]}t          j        |          V  d S r$   r%   r(   s     r+   r,   z%track_flipkart_url.<locals>.<genexpr>   s/      "]"]g28GS#9#9"]"]"]"]"]"]r-   amazonflipkartz'Creating affiliate link... Please wait!zProcessing affiliate link...ztag=thrissurvarth-21u&   
🔗 Affiliate tag added for trackingu*   
🔗 Flipkart link converted for tracking u   🔍 View DetailsrE   rF      ❌ Stop TrackingrH   u   ✅ Successfully tracking: "u   "

💰 Current Price: ₹u   
🛒 Platform: uC   

📋 Track My Products: /my_trackings
🔍 Get details: /product_)rK   uK   ❌ Failed to scrape product information! Please check if the URL is valid.zError in track_flipkart_url: 4   ❌ An error occurred while processing your request.)r9   stripr   r   r6   r   rP   r   r   r.   r   r   r	   r   r4   rL   r   r   titlerQ   rR   )r8   r0   original_urlstatusexpanded_urlplatformrA   r@   rB   rL   affiliate_msgr\   r]   r    s                @r+   track_flipkart_urlrp   o   sJ     D]|))++  -- 	V1G1U1U 	V"--.STTTTTTTTF/PTdvwwwL++:;;;;;;;;;CC<(( 	V"--.UVVVVVVVVF1,N`aaaCC C"--.TUUUUUUUUF  #"]"]"]"]I\"]"]"]]]m88cm z!!++GHHHHHHHHH7<<K++<=========$*3$9$9999999e 	mE 	m&sE;       B
 S!! #&<&C&C IZ''K3,>,> M " ,()<OWYOO\\\()<LTVLLYYY-  H ++3| 3 3*/3 3"*.."2"234A3 3 /1	3 3
 &             ++klllllllllll ] ] ]1a11222	]++TUUUUUUUUUU	]$$%[\\\\\\\\\\\ VUUUUU\\\\\\]s6   H&I
 -I
 

J6J1'JJ1J" J11J6rX   c                   K   	 |j                                         }t          |          dk    r|\  }}nh|j                                         d         }|                    d          r|                    dd          }n|                    d           d {V  d S |                    d           d {V }|rt          |           d {V }|r|                    d          }|                    d          p|                    d	          }	|                    d
          }
|                    d          }|                    d          }d| d|	 d|
 d| d| d| d| }|                    |d           d {V  d S |                    d           d {V  d S |                    d           d {V  d S # t          $ r}t          |           Y d }~d S d }~ww xY w)N   r   	/product_rf   #   ❌ Invalid product command format!Getting Product Info....r@   rA   r    rB   upperlower   🛍 **Product:** [rC      )

💲 **Current Price:**    
📉 **Lowest Price:**    
📈 **Highest Price:**    

📋 **Product ID:** `   `
❌ **Stop tracking:** /stop_TrJ   Product Not FoundFailed to fetch the product)r9   splitlen
startswithreplacer6   r   rN   rP   rQ   rR   )r8   r0   
text_parts__rL   commandrl   rX   r@   rZ   r[   maximum_priceminimum_pricerU   r]   s                  r+   track_productr      s     (\''))
z??aFB l((**1-G!!+.. __["55(()NOOOOOOOOO))*DEEEEEEEE 	=-b11111111G 7&{{>::%kk-88NGKK<N<N 'G 4 4 'G 4 4 'G 4 49, 9 9+ 9 9/<9 9.;9 9 0=9 9 02	9 9
 579 9 ! kk"2TkRRRRRRRRRRRkk"566666666666++;<<<<<<<<<<<   as+   BG  C&G  G  #G   
G$
GG$z^/product_[a-zA-Z0-9]+c                   K   	 |j                                         }|                    d          r8|                    dd          }|                    d           d {V }|rt          |           d {V }|r|                    d          }|                    d          p|                    d          }|                    d          }|                    d          }	|                    d	          }
d
| d| d| d|
 d|	 d| d| }|                    |d           d {V  d S |                    d           d {V  d S |                    d           d {V  d S |                    d           d {V  d S # t          $ r8}t          d|            |                    d           d {V  Y d }~d S d }~ww xY w)Nrs   rf   ru   r@   rA   r    rB   rv   rw   rx   rC   ry   rz   r{   r|   r}   Tr~   r   r   rt   z#Error in track_product_underscore: rh   )
r9   ri   r   r   r6   r   rN   rP   rQ   rR   )r8   r0   r   rL   rl   rX   r@   rZ   r[   r   r   rU   r]   s                r+   track_product_underscorer      s     "Y,$$&&k** 	Lb11B"--.HIIIIIIIIF A 1" 5 5555555 ;#*;;~#>#>L")++m"<"<"RE@R@RK$+KK$8$8M$+KK$8$8M$+KK$8$8M=l = =k = =3@= =2?= = 4A= = 46	= =
 9;= = % !++&6QU+VVVVVVVVVVV ++&9:::::::::::kk"?@@@@@@@@@@@$$%JKKKKKKKKKKK Y Y Y7A77888  !WXXXXXXXXXXXXXXXYs*   D+F 1F F +F 
G
-GG
stopc                   K   	 |j                                         }t          |          dk    r|\  }}nh|j                                         d         }|                    d          r|                    dd          }n|                    d           d {V  d S |                    d           d {V }|j        j        }|rRt          ||           d {V }|r|	                    d           d {V  d S |	                    d           d {V  d S |	                    d           d {V  d S # t          $ r}	t          |	           Y d }	~	d S d }	~	ww xY w)	Nrr   r   /stop_rf       ❌ Invalid stop command format!Deleting Product....z'Product Deleted from Your Tracking ListzFailed to Delete the product)r9   r   r   r   r   r6   r4   rL   r   rP   rQ   rR   )
r8   r0   r   r   rL   r   rl   rS   
is_deletedr]   s
             r+   delete_productr     s     \''))
z??aFB l((**1-G!!(++ __Xr22(()KLLLLLLLLL))*@AAAAAAAA,/ 	>)"g66666666J Bkk"KLLLLLLLLLLLkk"@AAAAAAAAAAA++<===========   as+   BD6 AD6 <D6 D6 6
E EEz^/stop_[a-zA-Z0-9]+c                   K   	 |j                                         }|                    d          r|                    dd          }|                    d           d {V }|j        j        }|rRt          ||           d {V }|r|                    d           d {V  d S |                    d           d {V  d S |                    d           d {V  d S |                    d           d {V  d S # t          $ r8}t          d|            |                    d           d {V  Y d }~d S d }~ww xY w)	Nr   rf   r   u+   ✅ Product Deleted from Your Tracking Listu    ❌ Failed to Delete the productr   z$Error in delete_product_underscore: rh   )r9   ri   r   r   r6   r4   rL   r   rP   rQ   rR   )r8   r0   r   rL   rl   rS   r   r]   s           r+   delete_product_underscorer   -  s     Y,$$&&h'' 	I2..B"--.DEEEEEEEEFloG F#-b'#:#:::::::
 J ++&STTTTTTTTTTT ++&HIIIIIIIIIIIkk"DEEEEEEEEEEE$$%GHHHHHHHHHHH Y Y Y8Q88999  !WXXXXXXXXXXXXXXXYs*   B C= &C= C=  C= =
D?-D::D?c                   K   	 |j         }|j        j        }|                    d          r5|                    dd          }t          |           d {V }|r|                    d          }|                    d          p|                    d          }|                    d          }|                    d          }	|                    d          }
t          t          d	d
|           t          dd|           gg          }d| d| d| d|
 d|	 d| d}|	                    |d|           d {V  d S |
                    dd           d {V  d S |                    d
          rj|                    d
d          }t          ||           d {V }|r|	                    d           d {V  d S |
                    dd           d {V  d S |                    d          r5|                    dd          }|
                    dd           d {V  d S d S # t          $ r:}t          d|            |
                    dd           d {V  Y d }~d S d }~ww xY w)NrE   rf   r@   rA   r    rB   rv   rw   rg   rH   rF   u   🔄 Refresh Pricerefresh_rx   rC   ry   rz   r{   r|   `TrI   u   ❌ Product not found!)
show_alertu,   ✅ Product removed from your tracking list.u   ❌ Failed to remove product!u/   🔄 Refreshing price... (Feature coming soon!)zCallback error: u   ❌ An error occurred!)data	from_userrL   r   r   r   rN   r   r   edit_message_textanswerr   rQ   rR   )r8   callback_queryr   user_idr?   rX   r@   rZ   r[   r   r   r\   rU   r   r]   s                  r+   handle_callbackr   G  s     9O" *-??:&& 1	lj"55J-j99999999G W&{{>::%kk-88NGKK<N<N 'G 4 4 'G 4 4 'G 4 4 0,-@PdXbPdPdeee,-AQh\fQhQhiii1  =, = =+ = =/<= =.;= = 0== = 0:	= = = ! %66$-1!) 7            %++,DQU+VVVVVVVVVVV__W%% 	lgr22J)*g>>>>>>>>J ^$667efffffffffff$++,KX\+]]]]]]]]]]]__Z(( 	lj"55J ''(Yfj'kkkkkkkkkkk	l 	l
  O O O$$$%%%##$<#NNNNNNNNNNNNNNNOs2   D=H- H- "AH- H- !AH- -
I17/I,,I1z00:00c                  N    t          j        t          t                              S r$   )asynciorunr
   app r-   r+   <lambda>r     s    GKS8I8I,J,J r-   	daily_jobc                  T    	 t          j                     t          j        d           ()NT   )schedulerun_pendingtimesleepr   r-   r+   run_scheduler     s(    
1r-   c                      t          j        t                    } |                                  t                              t          d                     d S )N)targetzBot Running)	threadingThreadr   r/   r   r   rR   )schedule_threads    r+   mainr     sG    &l;;;OGGE-  !!!!!r-   __main__)Fr   r   pyrogramr   r   pyrogram.typesr   r   r   dotenvr   osr   r   pytzr&   scraperr	   	schedulerr
   helpersr   r   r   r   regex_patternsr   r   r   	url_utilsr   r   r   r   r   flipkart_affiliater   r   is_flipkart_affiliatetimezonegetenvr   r   r   r   strbool
on_messager   privater/   r:   r^   regexjoinrp   r   r   r   r   on_callback_queryr   everydayatdotagr   r   __name__r   r-   r+   <module>r      s                    N N N N N N N N N N       				    				       " " " " " " V V V V V V V V V V V V W W W W W W W W W W I  I  I  I  I  I  I  I  I  I  I  I  I  I f f f f f f f f 4=(( BIk""		8		29Z  fvIVVVL L L L L L
 ((7?:;;/G / / / <;/ '''/9::/7 / / / ;:/  //'/ABB& & CB&R chh'78899::E] E] ;:E]P 	**W_<==) ) >=)Z 7887?JKK#Y #Y LK#YL '''/9::  ;:> 455GHHY Y IHY2 :O :O :Oz           # #$J$J K K O O  
  " " " zDFFFFF r-   