import time import requests from model.DataBaseUtils import MysqlUtils from model.DateUtils import DateUtils from data_processing.sql_tools import save_order from queue import Queue 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 # 时间队列,第一对于最后的时间差值. 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://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() 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: if int(x['state']) != 2: continue y = ((int(x['create_time']) + 8 * 3600) // 86400 * 86400 - 8 * 3600, stage, '七悦有声', x['wechat_app_name'], # 公众号名称 x['channel_id'], x['user_open_id'], time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['create_time'])), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['user_create_time'])), # 用户注册时间 x['money'], x['book_name'], x['id'] # 订单id ) order_list.append(y) 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: save_order(db_operation=self.db_quchen_text, order_list=order_list) # print(order_list) return 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)