get_order_dairly_qiyue.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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(st,et,account):
  49. order_list = ()
  50. url_frequency = 1
  51. # 参数
  52. order_url = "https://api.zhangwenwenhua.com" + "/v1/orders"
  53. stage = account[0]
  54. token = account[1]
  55. page = 1
  56. size = 50
  57. order_st_date = date_util.getSelfDateStr(int(st-86400),date_format='%Y-%m-%d')
  58. for i in range((et-st)//86400 + 1):
  59. statis_unix_time = et - (i+1)*86400
  60. date = date_util.getSelfDateStr(statis_unix_time,date_format='%Y-%m-%d')
  61. while True:
  62. if st == et:
  63. break
  64. if url_frequency % 6 == 0 :
  65. time.sleep(61)
  66. url_frequency += 1
  67. timestamp = int(time.time())
  68. url = order_url + "?" + "token=" + str(token) +"&timestamp=" + str(timestamp) +"&page=" + str(page) +"&size=" + str(size) +"&date=" + date
  69. response_result_json = requests.get(url=url).json()
  70. code = response_result_json['code']
  71. if code != 0:
  72. print(stage,'七悦充值接口异常:',response_result_json)
  73. break
  74. result_data = response_result_json['data']
  75. total = result_data['total']
  76. if total <= 0:
  77. break
  78. #total = result_data['total']
  79. #last_page2 = int(total//size)+1
  80. last_page = result_data['last_page']
  81. order_item_list = result_data['data']
  82. for x in order_item_list:
  83. if int(x['state']) != 2:
  84. continue
  85. y={}
  86. y['date'] = (int(x['finish_time'])+ 8 * 3600) // 86400 * 86400 - 8 * 3600
  87. y['platform'] = '七悦'
  88. y['channel'] = x['wechat_app_name'] ## 公众号名称
  89. y['channel_id'] = x['channel_id'] ## 公众号id
  90. y['from_novel'] = x['book_name'] ## 小说名称
  91. y['user_id'] = x['user_id'] ## 付费用户uid
  92. y['stage'] = stage ## 期数
  93. createTime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(x['create_time']))
  94. y['order_time']= createTime ## 订单生成时间
  95. y['amount']=x['money'] ## 原数据单位:元
  96. uid_reg_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(x['user_create_time']))
  97. y['reg_time']= uid_reg_time ## 用户注册时间
  98. y['order_id']= x['id'] ## 订单id
  99. y = sorted(y.items(), key=lambda item:item[0])
  100. y = dict(y)
  101. y = tuple(y.values())
  102. order_list = order_list+((y),)
  103. '''
  104. if page == last_page:
  105. break
  106. page += 1
  107. '''
  108. next_page_url = result_data['next_page_url']
  109. if next_page_url is None:
  110. break
  111. page += 1
  112. print(f"数据日期-{order_st_date}到{date}-期数-{stage}-获取数据-{len(order_list)}条,例如: {order_list[0:1]}")
  113. return order_list
  114. @robust_util.catch_exception
  115. def get_qiyue_order(st,et,account_list):
  116. total_order_list = ()
  117. start_exec_seconds = date_util.getCurrentSecondTime()
  118. #account_list = get_qiyue_account_list()
  119. executor = ProcessPoolExecutor(max_workers=5)
  120. futures = []
  121. for account in account_list:
  122. future = executor.submit(get_qiyue_order_task, st, et, account)
  123. futures.append(future)
  124. executor.shutdown(True)
  125. for future in futures:
  126. order_list = future.result()
  127. if len(order_list) > 0:
  128. total_order_list = order_list + total_order_list
  129. print('七悦订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  130. return total_order_list
  131. def batch_save_order(data):
  132. if data is None or len(data) == 0:
  133. print('数据为空,不执行数据库操作!')
  134. else:
  135. sql = 'INSERT IGNORE INTO quchen_text.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);'
  136. connect = MySQLConnection()
  137. try:
  138. num = connect.batch(sql, data)
  139. # 提交
  140. connect.commit()
  141. print('订单数据最终入库【{num}】条'.format(num=num))
  142. except Exception as e:
  143. print('订单数据入库失败:', e)
  144. finally:
  145. connect.close()
  146. def start_order_job():
  147. start_exec_seconds = date_util.getCurrentSecondTime()
  148. #st_unix, et_unix = date_util.getPreviousHourAndCurrentHourSecondTime(start_exec_seconds)
  149. # st_unix = 1602313200 # 2020/10/10 15:0:0
  150. # et_unix = 1602316800 # 2020/10/10 16:0:0
  151. #print('查询开始时间:', st_unix, date_util.getSecondsToDatetime(st_unix))
  152. #print('查询结束时间:', et_unix, date_util.getSecondsToDatetime(et_unix))
  153. ## 20201105添加文鼎平台
  154. st_unix = date_util.getYesterdayStartTime()
  155. et_unix = date_util.getTodayStartTime()
  156. st_unix = et_unix - 60
  157. account_list = get_qiyue_account_list()
  158. '''
  159. ## 2个账户测试看看
  160. account_list = [
  161. ['趣程26期','eyJpdiI6IlBjV0Z4aVlWNWFUemczdWt5Y0R1QXc9PSIsInZhbHVlIjoiOHZtZUw1Q2RuVnRYd0p1M01WSWoxZz09IiwibWFjIjoiMDE2Mzk5NDFkMDkwZTBmNzU2ZWNmMWMxMzk5YWYwMWIwZmRmZjYzMTgyZDRjYTVkZGY1ZGI5ZGQxNjYyMGRiMSJ9']
  162. ,['趣程27期','eyJpdiI6IithdDArQ2hhazlxVXJKUXIzK01PQXc9PSIsInZhbHVlIjoiMjAyTEtNOUI2TlkwdklJXC9tNHgwc1E9PSIsIm1hYyI6IjA0YjE0Mjc1MWJjN2VlYmViNWM3YTRiNmI0ZDE5M2QyMDJhMGU3NjRlNTQ4MTliYTgwMzg0ZDNjYmUwY2ZmNWEifQ==']
  163. ]
  164. '''
  165. batch_save_order(get_qiyue_order(st_unix,et_unix,account_list))
  166. print('订单同步执行时间(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  167. #start_job_time = '2020-11-20 02:00:00'
  168. if __name__ == '__main__':
  169. scheduler = BlockingScheduler()
  170. #scheduler.add_job(start_order_job, 'interval', days =1 ,start_date=start_job_time)
  171. scheduler.add_job(start_order_job,'cron',hour ="02",minute="25")
  172. scheduler.start()