
    Wg"                         d Z ddlmZ ddlZddlZddlmZ ddlmZ	 ddl
mZ ddlZddlZddZd	 Zd
 Zedk    r! e              ed            e             dS dS )zv
This script gets daily as well as new data for the stocks in the database "bse_data1" -> OHCL from Zerodha Demat API
    )
getBSEdataN)MongoClient)sleeplocalzerodha\credentials.jsonc                    	 t          |d          5 }t          j        |          }ddd           n# 1 swxY w Y   |                    |           }|r|S t	          d|            # t
          $ r t          d|            Y dS t          j        $ r t          d|            Y dS t          $ r}t          d|            Y d}~dS d}~ww xY w)a?  
    Access credentials from a JSON file.

    Args:
    - env (str): Environment to fetch the credentials from ('local' or 'live'). Default is 'local'.
    - filepath (str): Path to the JSON file containing credentials. Default is 'creds.json'.

    Returns:
    - dict: Credentials for the specified environment.
    rNz&No credentials found for environment: zFile not found: zError decoding JSON in file: zAn error occurred: )	openjsonloadget
ValueErrorFileNotFoundErrorprintJSONDecodeError	Exception)envfilepathfdatacredentialses         0e:\HARSH\Automation\J_Work\zerodha\zBSE_fetch.pyget_credentialsr      s\   )(C   	 A9Q<<D	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  hhsmm 	MKcKKLLL - - -+++,,,,,, : : :8h88999999 ) ) )'A''((((((((()sD   A) 3A) 7A) 7A) A) )C C+	C4CCc                     t                      } |                     d          }|                    d          }|                    d          }|                    d          }t          |          }||         }||         }|                                }|D ]}	d }
t	          |	d                                         d           }d}|D ]G}|	d         |         }d	|vr|
|
|d	<   d
}t          d| d|
            |                    d          }
H|r(|                    d|	d         idd|	d         ii           t          d           d S )NmongoDB_credsconnection_strdatabasebse_collectionpricesc                 B    t           j                             | d          S )N%Y-%m-%d)datetimestrptime)dates    r   <lambda>z'prev_close_updaterBSE.<locals>.<lambda>F   s    XEVE_E_`dfpEqEq     )keyF
prev_closeTzUpdated prev_close for date : close_idz$setz(prev_close fields updated where missing!)r   r   r   findsortedkeysr   
update_one)credsr   connection_urlmongo_databaser   clientdb
collection	documentsdocprevious_closesorted_datesupdate_neededr%   r   s                  r   prev_close_updaterBSEr<   1   s   E IIo..M"&&'788N"&&z22N"&&'788N((F		BN#J !!I  \ \ c(m00228q8qrrr  ! 	/ 	/Dx=&D 4''!-)7D&$(MQQQQQRRR "XXg..NN  	\!!5#e*"53x=@Y7Z[[[	
455555r'   c            
      
   t                      } |                     d          }|                    d          }|                    d          }|                    d          }|                     d          }|                    d          }|                    d          }|                    d          }|                    d          }	|                    d	          }
