
    i:                     `    S SK r S SKrS SKJr  S SKJr  \" 5         SrS r\S:X  a  \" 5         gg)    N)load_dotenv)configz.https://api.frankfurter.dev/v1/latest?base=USDc            
      L    [        S[         S35        [        R                  " [        SS9n U R	                  5         U R                  5       nUR                  S0 5      nU(       d  [        S5        g SUS'   UR                  S	5      nU(       a  US
-  US'   [        SUS    SU S35        O[        S5        [        S5        S[        R                   S[        R                   S[        R                   S[        R                   S[        R                   3
n[        R                  " U5       nUR                  5        nUR                  S5        SnUR!                  5        VV	s/ s H  u  pX4PM
     n
nn	UR#                  Xz5        S S S 5        UR%                  5         S S S 5        [        S['        U5       S35        g s  sn	nf ! , (       d  f       NE= f! , (       d  f       N>= f! [        R(                  R*                   a  n[        SU 35         S nAg S nAff = f)NzFetching rates from z...
   )timeoutratesz<Failed to fetch rates: 'rates' key is missing from response.g      ?USDINRg?NPRzCalculated NPR rate: z (from INR z * 1.6)z2Warning: INR rate not found, cannot calculate NPR.z)Connecting to PostgreSQL to save rates...zpostgresql://:@/a  
                CREATE TABLE IF NOT EXISTS exchange_rates (
                    currency VARCHAR(3) PRIMARY KEY,
                    rate FLOAT NOT NULL,
                    updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
                )
                a   
                INSERT INTO exchange_rates (currency, rate, updated_at)
                VALUES (%s, %s, CURRENT_TIMESTAMP)
                ON CONFLICT (currency) DO UPDATE SET
                    rate = EXCLUDED.rate,
                    updated_at = EXCLUDED.updated_at
                zSuccessfully upserted z  exchange rates into PostgreSQL.zFetch failed: )printURLrequestsgetraise_for_statusjsonr   PG_USERPG_PASSWORDPG_HOSTPG_PORTPG_DATABASEpsycopgconnectcursorexecuteitemsexecutemanycommitlen
exceptions	HTTPError)responsedatar   inr_rateconn_stringconncursqlcurrraterowses               ?/home/azureuser/projects/bhasika-chatbot/scripts/fetch_rates.pyupdate_ratesr0      s   3$$SE-.<<R0!!#}}"%PQe99U##c>E%L)%,{8*GTUFG9: FNN+1V-?-?,@q 0&2D2D1EG 	 __[)T#   8={{}E}}E*% & KKM) *, 	&s5zl2RST	 F#  *)0 (( $qc"##$sa   A0G- 3B>G- 1G'G)G
8GG$ G- G
G	G
G*&G- -H#HH#__main__)	r   r   dotenvr   appr   r   r0   __name__     r/   <module>r7      s7         74$n zN r6   