
    _h-#                         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         ed
        e        yy)zv
This script gets daily as well as new data for the stocks in the database "nse_data1" -> OHCL from Zerodha Demat API
    )
getNSEdataN)MongoClient)sleepc                 r   	 t        |d      5 }t        j                  |      }ddd       j                  |       }|r|S t	        d|        # 1 sw Y   ,xY w# t
        $ r t        d|        Y yt        j                  $ r t        d|        Y yt        $ r}t        d|        Y d}~y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         //var/www/html/trade_iq/db_updater/zNSE_fetch.pyget_credentialsr      s    )(C  	 A99Q<D	  hhsmEcUKLL	  	   - 
+, :-hZ89 )#A3'(()s?   A AA A AA B64 B6B6B11B6c                  `   t               } | j                  d      }|j                  d      }|j                  d      }|j                  d      }t        |      }||   }||   }|j                  dddii      }|D ]  }	d }
d}t	        |	d   j                         d	 
      }|D ][  }|	d   |   }t        |t              r0d|vr|
|
|d<   d}t        d| d|
        |j                  d      }
Kt        d| d|        ] |s|j                  d|	d   idd|	d   ii        t        d       y )NmongoDB_credsconnection_strdatabasense_collectionpricesz$existsTFc                 B    t         j                   j                  | d      S )N%Y-%m-%d)datetimestrptime)dates    r   <lambda>z'prev_close_updaterNSE.<locals>.<lambda>H   s    XEVEVE_E_`dfpEq     )key
prev_closezUpdated prev_close for date : closezWarning: Data for z is not a dictionary: _idz$setz5prev_close fields updated successfully where missing!)
r   r   r   findsortedkeys
isinstancedictr   
update_one)credsr   connection_urlmongo_databaser   clientdb
collection	documentsdocprevious_closeupdatedsorted_datesr#   r   s                  r   prev_close_updaterNSEr<   1   s~   E IIo.M"&&'78N"&&z2N"&&'78N (F		BN#J It+< =>I  \ c(m0028qr ! 	ODx=&D $%t+%1-;\*"& <TF"^DTUV "&'!2*4&0FtfMN!	O& !!5#e*"53x=@Y7Z[9\< 

ABr%   c            	      t	   t               } | j                  d      }|j                  d      }|j                  d      }|j                  d      }| j                  d      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }	t        |      }
|
|   }||   }t        j                  ||||		      }|j                         }|j                  d
       |j                         }t        j                  j                         }|d   }|j                  dt        |      i      }|d   }t        |       ddd| d}t        j                  j                         j                  d      }t        j                  j                  |d      }|t        j                   d      z
  j                         }t        j                  ddd      }|D ]e  }|j                  d|d   i      }|rU	 t#        d |d   j%                         D              }|t        j                   d      z   }||kD  rt        d|d    d       q|}t        d|d    d| d|        |j                  d |d    d!       |j)                         }||\  }}}d"| d#| d$| }t+        j,                  t/        j0                  d%||i &      j2                        } t        |        	 | r6d'| v r1d(| d'   v r)i }!| d'   d(   D ]p  }"t        j                  j                  |"d   j5                  d)      d   d      j                         }#|"d   |"d*   |"d+   |"d,   |"d-   d.}$|$|!|#j                  d      <   r |rM|d   j7                  |!       t        j                  j9                         |d/<   |j;                  d|d   i|       nK|d   |t=        |      t=        |      t        j                  j9                         |!d0}%|j?                  |%       t        d1|d           nt        d2|d    d3       Ut        d6|d           h t        d7       |
jC                          |jC                          y # t&        $ r |}Y Jw xY w# t@        $ r}&t        d4|d    d5|&        Y d }&~&d }&~&ww xY w)8Nr   r   r   r   sqlDB_credsuserhostpassword)r?   r@   rA   r   z8SELECT `Symbol.1` FROM mytable WHERE `exchange` = 'NSE';zerodha_credentialsr#   enctokenz*/*zen-US,en;q=0.9z	enctoken )acceptzaccept-languageauthorizationr       )daysi  
   symbolr   c              3   x   K   | ]2  }t         j                   j                  |d       j                          4 yw)r    N)r!   r"   r#   ).0r#   s     r   	<genexpr>znse_fetcher.<locals>.<genexpr>   s3        DX\ 1 1 : :4 L Q Q S   Ds   8:r   z	Data for z is up to date.zProcessing z from z to zWSELECT instrument_token, segment, exchange_token FROM `mytable` WHERE `Symbol.1` LIKE 'z' AND `exchange` LIKE 'NSE';z4https://kite.zerodha.com/oms/instruments/historical/z/day?user_id=GMG829&oi=0&from=z&to=GET)headersr   r   candlesT            )r   highlowr)   volume	timestamp)rI   
Instrumentexchange_tokeninstrument_tokenrX   r   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!   r#   todayfind_onestrr   strftimer"   	timedeltamaxr-   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_databasemongo_clientr5   r6   connr_   checkSymbolsrb   creds_collection	get_credsAUTHrN   	yesterdaydefault_start_daterI   existing_document	last_date	from_datetoken	instTokensegmentrZ   urlresponse
new_pricescandlecandle_dateohlcnew_documentr   s'                                          r   nse_fetcherr   c   s   E IIo.M"&&'78N"&&z2N"&&'78N))M*K??6"D??6"Dz*H??:.L ~.L	n	%BN#J ::4dXUD[[]F NNMN??$LMM!E/0 ))VSZ+ACIZ D	$K +$TF+G ##%..z:E&&uj9E++3399;I!tQ3 G@&//6!90EF/   D`qrz`{  aA  aA  aC   D  D	%(:(:(BB	 9$	&)O<= +IF1I;fYKtI;GH 	pqwxyqzp{  |X  Y  	Z!16.IwHSqr{q|  }A  BK  AL  MCzz("2"25#wUW"X"]"]^H(O#<( 2yHVDT7T!#J"*6"29"= 	L&.&7&7&@&@QTAUVWAXZd&e&j&j&l$*1I$*1I#)!9%+AY&,Qi  HL
;#7#7
#CD	L ))(3:::F9A9J9J9N9N9P)+6"..&)/DFWX '-Qi*1.1..A03I)1):):)>)>)@&0( #--l;1&)=>6vaykCD
 26!9+>?OG@R 

:;JJLE  /.	/t  <)&)Bqc:;;<s+   <Q=0ER=RR	R7R22R7__main__rR   )livez2/var/www/html/trade_iq/db_updater/credentials.json)__doc__z_getNSEdatar   r!   r	   pymongor   mysql.connector	connectorr]   timer   pandaspdrj   r   r<   r   __name__ r%   r   <module>r      sh    $        )>0Cdzx zM	!H	!HL r%   