import time import requests from model.DataBaseUtils import MysqlUtils from model.DateUtils import DateUtils from queue import Queue from app.api_data.platform_order.order_util import save_order class AudioQiyue: def __init__(self): self.db_quchen_text = MysqlUtils().quchen_text self.date_action = DateUtils() self.page_size = 50 # 一次请求的量级 # 限制一分钟120次请求,设置为本地限制70s 120次请求 self.time_sing_size = 120 # 时间队列长度 self.time_sing_ms = 70 # 时间队列,第一对于最后的时间差值. # 暂时没有限制 self.time_sing_ms = 0.1 def control_speed(self, time_queue): if time_queue.full(): time_s = time_queue.get() differ_time_num = time.time() - time_s if differ_time_num < self.time_sing_ms: print('访问速度过快,进行休眠,{}'.format(self.time_sing_ms - differ_time_num)) time.sleep(self.time_sing_ms - differ_time_num) else: time_queue.put(time.time()) else: time_queue.put(time.time()) def get_order(self, start, end, account): order_list = [] # 参数 order_url = "https://a-o-api.qiyuept.com" + "/v1/orders" stage = account[0] token = account[1] time_sign = Queue(self.time_sing_size) for date in self.date_action.getDateLists(start, end): page = 1 while True: timestamp = int(time.time()) self.control_speed(time_queue=time_sign) url = order_url + "?" + "token=" + str(token) + "×tamp=" + str(timestamp) + "&page=" + str( page) + "&size=" + str(self.page_size) + "&date=" + date rsp = requests.get(url=url) response_result_json = rsp.json() # print(response_result_json) code = response_result_json['code'] if code != 0: print(stage, '七悦充值接口异常:', response_result_json) break result_data = response_result_json['data'] total = result_data['total'] if total <= 0: break order_item_list = result_data['data'] for x in order_item_list: create_time = DateUtils.stamp_to_str(x['create_time'], '%Y-%m-%d %H:%M:%S') reg_time = DateUtils.stamp_to_str(x['user_create_time'], '%Y-%m-%d %H:%M:%S') order_list.append(( create_time[:10], stage, '七悦有声', x['wechat_app_name'], # 公众号名称 x['channel_id'], x['user_open_id'], create_time, reg_time, # 用户注册时间 x['money'], x['book_name'], x['transaction_no'] if x['transaction_no'] != '0' else x['trade_no'], # 订单id x['state'] # , x['user_id'] ) ) next_page_url = result_data['next_page_url'] if next_page_url is None: break page += 1 # print(len(order_list)) print(f'{stage} [{start}~{end}] 有订单{order_list.__len__()}') if order_list.__len__() > 0: # print(order_list) save_order(order_list) if __name__ == "__main__": account = ['趣程15期', 'eyJpdiI6Ilc0dmJWTjlHZnpJVVUwM3Q3dlc2aWc9PSIsInZhbHVlIjoiNFFvbXJISzBoTExoa0NJMmtXd0FMUT09IiwibWFjIjoiNTY1YjA3MTVlMzliYzg2MzcxMjZjOTRkYTMyY2FlZmJmNDUyZjYyZGEzM2I4MTMxNDNhMTIwNTIzZWViZjMyMSJ9'] start = '2021-04-01' end = '2021-04-22' # AudioQiyue().get_order(start=start, end=end, account=account) AudioQiyue().run(start=start, end=end)