From 81dba3555b85a21582e263ef5fa72db5ff361e3d Mon Sep 17 00:00:00 2001 From: Bashar Khatib <31943899+basharkhatib@users.noreply.github.com> Date: Fri, 23 Jul 2021 15:31:02 -0700 Subject: [PATCH] Update openexchange_data_pull.py --- openexchange_data_pull.py | 35 ++++------------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/openexchange_data_pull.py b/openexchange_data_pull.py index 5dc8282..9e89b59 100644 --- a/openexchange_data_pull.py +++ b/openexchange_data_pull.py @@ -13,26 +13,13 @@ """ api_key='b0f11c5769c54c23b381f0ad0d19c619' -day_list = "" +current_date = datetime.today().strftime('%Y-%m-%d') -date_ = datetime.today().strftime('%Y-%m-%d') -day_list += date_ -# print(day_list) - -# url = f'https://openexchangerates.org/api/historical/%7Bdate_%7D.json' - -url = f'https://openexchangerates.org/api/historical/%7Bday_list%7D.json' +url = f'https://openexchangerates.org/api/historical/%7Bcurrent_date%7D.json' response = requests.get(f'{url}?app_id={api_key}') +revenue_rates = response.json()["rates"] -exchange_rate = response.json()["rates"] -my_json = json.dumps(exchange_rate) - - -# ////////////////////////////////////////// -# /////////////////DATABASE///////////////// -# ////////////////////////////////////////// load_dotenv() - DB_HOST = os.getenv('DB_HOST') DB_NAME = os.getenv('DB_NAME') DB_USER = os.getenv('DB_USER') @@ -44,23 +31,9 @@ # Execute statements cur = conn.cursor() -# Create table, add to it two coloms, date and revenue_rates_usd -# cur.execute("CREATE TABLE usd_rates (id SERIAL PRIMARY KEY, date DATE , revenue_rates_usd JSONB NOT NULL );") - # Inserting new data to date and revenue_rates_usd -cur.execute("INSERT INTO usd_rates (date, revenue_rates_usd) VALUES(%s, %s)", (day_list, my_json)) - -# Shows everything in usd_rates table -# all_data = pd.read_sql("SELECT * FROM usd_rates;", conn) - -# Read db table, converting from USD to -all_data = pd.read_sql("SELECT date as date, revenue_rates_usd ->> 'SAR' AS SAR FROM usd_rates;", conn) - -print(all_data) +cur.execute("INSERT INTO usd_rates (date, revenue_rates_usd) VALUES(%s, %s)", (current_date, json.dumps(revenue_rates))) conn.commit() - cur.close() -# Closing conn.close() -