get_order.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import requests
  2. import time
  3. import hashlib
  4. import json
  5. from model.ComUtils import ComUtils
  6. from model.DataBaseUtils import MysqlUtils
  7. from model.DateUtils import DateUtils
  8. db=MysqlUtils()
  9. class OrderAccount():
  10. def get_account(self,plactform):
  11. data = db.quchen_text.getData(f"select text from order_account_text where platform='{plactform}'")
  12. new_data = []
  13. for i in data:
  14. new_data.append(i[0].replace('\n', '').split(","))
  15. return new_data
  16. def get_yg_acccount(self):
  17. return self.get_account("阳光")
  18. def get_wd_account(self):
  19. return self.get_account("文鼎")
  20. class GetOrderData(ComUtils,OrderAccount):
  21. def yg(self,start,end):
  22. client_id = 10008097
  23. token = '2xa1d55tTPBjeEA8Ho'
  24. for i in self.get_yg_acccount():
  25. stage = i[0]
  26. vip_id = i[1]
  27. print(vip_id)
  28. self.get_yg_vip_channel(stage,vip_id,client_id,token)
  29. self.get_yg_data(stage, vip_id, client_id, token,start,end)
  30. self.yg_prase()
  31. def yg_prase(self):
  32. while True:
  33. a = db.quchen_text.getOne("select count(1) from yangguang_path where update_time is null")
  34. print(a)
  35. if a==0:
  36. break
  37. time.sleep(60)
  38. for i in self.get_yg_acccount():
  39. vip_id = i[1]
  40. self.parse_yg_data(vip_id)
  41. def get_yg_data(self,stage,vip_id,client_id,token,start,end):
  42. url = 'https://data.yifengaf.cn:443/channeldata/data/orders/list'
  43. nonce=self.get_random_str()
  44. timestamp=int(time.time())
  45. signaure =self.sha1(str(token) + str(timestamp) + str(client_id) + str(nonce))
  46. params = {
  47. "client_id": client_id,
  48. "token": token,
  49. "nonce": nonce,
  50. "timestamp": timestamp,
  51. "signaure": signaure,
  52. "vip_id": vip_id,
  53. "start_time":start, # %Y-%m-%d %H:%i:%s:
  54. "end_time":end
  55. }
  56. headers={"Content-Type":"application/json"}
  57. r=requests.post(url=url,data=json.dumps(params),headers=headers)
  58. print(r.text)
  59. task_id = json.loads(r.text).get("data").get("task_id")
  60. db.quchen_text.execute(f"replace into yangguang_path(vip_id,task_id,stage,type) values ('{vip_id}','{task_id}','{stage}','order')")
  61. def get_yg_vip_channel(self,stage,vip_id,client_id,token):
  62. url='https://data.yifengaf.cn:443/channeldata/data/account/list'
  63. nonce = self.get_random_str()
  64. timestamp = int(time.time())
  65. signaure = self.sha1(str(token) + str(timestamp) + str(client_id) + str(nonce))
  66. params = {
  67. "client_id": client_id,
  68. "token": token,
  69. "nonce": nonce,
  70. "timestamp": timestamp,
  71. "signaure": signaure,
  72. "vip_id": vip_id,
  73. }
  74. headers = {"Content-Type": "application/json"}
  75. r=requests.post(url=url,data=json.dumps(params),headers=headers)
  76. print(r.text)
  77. task_id= json.loads(r.text).get("data").get("task_id")
  78. db.quchen_text.execute(f"replace into yangguang_path(vip_id,task_id,stage,type) values ('{vip_id}','{task_id}','{stage}','channel')")
  79. def parse_yg_data(self,vip_id):
  80. url=db.quchen_text.getOne(f"select path from yangguang_path where type='channel' and vip_id={vip_id} ")
  81. r= requests.get(url).text
  82. channel_di={}
  83. a = r.split('}')
  84. for i in a[:-1]:
  85. if i[-1] != '}':
  86. b=json.loads(i + "}", strict=False)
  87. else:
  88. b=json.loads(i, strict=False)
  89. channel_di[b["channel_id"]]=b["wx_nickname"]
  90. print(channel_di)
  91. info=db.quchen_text.getData(f"select stage,path from yangguang_path where type='order' and vip_id={vip_id}")
  92. stage=info[0][0]
  93. path=info[0][1]
  94. text=requests.get(path).text.replace('"referral_url":,','')
  95. insert_data=[]
  96. for j in text.split("}")[:-1]:
  97. if j[-1] != '}':
  98. j=j+'}'
  99. try:
  100. di=json.loads(j, strict=False)
  101. except Exception as e:
  102. print(j)
  103. print(e)
  104. if di["state"] == "未完成":
  105. continue
  106. platform = "阳光"
  107. channel_id = di["channel_id"]
  108. channel=channel_di[channel_id]
  109. user_id = di["openid"]
  110. order_time = di["create_time"]
  111. reg_time = di["user_createtime"]
  112. from_novel = di["book_name"]
  113. amount = di["money"]
  114. order_id = di["transaction_id"]
  115. date = DateUtils.str_to_stamp(order_time[:10])
  116. insert_data.append((date,stage,platform,channel,channel_id,user_id,order_time,reg_time,amount,from_novel,order_id))
  117. # print(insert_data)
  118. db.quchen_text.executeMany("replace into `order`(date,stage,platform,channel,channel_id,"
  119. "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))
  120. if __name__ == '__main__':
  121. a=GetOrderData()
  122. a.yg_prase()
  123. # a.parse_yg_data(17382)
  124. # a.get_yg_data('趣程15期','17382','10008097','2xa1d55tTPBjeEA8Ho','2020-11-27','2020-12-28')
  125. # a.get_yg_vip_channel('趣程15期','17382','10008097','2xa1d55tTPBjeEA8Ho')