123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- """
- Author renyupeng
- coding=utf-8
- @Time : 2023/2/9 5:07 下午
- @Site :
- @File : PromoterInfoWebHook.py
- @Software: PyCharm
- @contact: renyupeng@c-top.com.cn
- @Tel 1501435553
- """
- import os
- import random
- import redis
- import requests
- from flask import Flask, request, json
- from multiprocessing import Process
- from DouyinSpider.SpiderDouyinHeader import SpiderDouyinHeader
- from constant.ConfConstant import ConfConstant
- from spider.PromoterFansInfo import PromoterFansInfo
- from spider.PromoterInfoSpider import PromoterInfoSpider
- from spider.PromoterLiveInfoSpider import PromoterLiveInfoSpider
- from spider.PromoterVideoAnalysisInfo import PromoterVideoAnalysisInfo
- from spider.PromoterVideoAnalysisTrend import PromoterVideoAnalysisTrend
- from spider.ReturnPromoterInfoSpider import ReturnPromoterInfoSpider
- from utils.EtlFirstDeliveryValidMaterials import EtlFirstDeliveryValidMaterials
- from utils.UpLoadFile import UpLoadFile
- from utils.cookie_update import cookie_update
- from utils.douyin_cookie_update import douyin_cookie_update
- app = Flask(__name__)
- def api_root():
- return 'Welcome guys'
- @app.route('/promoterInfo/getPromoterId', methods=['POST'])
- def webhook_get_promoter():
- r = redis.Redis(host='192.168.0.99', password='', port=6379, db=0)
- rep = json.loads(request.data)
- promoterId = rep["promoterId"]
- media_id = rep["mediaId"]
- if media_id == '2' or media_id is None:
- if r.zcard('kuaishou_shop_click_token') == 0:
- cookie_update.get_click_coookie_to_redis()
- random_member = r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
- random_item = random.choice(random_member)
- cookie = cookie_update.get_cookie_handler()
- # cookie = random_item[0].decode('utf-8')
- roll = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, cookie)
- print(roll, '-----000-----{promoterId}'.format(promoterId=promoterId))
- return roll
- else:
- random_member = r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
- random_item = random.choice(random_member)
- # cookie = random_item[0].decode('utf-8')
- cookie = cookie_update.get_cookie_handler()
- print(cookie,'----------cookie--------------')
- phone_num = int(random_item[1])
- print(phone_num,'------phone_num-------')
- results = ReturnPromoterInfoSpider.PromoterInfoSpiderHandler(promoterId, cookie)
- print(results, '-----results-----{promoterId}'.format(promoterId=promoterId))
- if json.loads(results).__contains__("result"):
- if json.loads(results)["result"] == 109:
- r.zrem('kuaishou_shop_click_token', random_item[0])
- retry_item = cookie_update.r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
- print(retry_item[0][0], '-----cookie-----')
- retry_cookie = cookie_update.get_cookie_handler()
- # retry_cookie = retry_item[0][0].decode('utf-8')
- cookie_update.update_cookie_handler(phone_num)
- rollback = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, retry_cookie)
- p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, retry_cookie))
- p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, retry_cookie))
- p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander,
- args=(promoterId, retry_cookie))
- p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler,
- args=(promoterId, retry_cookie))
- p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler,
- args=(promoterId, retry_cookie))
- p1.start()
- p2.start()
- p3.start()
- p4.start()
- p5.start()
- print(rollback, '--222---{promoterId}'.format(promoterId=promoterId))
- return rollback
- else:
- cookie_update.update_temporary_cookie_handler(phone_num)
- r.zrem('kuaishou_shop_click_token', random_item[0])
- retry_item = cookie_update.r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
- # retry_cookie = retry_item[0][0].decode('utf-8')
- retry_cookie = cookie_update.update_cookie_handler(phone_num)
- rolls = ReturnPromoterInfoSpider().PromoterInfoSpiderHandler(promoterId, retry_cookie)
- p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, retry_cookie))
- p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, retry_cookie))
- p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander,
- args=(promoterId, retry_cookie))
- p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler,
- args=(promoterId, retry_cookie))
- p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler,
- args=(promoterId, retry_cookie))
- p1.start()
- p2.start()
- p3.start()
- p4.start()
- p5.start()
- print(rolls, '-------333-roll3---------{promoterId}'.format(promoterId=promoterId))
- return rolls
- else:
- p1 = Process(target=PromoterInfoSpider().PromoterInfoSpiderHandler, args=(promoterId, cookie))
- p2 = Process(target=PromoterFansInfo().PromoterFansInfoHandler, args=(promoterId, cookie))
- p3 = Process(target=PromoterLiveInfoSpider().PromoterLiveInfoSpiderHander, args=(promoterId, cookie))
- p4 = Process(target=PromoterVideoAnalysisInfo().PromoterVideoAnalysisInfoHandler,
- args=(promoterId, cookie))
- p5 = Process(target=PromoterVideoAnalysisTrend().PromoterVideoAnalysisTrendHandler,
- args=(promoterId, cookie))
- p1.start()
- p2.start()
- p3.start()
- p4.start()
- p5.start()
- r.zrem('kuaishou_shop_click_token', random_item[0])
- print(results, '--------444---roll1---------{promoterId}'.format(promoterId=promoterId))
- return results
- else:
- rep = json.loads(request.data)
- promoterId = rep["promoterId"]
- cookie = douyin_cookie_update.get_cookie_handler()[0]
- phone_num = douyin_cookie_update.get_cookie_handler()[1]
- roll = SpiderDouyinHeader().DouyinHandler(promoterId, cookie)
- if roll != 10086:
- print(roll, '--------roll2---------{promoterId}'.format(promoterId=promoterId))
- return roll
- else:
- douyin_cookie_update.update_cookie_handler(phone_num)
- return {}
- @app.route('/promoterInfo/updatePromoter', methods=['POST'])
- def webhook_update_promoter():
- r = redis.Redis(host='192.168.0.99', password='', port=6379, db=0)
- rep = json.loads(request.data)
- promoterId = rep["promoterId"]
- if r.zcard('kuaishou_shop_click_token') == 0:
- cookie_update.get_click_coookie_to_redis()
- else:
- random_member = r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True)
- random_item = random.choice(random_member)
- # cookie = random_item[0].decode('utf-8')
- cookie= cookie_update.get_cookie_handler()
- ReturnPromoterInfoSpider().PromoterUpdate(promoterId, cookie)
- r.close()
- return '0'
- @app.route('/webhook/get_file', methods=['POST'])
- def api_webhook_get_file():
- payload = {}
- headers = {
- 'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
- 'Accept': '*/*',
- 'Host': 'api.tjyourong.com.cn',
- 'Connection': 'keep-alive'
- }
- if 'file' not in request.files:
- url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error')
- response = requests.request("GET", url, headers=headers, data=payload)
- print(response.text, 'error1')
- return '200'
- file = request.files['file']
- if file.filename == '':
- url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error')
- response = requests.request("GET", url, headers=headers, data=payload)
- print(response.text, 'error2')
- return '200'
- try:
- file.save('/home/excel/' + 'upload.csv')
- status = UpLoadFile.uploadFile('upload.csv')
- if status == 0:
- material_status = EtlFirstDeliveryValidMaterials().handler()
- if material_status == 0:
- url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(
- msg='success')
- requests.request("GET", url, headers=headers, data=payload)
- else:
- url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(
- msg='error')
- requests.request("GET", url, headers=headers, data=payload)
- else:
- url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error')
- requests.request("GET", url, headers=headers, data=payload)
- if os.path.isfile('/home/excel/' + 'upload.csv'):
- print('文件存在删除文件')
- os.remove('/home/excel/' + 'upload.csv')
- return '200'
- else:
- print('文件不存在')
- return '200'
- except Exception as e:
- url = "http://api.tjyourong.com.cn/jeecg-boot/first/delivery/excelCallBack?msg={msg}".format(msg='error')
- response = requests.request("GET", url, headers=headers, data=payload)
- print(response.text, 'error3')
- return '200'
- if __name__ == '__main__':
- app.run(port=9999, host=ConfConstant.URL, debug=True)
|