yangguang.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. from model.DataBaseUtils import MysqlUtils
  2. from model.DingTalkUtils import DingTalkDecorators,DingTalkUtils
  3. from model.DateUtils import DateUtils
  4. from model import ComUtils
  5. import time
  6. import json
  7. import requests
  8. db = MysqlUtils()
  9. du = DateUtils()
  10. @DingTalkDecorators("阳光")
  11. def yangguang(start=None, end=None):
  12. accounts = get_account("阳光")
  13. if start:
  14. start = start + ' 00:00:00'
  15. end = end + ' 23:59:59'
  16. else:
  17. start = du.getTodayOrYestoday() + ' 00:00:00'
  18. end = du.get_n_hours_ago(0)
  19. client_id = 10008097
  20. token = '2xa1d55tTPBjeEA8Ho'
  21. if accounts.__len__() == 0:
  22. return
  23. else:
  24. print(f"阳光账号数:{accounts.__len__()}")
  25. for i in accounts:
  26. stage = i[0]
  27. vip_id = i[1]
  28. print(vip_id)
  29. # get_yg_vip_channel(stage, vip_id, client_id, token)
  30. get_yg_data(stage, vip_id, client_id, token, start, end)
  31. print(check())
  32. parse_order_data()
  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 get_yg_vip_channel(stage, vip_id, client_id, token):
  54. url = "https://data.yifengaf.cn:443/channeldata/data/account/list"
  55. nonce = ComUtils.get_random_str()
  56. timestamp = int(time.time())
  57. signaure = ComUtils.sha1(str(token) + str(timestamp) + str(client_id) + str(nonce))
  58. params = {
  59. "client_id": client_id,
  60. "token": token,
  61. "nonce": nonce,
  62. "timestamp": timestamp,
  63. "signaure": signaure,
  64. "vip_id": vip_id,
  65. }
  66. headers = {"Content-Type": "application/json"}
  67. r = requests.post(url=url, data=json.dumps(params), headers=headers)
  68. print(r.text)
  69. task_id = json.loads(r.text).get("data").get("task_id")
  70. db.quchen_text.execute(
  71. f"replace into yangguang_path(vip_id,task_id,stage,type) values ('{vip_id}','{task_id}','{stage}','channel')")
  72. def get_channel_info():
  73. accounts = get_account("阳光")
  74. client_id = 10008097
  75. token = '2xa1d55tTPBjeEA8Ho'
  76. for i in accounts:
  77. stage = i[0]
  78. vip_id = i[1]
  79. print(vip_id)
  80. get_yg_vip_channel(stage, vip_id, client_id, token)
  81. def parse_order_data():
  82. print(111)
  83. accounts = get_account("阳光")
  84. for i in accounts:
  85. # print(i)
  86. vip_id = i[1]
  87. stage = i[0]
  88. data = parse_order(vip_id, stage)
  89. save_data(data)
  90. def parse_order(vip_id,stage):
  91. url = db.quchen_text.getOne(f"select path from yangguang_path where type='channel' and vip_id={vip_id} ")
  92. r = requests.get(url).text
  93. channel_di={}
  94. a = r.split('}')
  95. for i in a[:-1]:
  96. if i[-1] != '}':
  97. b=json.loads(i + "}", strict=False)
  98. else:
  99. b=json.loads(i, strict=False)
  100. channel_di[b["channel_id"]]=(b["wx_nickname"],b['app_id'])
  101. # print(channel_di)
  102. print(f'{stage} 有channel数:{len(channel_di)}')
  103. info=db.quchen_text.getData(f"select stage,path from yangguang_path where type='order' and vip_id={vip_id}")
  104. stage=info[0][0]
  105. path=info[0][1]
  106. text=requests.get(path).text.replace('"referral_url":,','')
  107. # print(text)
  108. insert_data=[]
  109. for j in text.split("}")[:-1]:
  110. if j[-1] != '}':
  111. j=j+'}'
  112. try:
  113. di=json.loads(j, strict=False)
  114. except Exception as e:
  115. print(j)
  116. print(e)
  117. insert_data.append((
  118. di["create_time"][:10],
  119. stage,
  120. '阳光',
  121. channel_di[di['channel_id']][0],
  122. di['channel_id'],
  123. di['openid'],
  124. di['create_time'],
  125. di['user_createtime'],
  126. di['money'],
  127. di.get('book_name'),
  128. di['merchant_id'],
  129. 2 if di['state']=="完成" else 1,
  130. di['user_id'],
  131. channel_di[di['channel_id']][1],
  132. 1 if di['type'] == '书币充值' else 2,
  133. di['merchant_id'],
  134. di['transaction_id']
  135. ))
  136. # print(insert_data)
  137. # exit(0)
  138. print("订单数:"+ str(insert_data.__len__()))
  139. save_data(insert_data)
  140. def save_data(data):
  141. sql = """replace into ods_order(date,stage,platform,channel,channel_id,user_id,order_time,
  142. reg_time,amount,from_novel,order_id,status,platform_user_id,wechat_app_id,order_type,
  143. trade_no,transaction_no) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) """
  144. # print(sql)
  145. db.quchen_text.executeMany(sql,data)
  146. def check():
  147. x = 1
  148. while True:
  149. a = db.quchen_text.getOne("select count(1) from yangguang_path where type ='order' and path is null")
  150. print(f" 回调接口 待处理数量 {a} ")
  151. if a == 0:
  152. info = '回调完成'
  153. break
  154. time.sleep(60)
  155. x += 1
  156. if x > 10:
  157. DingTalkUtils.send('阳光订单回调延时10min', '18860455786')
  158. info = '回调未完成'
  159. break
  160. return info
  161. def get_account(plactform, id=None):
  162. op = f" and id={id} " if id else ''
  163. data = db.quchen_text.getData(f"select text from order_account_text where platform='{plactform}' {op}")
  164. new_data = []
  165. for i in data:
  166. new_data.append(i[0].replace('\n', '').split(","))
  167. return new_data
  168. def get_yg_user_info():
  169. """获取阳光的用户信息"""
  170. accounts = get_account("阳光")
  171. # if start:
  172. # start = start + ' 00:00:00'
  173. # end = end + ' 23:59:59'
  174. # else:
  175. # start = du.getTodayOrYestoday() + ' 00:00:00'
  176. # end = du.get_n_hours_ago(0)
  177. client_id = 10008097
  178. token = '2xa1d55tTPBjeEA8Ho'
  179. if accounts.__len__() == 0:
  180. return
  181. else:
  182. print(f"阳光账号数:{accounts.__len__()}")
  183. for i in accounts:
  184. stage = i[0]
  185. vip_id = i[1]
  186. print(vip_id)
  187. # get_yg_vip_channel(stage, vip_id, client_id, token)
  188. get_yg_data(stage, vip_id, client_id, token, start, end)
  189. print(check())
  190. # parse_data()
  191. def daily_yg():
  192. st = du.get_n_days(-10)
  193. et = du.get_n_days(-1)
  194. yangguang(st, et)
  195. if __name__ == '__main__':
  196. # get_channel_info()
  197. # exit(0)
  198. # yangguang('2020-09-20','2020-10-01')
  199. # # exit(0)
  200. # yangguang('2021-05-28','2021-05-28')
  201. # daily_yg()
  202. # for i in du.split_date2('2020-06-28','2020-11-03',30):
  203. # print(i)
  204. # yangguang(i[0], i[1])
  205. # parse_order_data()
  206. yangguang()