t          |          }||         }||         }t          j        ||||	|

          }|                                }|                    d           |                                }dddd}t          j        	                                
                    d          }t          j                            |d          }|t          j        d          z
                                  }t          j        ddd          }|D ]/}|                    d|d         i          }|r~	 t          d |d                                         D                       }|t          j        d          z   }n# t"          $ r |}Y nw xY w||k    rt%          d|d          d           n|}t%          d|d          d| d|            |                    d|d          d           |                                }|!|\  }}}}d| d }t)          j        t-          j        d!||i "          j                  }t%          |           	 |rd#|v rd$|d#         v rti } |d#         d$         D ]}!t          j                            |!d                             d%          d         d                                          }"|!d         |!d&         |!d'         |!d(         |!d)         d*}#|#| |"
                    d          <   |r[|d                             |            t          j                                        |d+<   |                    d|d         i|           n\|d         ||t;          |          t;          |          t          j                                        d,| d-}$|                    |$           t%          d.|d                     nt%          d/|d          d0           # t>          $ r&}%t%          d1|d          d2|%            Y d }%~%d }%~%ww xY wt%          d3|d                     1t%          d4           |                                  |                                  d S )5Nr   r   r   r   sqlDB_credsuserhostpasswordport)r?   r@   rA   r   rB   z=SELECT `tradingsymbol` FROM mytable WHERE `exchange` = 'BSE';z*/*zen-US,en;q=0.9zenctoken yjkybfB4nXKkah/J7cGHoHQ/tTTzskUzqUkkca0JGaFYK4kSxBYgrTAHGQztM3RXoJf0QqlSEGr33vtw2ZLzzp12UGSES3LqBx/fIxvlG26SZw1/OjcO6w==)acceptzaccept-languageauthorizationr"      )daysi  symbolr   c              3   z   K   | ]6}t           j                             |d                                           V  7dS )r"   N)r#   r$   r%   ).0r%   s     r   	<genexpr>zbse_fetcher.<locals>.<genexpr>   sZ         D   DX\ 1 : :4 L L Q Q S S   D   D   D   D   D   Dr'   r    z	Data for z is up to date.zProcessing z from z to zbSELECT instrument_token, segment, exchange_token, name FROM `mytable` WHERE `tradingsymbol` LIKE 'z' AND `exchange` LIKE 'BSE';z4https://kite.zerodha.com/oms/instruments/historical/z6/day?user_id=GMG829&oi=0&from=2024-01-01&to=2025-03-11GET)headersr   r   candlesT            )r
   highlowr+   volume	timestampBSE)rG   company_name
Instrumentexchange_tokeninstrument_tokenrV   exchanger    zCompleted processing zNo new data available for .zError processing r*   zNo instrument token found for z.Processing completed. Results saved to MongoDB)!r   r   r   mcconnectcursorexecutefetchallr#   todaystrftimer$   	timedeltar%   find_onemaxr/   r   r   fetchoner   loadsrequestsrequesttextsplitupdatenowreplace_oneint
insert_oner   r+   )&r1   r   r2   r3   r   r>   r?   r@   rA   sql_databaserB   mongo_clientr5   r6   connr`   checkSymbolsrL   rc   	yesterdaydefault_start_daterG   existing_document	last_date	from_datetoken	instTokensegmentrZ   nameurlresponse
new_pricescandlecandle_dateohlcnew_documentr   s&                                         r   bse_fetcherr   `   s   E IIo..M"&&'788N"&&z22N"&&'788N))M**K??6""D??6""Dz**H??:..L??6""D ~..L	n	%BN#J :4dX[_```D[[]]F
NNRSSS??$$L + ^ G ##%%..z::E&&uj99E+333399;;I!tQ22 L@ L@&//6!90EFF 	+/   D   D`qrz`{  aA  aA  aC  aC   D   D   D  D  D	%(:(B(B(BB		 / / /.			/ 9$$<&)<<<=== %
 +IGF1IGGYGGIGGHHH 	  d  }C  DE  }F  d  d  d  	e  	e  	e!!7<4Iw K  K  K  KCz("25#wUW"X"X"X"]^^H(OOO'< #E( 2 2yHVDT7T7T!#J"*6"29"= 	L 	L&.&7&@&@QTAUAUVWAXZd&e&e&j&j&l&l$*1I$*1I#)!9%+AY&,Qi    HL
;#7#7
#C#CDD( <)(3:::FFF9A9J9N9N9P9P)+6"..&)/DFWXXXX '-Qi,0*1.1..A.A03I)1):)>)>)@)@(-&0	( 	( #--l;;;=&)==>>>> CvayCCCDDD < < <:&)::q::;;;;;;;;< >6!9>>???? 

:;;;JJLLLLLs,   A	II+*I+1F S
TS>>T__main__rP   )r   r   )__doc__z_getBSEdatar   r#   r   pymongor   mysql.connector	connectorr^   timer   pandaspdrj   r   r<   r   __name__ r'   r   <module>r      s     $ # # # # #                          ) ) ) )>-6 -6 -6^y y yx zKMMM	E!HHH r'   