order_util.py 7.4 KB

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