yangguang.py 7.5 KB

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