import pymysql import json from api_request import ApiRequest from param_options import option_experiment from param_options import option_variable import random if __name__ == '__main__': db = pymysql.connect(host='39.101.194.63', port=23306, user='root', passwd='passok123A', db='jd_data', charset='utf8mb4') cursor = db.cursor() ar = ApiRequest() times = 0 while times < 20: # current_experiment = 'ssp5_8_5' # current_variable = 'eastward_wind' current_experiment = random.choice(option_experiment) current_variable = random.choice(option_variable) times += 1 print("Current combination: Experiment - %s, Variable - %s" % (current_variable, current_experiment)) sql_select = "SELECT id, experiment, variable, model, level " \ "FROM data_detail WHERE valid = 1 AND available IS null " \ "AND experiment = '%s' AND variable = '%s'" % (current_experiment, current_variable) cursor.execute(sql_select) result = cursor.fetchall() total_num = len(result) current_num = 0 for row in result: current_num += 1 if current_num > 10: break row_id = row[0] experiment = row[1] variable = row[2] model = row[3] level = row[4] print('[No %d/%d] %s, %s, %s, %s' % (current_num, total_num, row_id, experiment, variable, model)) param = { 'experiment': experiment, 'variable': variable, 'model': model, 'level': level } reply = ar.get_reply(param) if reply: remarks = json.dumps(reply) location = '' content_length = 0 if reply['state'] == 'completed': location = reply['location'] content_length = reply['content_length'] sent_to_rmq_at = 'null' if 'sent_to_rmq_at' in reply: sent_to_rmq_at = "'" + reply['sent_to_rmq_at'].replace("T", " ").replace("Z", "") + "'" print(reply) sql_update = "UPDATE data_detail " \ "SET available = 1, url = '%s', data_size = %d, update_time = %s, remarks = '%s' " \ "WHERE id = %d" \ % (location, content_length, sent_to_rmq_at, remarks.replace("'", "\\'"), row_id) cursor.execute(sql_update) db.commit() db.close()