PromoterInfoWebHook.py 10.0 KB

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