import time
from full_fred.fred import Fred
import pymysql
import time
fred=Fred('example_key.txt')
fred.set_api_key_file('example_key.txt')
times="00:00:00"
while True:
    import datetime
    # now_time = datetime.datetime.now()
    # next_time = now_time + datetime.timedelta(days=+1)
    # next_year = next_time.date().year
    # next_month = next_time.date().month
    # next_day = next_time.date().day
    # next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + " 15:00:00","%Y-%m-%d %H:%M:%S")
    # timer_start_time = (next_time - now_time).total_seconds()
    data=fred.get_series_df('WEI')
    data_date=data['date']
    data_value=data['value']
    # 获取网站数据
    list1=[]
    list2=[]
    for i in data_date:
        list1+=[i]
    for i2 in data_value:
        list2+=[i2]
    # 拿最新七条数据
    date1=list1[-1]
    date1= date1 + ' ' + times
    timeArray = time.strptime(date1, "%Y-%m-%d %H:%M:%S")
    timestamp = time.mktime(timeArray)
    date1 = int(timestamp * 1000) - 28800000
    value1=list2[-1]

    date2=list1[-2]
    date2 = date2 + ' ' + times
    timeArray = time.strptime(date2, "%Y-%m-%d %H:%M:%S")
    timestamp = time.mktime(timeArray)
    date2 = int(timestamp * 1000) - 28800000
    value2=list2[-2]

    date3=list1[-3]
    date3 = date3 + ' ' + times
    timeArray = time.strptime(date3, "%Y-%m-%d %H:%M:%S")
    timestamp = time.mktime(timeArray)
    date3 = int(timestamp * 1000) - 28800000
    value3=list2[-3]

    date4=list1[-4]
    date4 = date4 + ' ' + times
    timeArray = time.strptime(date4, "%Y-%m-%d %H:%M:%S")
    timestamp = time.mktime(timeArray)
    date4 = int(timestamp * 1000) - 28800000
    value4=list2[-4]

    date5=list1[-5]
    date5 = date5 + ' ' + times
    timeArray = time.strptime(date5, "%Y-%m-%d %H:%M:%S")
    timestamp = time.mktime(timeArray)
    date5 = int(timestamp * 1000) - 28800000
    value5=list2[-5]

    date6=list1[-6]
    date6 = date6 + ' ' + times
    timeArray = time.strptime(date6, "%Y-%m-%d %H:%M:%S")
    timestamp = time.mktime(timeArray)
    date6 = int(timestamp * 1000) - 28800000
    value6=list2[-6]

    date7=list1[-7]
    date7 = date7 + ' ' + times
    timeArray = time.strptime(date7, "%Y-%m-%d %H:%M:%S")
    timestamp = time.mktime(timeArray)
    date7 = int(timestamp * 1000) - 28800000
    value7=list2[-7]

    db = pymysql.connect(host="127.0.0.1",user="root",password="2GS@bPYcgiMyL14A",database="Macroeconomics",port=4423)
    cursor = db.cursor()
    sql = "select times from WEI order by times desc limit 1"
    cursor.execute(sql)
    db.commit()
    old_time = cursor.fetchall()
    old_time = old_time[0][0]
    sql = "update WEI set WEI=%s where times='%s'" % (value2, date2)
    cursor.execute(sql)
    sql = "update WEI set WEI=%s where times='%s'" % (value3, date3)
    cursor.execute(sql)
    sql = "update WEI set WEI=%s where times='%s'" % (value4, date4)
    cursor.execute(sql)
    sql = "update WEI set WEI=%s where times='%s'" % (value5, date5)
    cursor.execute(sql)
    sql = "update WEI set WEI=%s where times='%s'" % (value6, date6)
    cursor.execute(sql)
    sql = "update WEI set WEI=%s where times='%s'" % (value7, date7)
    cursor.execute(sql)
    db.commit()
    if date1 == old_time:
        time.sleep(21600)
        # time.sleep(timer_start_time)
    else:
        db = pymysql.connect(host="127.0.0.1",user="root",password="2GS@bPYcgiMyL14A",database="Macroeconomics",port=4423)
        cursor = db.cursor()
        sql = "insert into WEI(times,WEI)values('%s','%s')" % (date1, value1)
        cursor.execute(sql)
        db.commit()