def wait_element_loaded(driver, delay, element_id): try: elem_founded = WebDriverWait(driver, delay).until( EC.presence_of_element_located((By.ID, element_id))) return True except TimeoutException: LOGGER.warn("等待页面加载元素%s在%s秒内超时", element_id, delay) return False
def save_all_data_to_cache(order_download_date, res): """ 保存所有信息到缓存 :return: """ if not is_valid_data(res): LOGGER.warn("跳过刷新商户数据缓存,无效的缓存。tip=%s", res.get('tip')) return None try: cache_file_full_path = get_cache_full_path(order_download_date) with open(cache_file_full_path, 'wb') as cache_file: pickle.dump(res, cache_file) LOGGER.info("刷新商户数据缓存成功,tip=%s", res.get('tip')) except Exception as ex: LOGGER.error("刷新商户数据缓存异常,%s", ex)
def download_all_orders(order_download_date=None, enable_cache=False): """ 下载所有的商户,过滤session无效的 :param order_download_date: :return: res = {'status': False, 'summary': summary, 'orders': orders, 'errors': errors, 'warnings': warnings, 'tip': ''} """ if not order_download_date: order_download_date = get_now_date_str() if enable_cache: return get_all_data_from_cache(order_download_date) jobs = list() jobs_dict = dict() for key in MERCHANTS_DATA: merchant = MERCHANTS_DATA[key] if isinstance(merchant, MerchantInfo): logon_id = merchant.logon_id greenlet = gevent.spawn(_download_order_by_logon_id_helper, logon_id, order_download_date) jobs.append(greenlet) jobs_dict[logon_id] = greenlet gevent.joinall(jobs) # 获取每个任务的结果输出 warnings = list() errors = list() # 用于前台返回 summary = list() orders = list() # 用于生成excel summary_dict = dict() orders_dict = dict() res = { 'status': True, 'summary': summary, 'orders': orders, 'errors': errors, 'warnings': warnings, 'tip': '' } # 按照配置文件的顺序组装最终返回数据 logon_ids = get_merchants_logon_ids() valid_logon_ids = list() for logon_id in logon_ids: job = jobs_dict.get(logon_id) merchant = get_merchant(logon_id) if isinstance(job, gevent.greenlet.Greenlet): job_error, job_summary, job_orders = job.get() else: LOGGER.warn("%s对应的返回数据为None,跳过,请刷新页面,加载配置数据", logon_id) continue if is_no_data(job_error): warnings.append("商户[%s]%s无数据,跳过" % (merchant.alias, order_download_date)) elif job_error: errors.append(job_error) # 有一个状态失败,则都失败 res['status'] = False else: summary.append(job_summary.to_dict()) for job_order in job_orders: orders.append(job_order.to_dict()) summary_dict[logon_id] = job_summary orders_dict[logon_id] = job_orders valid_logon_ids.append(logon_id) try: file_path = batch_save_order_data_to_excel(order_download_date, valid_logon_ids, summary_dict, orders_dict) res['tip'] = "成功处理了%s个商户,%s条订单,文件路径:%s" % (len(valid_logon_ids), len(orders), file_path) except Exception as ex: LOGGER.exception("批量保存邮件异常:%s", ex) errors.append("保存excel异常%s" % ex.message) save_all_data_to_cache(order_download_date, res) return res