import cdsapi
import pymysql as pymysql


class ApiRequest:
    def __init__(self):
        self.client = cdsapi.Client()
        self.max_time_delay = 120

    def get_reply(self, year, month, variable):
        try:
            param = {
                'product_type': 'reanalysis',
                'format': 'netcdf',
                'year': str(year),
                'variable': [
                    variable
                ],
                'month': [
                    month
                ],
                'day': [
                    '01', '02', '03',
                    '04', '05', '06',
                    '07', '08', '09',
                    '10', '11', '12',
                    '13', '14', '15',
                    '16', '17', '18',
                    '19', '20', '21',
                    '22', '23', '24',
                    '25', '26', '27',
                    '28', '29', '30',
                    '31',
                ],
                'area': [
                    60, -180, 0,
                    180,
                ],
                'time': [
                    '00:00', '01:00', '02:00',
                    '03:00', '04:00', '05:00',
                    '06:00', '07:00', '08:00',
                    '09:00', '10:00', '11:00',
                    '12:00', '13:00', '14:00',
                    '15:00', '16:00', '17:00',
                    '18:00', '19:00', '20:00',
                    '21:00', '22:00', '23:00',
                ],
            }

            result = self.client._api("%s/resources/%s" % (self.client.url, 'reanalysis-era5-single-levels'), param,
                                      "POST")
            return result.reply
        except Exception as e:
            print(e)
            return None


if __name__ == "__main__":
    years = range(1980, 2021)
    months = [
        '01', '02', '03',
        '04', '05', '06',
        '07', '08', '09',
        '10', '11', '12',
    ]
    variables = [
        '10m_u_component_of_wind',
        '10m_v_component_of_wind',
        '2m_temperature',
    ]

    ar = ApiRequest()
    db = pymysql.connect(host='47.113.231.200', port=28089,
                         user='root', passwd='passok123A', db='jd_data', charset='utf8mb4')
    cursor = db.cursor()

    for y in years:
        for m in months:
            for v in variables:
                reply = ar.get_reply(year=y, month=m, variable=v)
                print(reply)
                request_id = reply['request_id']
                content_length = reply['content_length']
                download_url = reply['location']
                SQL_UPDATE = f'UPDATE jd_data.era5_detail SET request_id = "{request_id}", ' \
                             f'content_length = "{content_length}", download_url = "{download_url}" ' \
                             f'WHERE data_year = "{y}" AND data_month = "{m}" AND data_variable = "{v}";'
                cursor.execute(SQL_UPDATE)
                db.commit()
                # break
            # break
        break
    cursor.close()
    db.close()
