import time
import requests
import pymysql
from bs4 import BeautifulSoup
from w3lib.html import remove_tags
import datetime
while True:
    try:
        # 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) + " 20:30:01","%Y-%m-%d %H:%M:%S")
        # timer_start_time = (next_time - now_time).total_seconds()
        page = requests.get("https://www.federalreserve.gov/data/intlsumm/current.htm")
        page = page.text
        page = BeautifulSoup(page, 'html.parser')
        page1 = page.find_all('th', class_="colorrev")
        page = page.find_all('td', class_="shadedata1")
        value1=remove_tags(str(page[-1]))
        value1 = value1.replace(",", "")
        value1 = value1.replace(" ", "")
        date1=remove_tags(str(page1[-1]))
        date1 = date1.replace(" ", "")
        date1 = date1.replace("/r", "")
        date1 = date1.replace("/p", "")
        date1= date1[-4:] + date1[0:3]
        date1 = date1.replace("Jan", "/1/01")
        date1 = date1.replace("Feb", "/2/01")
        date1 = date1.replace("Mar", "/3/01")
        date1 = date1.replace("Apr", "/4/01")
        date1 = date1.replace("May", "/5/01")
        date1 = date1.replace("Jun", "/6/01")
        date1 = date1.replace("Jul", "/7/01")
        date1 = date1.replace("Aug", "/8/01")
        date1 = date1.replace("Sep", "/9/01")
        date1 = date1.replace("Oct", "/10/01")
        date1 = date1.replace("Nov", "/11/01")
        date1 = date1.replace("Dec", "/12/01")
        format1 = '%Y/%m/%d'

        value2 = remove_tags(str(page[-2]))
        value2 = value2.replace(",", "")
        value2 = value2.replace(" ", "")

        date2 = remove_tags(str(page1[-2]))
        date2 = date2.replace(" ", "")
        date2 = date2.replace("/r", "")
        date2 = date2.replace("/p", "")
        date2 = date2[-4:] + date2[0:3]
        date2 = date2.replace("Jan", "/1/01")
        date2 = date2.replace("Feb", "/2/01")
        date2 = date2.replace("Mar", "/3/01")
        date2 = date2.replace("Apr", "/4/01")
        date2 = date2.replace("May", "/5/01")
        date2 = date2.replace("Jun", "/6/01")
        date2 = date2.replace("Jul", "/7/01")
        date2 = date2.replace("Aug", "/8/01")
        date2 = date2.replace("Sep", "/9/01")
        date2 = date2.replace("Oct", "/10/01")
        date2 = date2.replace("Nov", "/11/01")
        date2 = date2.replace("Dec", "/12/01")
        format2 = '%Y/%m/%d'
        from datetime import datetime
        date1 = datetime.strptime(date1, format1)
        date2 = datetime.strptime(date2, format2)
        db = pymysql.connect(host="127.0.0.1",user="root",password="2GS@bPYcgiMyL14A",database="Macroeconomics",port=4423)
        cursor = db.cursor()
        sql = "select date from FER order by date desc limit 1"
        cursor.execute(sql)
        db.commit()
        ole_time = cursor.fetchall()
        ole_time = ole_time[0][0]
        date2= "'" + str(date2) + "'"
        sql = "update FER set FER= %s where date=%s" % (value2, date2)
        cursor.execute(sql)
        db.commit()
        if date1 != ole_time:
            sql = "insert into FER(date,FER)values('%s','%s')" % (date1, value1 + '*')
            cursor.execute(sql)
            db.commit()
            db.close()
        # time.sleep(timer_start_time)
        time.sleep(21600)
    except:
        time.sleep(30)
        continue

