import os

import pymysql

download_paths = [
    'F:/cmip6',
    'G:/cmip6',
    'H:/cmip6',
]

if __name__ == '__main__':
    db = pymysql.connect(host='39.101.194.63', port=23306,
                         user='root', passwd='passok123A', db='nfm', charset='utf8mb4')
    cursor = db.cursor()
    count = 0
    SQL_SELECT = "SELECT name FROM jd_data.cmip6_xml_links WHERE downloaded = 0"
    cursor.execute(SQL_SELECT)
    not_downloaded_files = [tp[0] for tp in cursor.fetchall()]
    print("共 {} 个 未下载文件".format(len(not_downloaded_files)))
    for d_path in download_paths:
        if os.path.exists(d_path):
            for file_name in os.listdir(d_path):
                if file_name.endswith('.nc') and file_name in not_downloaded_files:
                    count += 1
                    SQL_UPDATE = "UPDATE jd_data.cmip6_xml_links SET downloaded = 1 WHERE name = '{}' ".format(
                        file_name)
                    print("[No. {}] 文件 {} 已下载".format(count, file_name))
                    cursor.execute(SQL_UPDATE)
                    if count % 50 == 0:
                        db.commit()
    db.commit()
    db.close()
