order_util.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import time
  2. from model import ComUtils
  3. import requests
  4. import json
  5. from model.DataBaseUtils import MysqlUtils
  6. from model.DateUtils import DateUtils
  7. from model.ComUtils import *
  8. import math
  9. from model.DateUtils import DateUtils
  10. db=MysqlUtils()
  11. du=DateUtils()
  12. def get_yg_vip_channel(stage, vip_id, client_id, token):
  13. url = "https://data.yifengaf.cn:443/channeldata/data/account/list"
  14. nonce = ComUtils.get_random_str()
  15. timestamp = int(time.time())
  16. signaure = ComUtils.sha1(str(token) + str(timestamp) + str(client_id) + str(nonce))
  17. params = {
  18. "client_id": client_id,
  19. "token": token,
  20. "nonce": nonce,
  21. "timestamp": timestamp,
  22. "signaure": signaure,
  23. "vip_id": vip_id,
  24. }
  25. headers = {"Content-Type": "application/json"}
  26. r = requests.post(url=url, data=json.dumps(params), headers=headers)
  27. print(r.text)
  28. task_id = json.loads(r.text).get("data").get("task_id")
  29. db.quchen_text.execute(
  30. f"replace into yangguang_path(vip_id,task_id,stage,type) values ('{vip_id}','{task_id}','{stage}','channel')")
  31. def get_yg_data(stage,vip_id,client_id,token,start,end):
  32. url = "https://data.yifengaf.cn:443/channeldata/data/orders/list"
  33. nonce=ComUtils.get_random_str()
  34. timestamp=int(time.time())
  35. signaure =ComUtils.sha1(str(token) + str(timestamp) + str(client_id) + str(nonce))
  36. params = {
  37. "client_id": client_id,
  38. "token": token,
  39. "nonce": nonce,
  40. "timestamp": timestamp,
  41. "signaure": signaure,
  42. "vip_id": vip_id,
  43. "start_time":start, # %Y-%m-%d %H:%i:%s:
  44. "end_time":end
  45. }
  46. headers={"Content-Type":"application/json"}
  47. r=requests.post(url=url,data=json.dumps(params),headers=headers)
  48. print(r.text)
  49. task_id = json.loads(r.text).get("data").get("task_id")
  50. db.quchen_text.execute(f"replace into yangguang_path(vip_id,task_id,stage,type) values ('{vip_id}','{task_id}','{stage}','order')")
  51. def parse_yg_data(vip_id):
  52. url = db.quchen_text.getOne(f"select path from yangguang_path where type='channel' and vip_id={vip_id} ")
  53. r = requests.get(url).text
  54. channel_di={}
  55. a = r.split('}')
  56. for i in a[:-1]:
  57. if i[-1] != '}':
  58. b=json.loads(i + "}", strict=False)
  59. else:
  60. b=json.loads(i, strict=False)
  61. channel_di[b["channel_id"]]=b["wx_nickname"]
  62. print(channel_di)
  63. info=db.quchen_text.getData(f"select stage,path from yangguang_path where type='order' and vip_id={vip_id}")
  64. stage=info[0][0]
  65. path=info[0][1]
  66. text=requests.get(path).text.replace('"referral_url":,','')
  67. insert_data=[]
  68. for j in text.split("}")[:-1]:
  69. if j[-1] != '}':
  70. j=j+'}'
  71. try:
  72. di=json.loads(j, strict=False)
  73. except Exception as e:
  74. print(j)
  75. print(e)
  76. if di["state"] == "未完成":
  77. continue
  78. platform = "阳光"
  79. channel_id = di["channel_id"]
  80. channel=channel_di[channel_id]
  81. user_id = di["openid"]
  82. order_time = di["create_time"]
  83. reg_time = di["user_createtime"]
  84. from_novel = di["book_name"]
  85. amount = di["money"]
  86. order_id = di["transaction_id"]
  87. date = DateUtils.str_to_stamp(order_time[:10])
  88. insert_data.append((date,stage,platform,channel,channel_id,user_id,order_time,reg_time,amount,from_novel,order_id))
  89. # print(insert_data)
  90. db.quchen_text.executeMany("replace into `order`(date,stage,platform,channel,channel_id,"
  91. "user_id,order_time,reg_time,amount,from_novel,order_id) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",tuple(insert_data))
  92. def get_hs_channel(account):
  93. url = 'https://vip.rlcps.cn/api/getMerchants'
  94. apiKey = str(account[0])
  95. apiSecurity = account[1]
  96. timestamp = str(int(time.time()))
  97. sign = md5(apiKey + timestamp + apiSecurity).upper()
  98. params = {
  99. 'apiKey': apiKey,
  100. 'apiSecurity': apiSecurity,
  101. 'timestamp': timestamp,
  102. 'sign': sign
  103. }
  104. response_result_json = requests.post(url, params).json()
  105. if 'data' not in response_result_json.keys():
  106. print('花生账号【{apiKey}】本次请求数据异常,响应报文【{result}】'.format(apiKey=apiKey, result=response_result_json))
  107. return
  108. return response_result_json['data']
  109. def get_huasheng_order_task(start,end, account, merchant,li):
  110. apiKey = str(account[0])
  111. apiSecurity = account[1]
  112. stage = account[2]
  113. timestamp = str(int(time.time()))
  114. order_url = 'https://vip.rlcps.cn/api/orderList'
  115. merchant_id = merchant['merchant_id']
  116. merchant_name = merchant['merchant_name']
  117. limit = 500
  118. for date in du.getDateLists(start,end):
  119. page = 1
  120. while True:
  121. sign = md5(apiKey + date + str(merchant_id) + timestamp + apiSecurity).upper()
  122. order_params = {
  123. 'apiKey': apiKey,
  124. 'apiSecurity': apiSecurity,
  125. 'timestamp': timestamp,
  126. 'date': date,
  127. 'merchant_id': merchant_id,
  128. 'sign': sign,
  129. 'page': page,
  130. 'limit': limit
  131. }
  132. r = requests.post(order_url, order_params)
  133. # print(r.text)
  134. response_result_json = r.json()
  135. if 'data' not in response_result_json.keys():
  136. print('花生账号【{key}】, 查询时间【{date}】, 渠道【{merchant_id}:{merchant_name}】本次请求数据异常,响应报文【{result}】'
  137. .format(key=apiKey, date=date, merchant_id=merchant_id, merchant_name=merchant_name,
  138. result=response_result_json))
  139. break
  140. if len(response_result_json['data']) == 0:
  141. break
  142. total_count = response_result_json['count']
  143. order_item_list = response_result_json['data']
  144. for order_item in order_item_list:
  145. if order_item['order_status'] == 1: # 1为已支付
  146. order = {}
  147. order['user_id'] = order_item['openid']
  148. order['order_id'] = order_item['trans_id']
  149. order['order_time'] = order_item['pay_at']
  150. order['reg_time'] = order_item['join_at']
  151. order['channel'] = merchant_name
  152. order['channel_id'] = merchant_id
  153. order['platform'] = '花生'
  154. order['stage'] = stage
  155. order['from_novel'] = order_item['book_name']
  156. order['amount'] = order_item['amount']
  157. order["date"]=int(time.mktime(time.strptime(order_item['pay_at'][:10],"%Y-%m-%d")))
  158. order = sorted(order.items(), key=lambda item: item[0])
  159. order = dict(order)
  160. order = tuple(order.values())
  161. if order.__len__()>0:
  162. li.append(order)
  163. if int(page) >= math.ceil(total_count / int(limit)):
  164. break
  165. page = page + 1
  166. def save_hs_data(data):
  167. sql = 'replace INTO quchen_text.`order` ' \
  168. '(amount,channel,channel_id,date,from_novel,order_id,order_time,platform,reg_time,stage,user_id)' \
  169. ' VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
  170. db.quchen_text.executeMany(sql,data)
  171. if __name__ == '__main__':
  172. print(du.str_to_stamp('2021-01-18'))
  173. a = time.strftime("%Y-%m-%d", time.localtime(1610899200))
  174. print(a)