audio_qiyue.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import time
  2. import requests
  3. from model.DataBaseUtils import MysqlUtils
  4. from model.DateUtils import DateUtils
  5. from data_processing.sql_tools import save_order
  6. from queue import Queue
  7. class AudioQiyue:
  8. def __init__(self):
  9. self.db_quchen_text = MysqlUtils().quchen_text
  10. self.date_action = DateUtils()
  11. self.page_size = 50 # 一次请求的量级
  12. # 限制一分钟120次请求,设置为本地限制70s 120次请求
  13. self.time_sing_size = 120 # 时间队列长度
  14. self.time_sing_ms = 70 # 时间队列,第一对于最后的时间差值.
  15. def control_speed(self, time_queue):
  16. if time_queue.full():
  17. time_s = time_queue.get()
  18. differ_time_num = time.time() - time_s
  19. if differ_time_num < self.time_sing_ms:
  20. print('访问速度过快,进行休眠,{}'.format(self.time_sing_ms - differ_time_num))
  21. time.sleep(self.time_sing_ms - differ_time_num)
  22. else:
  23. time_queue.put(time.time())
  24. else:
  25. time_queue.put(time.time())
  26. def get_order(self, start, end, account):
  27. order_list = []
  28. # 参数
  29. order_url = "https://o-api.qiyuept.com" + "/v1/orders"
  30. stage = account[0]
  31. token = account[1]
  32. time_sign = Queue(self.time_sing_size)
  33. for date in self.date_action.getDateLists(start, end):
  34. page = 1
  35. while True:
  36. timestamp = int(time.time())
  37. self.control_speed(time_queue=time_sign)
  38. url = order_url + "?" + "token=" + str(token) + "&timestamp=" + str(timestamp) + "&page=" + str(
  39. page) + "&size=" + str(self.page_size) + "&date=" + date
  40. rsp = requests.get(url=url)
  41. response_result_json = rsp.json()
  42. # print(response_result_json)
  43. code = response_result_json['code']
  44. if code != 0:
  45. print(stage, '七悦充值接口异常:', response_result_json)
  46. break
  47. result_data = response_result_json['data']
  48. total = result_data['total']
  49. if total <= 0:
  50. break
  51. order_item_list = result_data['data']
  52. for x in order_item_list:
  53. if int(x['state']) != 2:
  54. continue
  55. y = ((int(x['create_time']) + 8 * 3600) // 86400 * 86400 - 8 * 3600,
  56. stage,
  57. '七悦有声',
  58. x['wechat_app_name'], # 公众号名称
  59. x['channel_id'],
  60. x['user_open_id'],
  61. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['create_time'])),
  62. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['user_create_time'])), # 用户注册时间
  63. x['money'],
  64. x['book_name'],
  65. x['transaction_no'] # 订单id
  66. )
  67. order_list.append(y)
  68. next_page_url = result_data['next_page_url']
  69. if next_page_url is None:
  70. break
  71. page += 1
  72. # print(len(order_list))
  73. print(f'{stage} [{start}~{end}] 有订单{order_list.__len__()}')
  74. if order_list.__len__() > 0:
  75. save_order(db_operation=self.db_quchen_text, order_list=order_list)
  76. # print(order_list)
  77. return order_list
  78. if __name__ == "__main__":
  79. account = ['趣程15期',
  80. 'eyJpdiI6Ilc0dmJWTjlHZnpJVVUwM3Q3dlc2aWc9PSIsInZhbHVlIjoiNFFvbXJISzBoTExoa0NJMmtXd0FMUT09IiwibWFjIjoiNTY1YjA3MTVlMzliYzg2MzcxMjZjOTRkYTMyY2FlZmJmNDUyZjYyZGEzM2I4MTMxNDNhMTIwNTIzZWViZjMyMSJ9']
  81. start = '2021-04-01'
  82. end = '2021-04-22'
  83. # AudioQiyue().get_order(start=start, end=end, account=account)
  84. AudioQiyue().run(start=start, end=end)