import time
from full_fred.fred import Fred
import pymysql
from datetime import datetime
fred=Fred('example_key.txt')
fred.set_api_key_file('example_key.txt')
while True:
    BUSLOANS=fred.get_series_df('BUSLOANS')
    HBPIGDQ188S=fred.get_series_df('HBPIGDQ188S')
    date1_all = BUSLOANS['date']
    value1_all = BUSLOANS['value']
    date2_all = HBPIGDQ188S['date']
    value2_all = HBPIGDQ188S['value']

    list_date1 = []
    list_value1 = []
    list_date2 = []
    list_value2 = []

    for i in date1_all:
        list_date1 += [i]
    for i in value1_all:
        list_value1 += [i]
    for i in date2_all:
        list_date2 += [i]
    for i in value2_all:
        list_value2 += [i]

    date1 = list_date1[-2]
    value1 = list_value1[-2]
    date2 = list_date1[-3]
    value2 = list_value1[-3]
    date3 = list_date1[-4]
    value3 = list_value1[-4]
    date4 = list_date1[-5]
    value4 = list_value1[-5]

    list_date1 = list_date1[-1]
    list_value1 = list_value1[-1]
    list_date2 = list_date2[-1]
    list_value2 = list_value2[-1]

    list_date1 = list_date1.replace('-', '/')
    date_string = list_date1
    format = '%Y/%m/%d'
    list_date1 = datetime.strptime(date_string, format)
    list_date2 = list_date2.replace('-', '/')
    date_string2 = list_date2
    format = '%Y/%m/%d'
    list_date2 = datetime.strptime(date_string2, format)
    db = pymysql.connect(host="127.0.0.1",user="root",password="2GS@bPYcgiMyL14A",database="Macroeconomics",port=4423)
    cursor = db.cursor()
    sql = "select date from Loan order by date desc limit 1"
    cursor.execute(sql)
    db.commit()
    old_time = cursor.fetchall()
    old_time = old_time[0][0]
    date1 = date1.replace('-', '/')
    date_string = date1
    format = '%Y/%m/%d'
    date1 = datetime.strptime(date_string, format)

    date2 = date2.replace('-', '/')
    date_string = date2
    format = '%Y/%m/%d'
    date2 = datetime.strptime(date_string, format)

    date3 = date3.replace('-', '/')
    date_string = date3
    format = '%Y/%m/%d'
    date3 = datetime.strptime(date_string, format)

    date4 = date4.replace('-', '/')
    date_string = date4
    format = '%Y/%m/%d'
    date4 = datetime.strptime(date_string, format)
    sql = "update Loan set PSI=%s where date='%s'" % (value1, date1)
    cursor.execute(sql)
    sql = "update Loan set PSI=%s where date='%s'" % (value2, date2)
    cursor.execute(sql)
    sql = "update Loan set PSI=%s where date='%s'" % (value3, date3)
    cursor.execute(sql)
    sql = "update Loan set PSI=%s where date='%s'" % (value4, date4)
    cursor.execute(sql)
    db.commit()
    if list_date1 == old_time:
        db = pymysql.connect(host="127.0.0.1",user="root",password="2GS@bPYcgiMyL14A",database="Macroeconomics",port=4423)
        cursor = db.cursor()
        sql = "update Loan set PSI= %s where date='%s'" % (list_value1, list_date1)
        cursor.execute(sql)
        sql1 = "update Loan set FDHBPI_GDP=%s where date='%s'" % (list_value2, list_date2)
        cursor.execute(sql1)
        db.commit()
        time.sleep(21600)
    else:
        db = pymysql.connect(host="127.0.0.1",user="root",password="2GS@bPYcgiMyL14A",database="Macroeconomics",port=4423)
        cursor = db.cursor()
        sql = "insert into Loan(date,PSI)values('%s','%s')" % (list_date1, list_value1)
        cursor.execute(sql)
        sql1 = "update Loan set FDHBPI_GDP=%s where date='%s'" % (list_value2, list_date2)
        cursor.execute(sql1)
        db.commit()