get_order_dairly_wending.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import hashlib
  2. import time
  3. import datetime
  4. from concurrent.futures import ProcessPoolExecutor
  5. import requests
  6. from util import date_util
  7. from util import platform_config_util
  8. from util import robust_util
  9. from util.MySQLConnection import MySQLConnection
  10. def md5(s):
  11. md5 = hashlib.md5()
  12. md5.update(s.encode("utf-8"))
  13. return md5.hexdigest()
  14. def getSelfDateStr(times=time.time(),date_format='%Y%m%d'):
  15. """
  16. ## 20201028添加,阳光接口,文鼎接口,日期参数请求格式20201028,一日一拉api数据
  17. description: 获取指定时间戳
  18. time: 秒 默认当前时间
  19. return: 返回指定时间戳的前一日日期 。 比如 :接收20190512号的时间戳,返回 20190513 -> str
  20. tips: 一天86400秒
  21. """
  22. timestamps = str(time.strftime(date_format,time.localtime(times)))
  23. return timestamps
  24. def get_wending_account_list():
  25. """
  26. des cription: 文鼎账号列表
  27. return: [['consumerkey', 'secretkey', 'siteid', 'stage', 'account']] ->list
  28. """
  29. return platform_config_util.get_account_list('文鼎', 'wending_account_config.csv')
  30. def get_wending_order(st,et,account_list):
  31. total_order_list = ()
  32. start_exec_seconds = date_util.getCurrentSecondTime()
  33. futures = []
  34. for account in account_list:
  35. futures.append(get_wending_order_task(st, et, account))
  36. for future in futures:
  37. if len(future)>0:
  38. total_order_list = future + total_order_list
  39. print('文鼎订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  40. return total_order_list
  41. def get_wd_account_siteid_list(account):
  42. url = 'https://bi.reading.163.com/dist-api/siteList'
  43. consumerkey = account[0]
  44. secretkey = account[1]
  45. stage = account[3]
  46. timestamp = int(time.time()*1000)
  47. siteid_params = {
  48. "consumerkey":consumerkey,
  49. 'secretkey':secretkey,
  50. 'timestamp':timestamp,
  51. }
  52. sorted_data = sorted(siteid_params.items(),reverse = False)
  53. s=""
  54. for k,v in sorted_data:
  55. s = s+str(k)+"="+str(v)
  56. sign = md5(s).lower()
  57. siteid_params['sign'] = sign
  58. consumerkey = siteid_params['consumerkey']
  59. timestamp = siteid_params['timestamp']
  60. parameter = 'consumerkey='+str(consumerkey)+'&timestamp='+str(timestamp)+'&sign='+str(sign)
  61. get_url = url+"?"+parameter
  62. while True:
  63. r = requests.get(url=get_url)
  64. if r.status_code==200:
  65. break
  66. try:
  67. id_key_list = r.json()['data']
  68. except:
  69. return []
  70. mpid_list = []
  71. try:
  72. for id_key_val in id_key_list:
  73. mpid = dict(id_key_val)["mpId"]
  74. mpid_list.append(mpid)
  75. except Exception as e:
  76. print(stage,'站点查询返回结果:',r.json())
  77. return mpid_list
  78. def get_wending_json_object(url,params):
  79. params['timestamp'] = int(time.time()*1000)
  80. sorted_data = sorted(params.items(),reverse = False)
  81. s=""
  82. for k,v in sorted_data:
  83. s = s+str(k)+"="+str(v)
  84. sign = md5(s).lower()
  85. params['sign'] = sign
  86. consumerkey = params['consumerkey']
  87. secretkey = params['secretkey']
  88. timestamp = params['timestamp']
  89. siteid = params['siteid']
  90. pageSize = params['pageSize']
  91. starttime = params['starttime']
  92. endtime = params['endtime']
  93. page = params['page']
  94. paystatus = params['paystatus']
  95. ## +'&secretkey='+str(secretkey)
  96. parameter = 'consumerkey='+str(consumerkey)+'&timestamp='+str(timestamp)+'&siteid='+str(siteid)+'&pageSize='+str(pageSize)\
  97. +'&starttime='+str(starttime)+'&endtime='+str(endtime)+'&page='+str(page)+'&paystatus='+str(paystatus)+'&sign='+str(sign)
  98. global get_url
  99. get_url = url+"?"+parameter
  100. while True:
  101. r= requests.get(url=get_url)
  102. if r.status_code==200:
  103. break
  104. else:
  105. time.sleep(1)
  106. print("请求连接出错,等待1s...")
  107. response_result_json=r.json()
  108. del params['sign']
  109. return response_result_json
  110. def get_wending_order_task(st,et,account):
  111. order_list = ()
  112. url = 'https://bi.reading.163.com/dist-api/rechargeList'
  113. consumerkey = account[0]
  114. secretkey = account[1]
  115. siteid=account[2]
  116. stage = account[3]
  117. siteid_list = get_wd_account_siteid_list(account)
  118. print(siteid_list)
  119. if len(siteid_list) == 0:
  120. siteid_list.append(siteid)
  121. starttime = getSelfDateStr(st,'%Y%m%d%H%M')
  122. endtime = getSelfDateStr(et,'%Y%m%d%H%M')
  123. for siteid in siteid_list:
  124. page = 1
  125. while True:
  126. params = {
  127. 'consumerkey': consumerkey,
  128. 'secretkey':secretkey,
  129. 'timestamp':int(1601481600),
  130. 'siteid':siteid,
  131. 'pageSize':1000,
  132. 'starttime':starttime,
  133. 'endtime':endtime,
  134. 'page':page,
  135. 'paystatus':1}
  136. response_result_json = get_wending_json_object(url,params)
  137. order_item_list = response_result_json['data']['rechargeList']
  138. for x in order_item_list:
  139. y={}
  140. y['date'] = (int(x['payTime']//1000)+ 8 * 3600) // 86400 * 86400 - 8 * 3600 ## 网易的是13位时间戳
  141. y['platform'] = '文鼎'
  142. y['channel'] = x['wx_mpName'] ## 公众号名称
  143. y['channel_id'] = x['wx_originalId'] ## 公众号id
  144. y['from_novel'] = x['bookTitle'] ## 小说名称
  145. y['user_id'] = x['userId'] ## 付费用户uid
  146. y['stage'] = stage ## 期数
  147. createTime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(x['createTime']//1000)) ## 时间戳 》struct_time 》标准时间
  148. y['order_time']= createTime ## 订单生成时间
  149. y['amount']=x['money']/100 ## 原数据单位:分
  150. uid_reg_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(x['userRegisterTime']//1000)) ## 13位时间戳 》标准时间
  151. y['reg_time']= uid_reg_time ## 用户注册时间
  152. y['order_id']= x['ewTradeId'] ## 订单id
  153. y = sorted(y.items(), key=lambda item:item[0])
  154. y = dict(y)
  155. y = tuple(y.values())
  156. order_list = order_list+((y),)
  157. if len(order_item_list)<1000:
  158. break
  159. else:
  160. page+=1
  161. print(f"文鼎数据日期-{starttime}到{endtime}-期数-{stage}-获取数据-{len(order_list)}条")
  162. return order_list
  163. def batch_save_order(data):
  164. if data is None or len(data) == 0:
  165. print('数据为空,不执行数据库操作!')
  166. else:
  167. 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);'
  168. connect = MySQLConnection()
  169. try:
  170. num = connect.batch(sql, data)
  171. # 提交
  172. connect.commit()
  173. print('订单数据最终入库【{num}】条'.format(num=num))
  174. except Exception as e:
  175. print('订单数据入库失败:', e)
  176. finally:
  177. connect.close()
  178. def start_order_job():
  179. st_unix = date_util.get_n_day(n=-1, is_timestamp=1)
  180. et_unix = date_util.get_n_day(is_timestamp=1)
  181. account_list = get_wending_account_list()
  182. print("account get success")
  183. batch_save_order(get_wending_order(st_unix, et_unix,account_list))
  184. if __name__ == '__main__':
  185. start_order_job()