get_order_qiyue_new.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # !/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # 20201120
  4. ## 20201120新添加 七悦 平台接口
  5. ## 七悦api接口地址 https://vip.zhangwenwenhua.com/uploads/attachments/open-api/open-api.pdf
  6. ########################## 七悦订单接口,返回数据格式
  7. '''
  8. {'msg': 'ok', 'code': 0,
  9. 'data': {
  10. 'current_page': 1,
  11. 'data': [
  12. {'id': 167100631495102464, 'money': 30, 'type': 1, 'state': 2, 'user_id': 101280121, 'channel_id': 14880, 'create_time': 1605062783, 'finish_time': 1605062790
  13. , 'trade_no': '20201111104622_101280121_Ykhp', 'transaction_no': '4200000845202011115590974617', 'user_open_id': 'oxw0X6zGsL6dSX-Y8lsXMhPG1_cY'
  14. , 'user_is_subscribe': 1, 'user_subscribe_time': 1605045625, 'user_create_time': 1605045625
  15. , 'referral_url': 'https://wxaef9ff2c63776f6f.wenhuazw.com/index/book/chapter?book_id=10076083&sid=1007608300004&referral_id=727741'
  16. , 'book_name': '我的超级老婆', 'book_keywords': '', 'wechat_app_id': 'wxaef9ff2c63776f6f', 'wechat_app_name': '炎兵文楼', 'channel_name': '炎兵文楼(趣程)'
  17. , 'state_desc': '完成', 'type_desc': '书币充值'}
  18. ,{...}]
  19. ,'first_page_url': 'https://api.zhangwenwenhua.com/v1/orders?page=1'
  20. , 'from': 1
  21. , 'last_page': 1
  22. , 'last_page_url': 'https://api.zhangwenwenhua.com/v1/orders?page=1'
  23. , 'next_page_url': None
  24. , 'path': 'https://api.zhangwenwenhua.com/v1/orders'
  25. , 'per_page': '10'
  26. , 'prev_page_url': None
  27. , 'to': 5
  28. , 'total': 5
  29. }
  30. }
  31. '''
  32. #########################
  33. import time
  34. import requests
  35. from concurrent.futures import ProcessPoolExecutor
  36. import requests
  37. from util import date_util
  38. from util import platform_config_util ## 账号配置
  39. from util import robust_util
  40. # from apscheduler.schedulers.blocking import BlockingScheduler
  41. from util.MySQLConnection import MySQLConnection
  42. def get_qiyue_account_list():
  43. """
  44. des cription: 七悦账号列表
  45. return: [['stage','token']] ->list
  46. """
  47. return platform_config_util.get_account_list('七悦', 'qiyue_account_config.csv')
  48. def get_qiyue_order_task(date, account):
  49. order_list = ()
  50. order_url = "https://api.zhangwenwenhua.com" + "/v1/orders"
  51. stage = account[0]
  52. token = account[1]
  53. page = 1
  54. size = 50
  55. while True:
  56. while True:
  57. timestamp = int(time.time())
  58. url = order_url + "?" + "token=" + str(token) + "&timestamp=" + str(timestamp) + "&page=" + str(
  59. page) + "&size=" + str(size) + "&date=" + date
  60. r = requests.get(url=url)
  61. if r.status_code==200:
  62. break
  63. else:
  64. print(r.text)
  65. time.sleep(61)
  66. response_result_json = r.json()
  67. result_data = response_result_json['data']
  68. total = result_data['total']
  69. if total <= 0:
  70. break
  71. order_item_list = result_data['data']
  72. for x in order_item_list:
  73. if int(x['state']) != 2:
  74. continue
  75. y = {}
  76. y['platform'] = '七悦'
  77. y['channel'] = x['wechat_app_name'] ## 公众号名称
  78. y['channel_id'] = x['channel_id'] ## 公众号id
  79. y['from_novel'] = x['book_name'] ## 小说名称
  80. y['user_id'] = x['user_id'] ## 付费用户uid
  81. y['stage'] = stage ## 期数
  82. createTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['create_time']))
  83. y['order_time'] = createTime ## 订单生成时间
  84. y['amount'] = x['money'] ## 原数据单位:元
  85. uid_reg_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['user_create_time']))
  86. y['reg_time'] = uid_reg_time ## 用户注册时间
  87. y['order_id'] = x['id'] ## 订单id
  88. y['date'] = createTime
  89. y = sorted(y.items(), key=lambda item: item[0])
  90. y = dict(y)
  91. y = tuple(y.values())
  92. order_list = order_list + ((y),)
  93. next_page_url = result_data['next_page_url']
  94. if next_page_url is None:
  95. break
  96. page += 1
  97. print(f"【{stage}】-【{order_list.__len__()}】条")
  98. return order_list
  99. @robust_util.catch_exception
  100. def get_qiyue_order(dt, account_list):
  101. total_order_list = ()
  102. start_exec_seconds = date_util.getCurrentSecondTime()
  103. for account in account_list:
  104. order_list= get_qiyue_order_task(dt, account)
  105. total_order_list = order_list + total_order_list
  106. print('七悦订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  107. return total_order_list
  108. def batch_save_order(data):
  109. if data is None or len(data) == 0:
  110. print('数据为空,不执行数据库操作!')
  111. else:
  112. sql = 'INSERT IGNORE INTO quchen_text.ods_order(amount,channel,channel_id,date,from_novel,order_id,order_time,platform,reg_time,stage,user_id) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
  113. connect = MySQLConnection()
  114. try:
  115. num = connect.batch(sql, data)
  116. # 提交
  117. connect.commit()
  118. print('订单数据最终入库【{num}】条'.format(num=num))
  119. except Exception as e:
  120. print('订单数据入库失败:', e)
  121. finally:
  122. connect.close()
  123. def batch_save_order_new(data):
  124. if data is None or len(data) == 0:
  125. print('数据为空,不执行数据库操作!')
  126. else:
  127. sql = 'INSERT IGNORE INTO quchen_text.ods_order(amount,channel,channel_id,date,from_novel,order_id,order_time,platform,reg_time,stage,user_id) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
  128. connect = MySQLConnection()
  129. try:
  130. num = connect.batch(sql, data)
  131. # 提交
  132. connect.commit()
  133. print('订单数据最终入库【{num}】条'.format(num=num))
  134. except Exception as e:
  135. print('订单数据入库失败:', e)
  136. finally:
  137. connect.close()
  138. def start_order_job_qiyue(dt):
  139. account_list = get_qiyue_account_list()
  140. batch_save_order(get_qiyue_order(dt, account_list))
  141. if __name__ == '__main__':
  142. dt=date_util.get_n_day()
  143. start_order_job_qiyue(dt)