PromoterInfoWebHook.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. """
  2. Author renyupeng
  3. coding=utf-8
  4. @Time : 2023/2/9 5:07 下午
  5. @Site :
  6. @File : PromoterInfoWebHook.py
  7. @Software: PyCharm
  8. @contact: renyupeng@c-top.com.cn
  9. @Tel 1501435553
  10. """
  11. import os
  12. import random
  13. import redis
  14. import requests
  15. from flask import Flask, request, json
  16. from multiprocessing import Process
  17. from DouyinSpider.SpiderDouyinHeader import SpiderDouyinHeader
  18. from constant.ConfConstant import ConfConstant
  19. from spider.PromoterFansInfo import PromoterFansInfo
  20. from spider.PromoterInfoSpider import PromoterInfoSpider
  21. from spider.PromoterLiveInfoSpider import PromoterLiveInfoSpider
  22. from spider.PromoterVideoAnalysisInfo import PromoterVideoAnalysisInfo
  23. from spider.PromoterVideoAnalysisTrend import PromoterVideoAnalysisTrend
  24. from spider.ReturnPromoterInfoSpider import ReturnPromoterInfoSpider
  25. from utils.EtlFirstDeliveryValidMaterials import EtlFirstDeliveryValidMaterials
  26. from utils.UpLoadFile import UpLoadFile
  27. from utils.cookie_update import cookie_update
  28. from utils.douyin_cookie_update import douyin_cookie_update
  29. app = Flask(__name__)
  30. def api_root():
  31. return 'Welcome guys'
  32. @app.route('/promoterInfo/getPromoterId', methods=['POST'])
  33. def webhook_get_promoter():
  34. # r = redis.Redis(host='192.168.0.99', password='', port=6379, db=0)
  35. rep = json.loads(request.data)
  36. promoterId = rep["promoterId"]
  37. media_id = rep["mediaId"]
  38. if media_id == '2' or media_id is None:
  39. if media_id is None:
  40. # cookie_update.get_click_coookie_to_redis()
  41. # random_member = r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
  42. # random_item = random.choice(random_member)
  43. cookie = cookie_update.get_cookie_handler()[0]
  44. # cookie = random_item[0].decode('utf-8')
  45. roll = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, cookie)
  46. print(roll, '-----000-----{promoterId}'.format(promoterId=promoterId))
  47. return roll
  48. else:
  49. # random_member = r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
  50. # random_item = random.choice(random_member)
  51. # cookie = random_item[0].decode('utf-8')
  52. cookie = cookie_update.get_cookie_handler()[0]
  53. print(cookie, '----------cookie--------------')
  54. phone_num = cookie_update.get_cookie_handler()[1]
  55. print(phone_num, '------phone_num-------')
  56. results = ReturnPromoterInfoSpider.PromoterInfoSpiderHandler(promoterId, cookie)
  57. print(results, '-----results-----{promoterId}'.format(promoterId=promoterId))
  58. if json.loads(results).__contains__("result"):
  59. if json.loads(results)["result"] == 109:
  60. # r.zrem('kuaishou_shop_click_token', random_item[0])
  61. # retry_item = cookie_update.r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
  62. # print(retry_item[0][0], '-----cookie-----')
  63. retry_cookie = cookie_update.get_cookie_handler()[0]
  64. # retry_cookie = retry_item[0][0].decode('utf-8')
  65. cookie_update.update_cookie_handler(phone_num)
  66. rollback = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, retry_cookie)
  67. p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, retry_cookie))
  68. p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, retry_cookie))
  69. p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander,
  70. args=(promoterId, retry_cookie))
  71. p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler,
  72. args=(promoterId, retry_cookie))
  73. p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler,
  74. args=(promoterId, retry_cookie))
  75. p1.start()
  76. p2.start()
  77. p3.start()
  78. p4.start()
  79. p5.start()
  80. print(rollback, '--222---{promoterId}'.format(promoterId=promoterId))
  81. return rollback
  82. else:
  83. cookie_update.update_temporary_cookie_handler(phone_num)
  84. # r.zrem('kuaishou_shop_click_token', random_item[0])
  85. # retry_item = cookie_update.r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
  86. # retry_cookie = retry_item[0][0].decode('utf-8')
  87. retry_cookie = cookie_update.update_cookie_handler(phone_num)
  88. rolls = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, retry_cookie)
  89. p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, retry_cookie))
  90. p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, retry_cookie))
  91. p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander,
  92. args=(promoterId, retry_cookie))
  93. p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler,
  94. args=(promoterId, retry_cookie))
  95. p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler,
  96. args=(promoterId, retry_cookie))
  97. p1.start()
  98. p2.start()
  99. p3.start()
  100. p4.start()
  101. p5.start()
  102. print(rolls, '-------333-roll3---------{promoterId}'.format(promoterId=promoterId))
  103. return rolls
  104. else:
  105. p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, cookie))
  106. p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, cookie))
  107. p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander, args=(promoterId, cookie))
  108. p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler,
  109. args=(promoterId, cookie))
  110. p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler,
  111. args=(promoterId, cookie))
  112. p1.start()
  113. p2.start()
  114. p3.start()
  115. p4.start()
  116. p5.start()
  117. # r.zrem('kuaishou_shop_click_token', random_item[0])
  118. print(results, '--------444---roll1---------{promoterId}'.format(promoterId=promoterId))
  119. return results
  120. else:
  121. rep = json.loads(request.data)
  122. promoterId = rep["promoterId"]
  123. cookie = douyin_cookie_update.get_cookie_handler()[0]
  124. phone_num = douyin_cookie_update.get_cookie_handler()[1]
  125. roll = SpiderDouyinHeader().DouyinHandler(promoterId, cookie)
  126. if roll != 10086:
  127. print(roll, '--------roll2---------{promoterId}'.format(promoterId=promoterId))
  128. return roll
  129. else:
  130. douyin_cookie_update.update_cookie_handler(phone_num)
  131. return {}
  132. @app.route('/promoterInfo/updatePromoter', methods=['POST'])
  133. def webhook_update_promoter():
  134. # r = redis.Redis(host='192.168.0.99', password='', port=6379, db=0)
  135. rep = json.loads(request.data)
  136. promoterId = rep["promoterId"]
  137. # if r.zcard('kuaishou_shop_click_token') == 0:
  138. # cookie_update.get_click_coookie_to_redis()
  139. # else:
  140. # random_member = r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
  141. # random_item = random.choice(random_member)
  142. # # cookie = random_item[0].decode('utf-8')
  143. cookie = cookie_update.get_cookie_handler()[0]
  144. ReturnPromoterInfoSpider().PromoterUpdate(promoterId, cookie)
  145. # r.close()
  146. return '0'
  147. @app.route('/webhook/get_file', methods=['POST'])
  148. def api_webhook_get_file():
  149. payload = {}
  150. headers = {
  151. 'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
  152. 'Accept': '*/*',
  153. 'Host': 'api.tjyourong.com.cn',
  154. 'Connection': 'keep-alive'
  155. }
  156. if 'file' not in request.files:
  157. url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error')
  158. response = requests.request("GET", url, headers=headers, data=payload)
  159. print(response.text, 'error1')
  160. return '200'
  161. file = request.files['file']
  162. if file.filename == '':
  163. url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error')
  164. response = requests.request("GET", url, headers=headers, data=payload)
  165. print(response.text, 'error2')
  166. return '200'
  167. try:
  168. file.save('/home/excel/' + 'upload.csv')
  169. status = UpLoadFile.uploadFile('upload.csv')
  170. if status == 0:
  171. material_status = EtlFirstDeliveryValidMaterials().handler()
  172. if material_status == 0:
  173. url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(
  174. msg='success')
  175. requests.request("GET", url, headers=headers, data=payload)
  176. else:
  177. url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(
  178. msg='error')
  179. requests.request("GET", url, headers=headers, data=payload)
  180. else:
  181. url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error')
  182. requests.request("GET", url, headers=headers, data=payload)
  183. if os.path.isfile('/home/excel/' + 'upload.csv'):
  184. print('文件存在删除文件')
  185. os.remove('/home/excel/' + 'upload.csv')
  186. return '200'
  187. else:
  188. print('文件不存在')
  189. return '200'
  190. except Exception as e:
  191. url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error')
  192. response = requests.request("GET", url, headers=headers, data=payload)
  193. print(response.text, 'error3')
  194. return '200'
  195. if __name__ == '__main__':
  196. app.run(port=9999, host=ConfConstant.URL, debug=True)