""" Author renyupeng coding=utf-8 @Time : 2023/2/9 1:21 下午 @Site : @File : PromoterFansInfo.py @Software: PyCharm @contact: renyupeng@c-top.com.cn @Tel 1501435553 """ import json import requests from utils.mysql_helper import insert, batch_insert from utils.mysql_utils import MysqlUtils from utils.send_feishu_msg import SendFeiShuMsg class PromoterFansInfo: def __init__(self): self.conn = MysqlUtils() self.list = [1, 2, 3] def PromoterFansInfoHandler(self, promoterId): sql = "select cookie from ruixuan.kuaishou_supply_chain_cookie" cookie = self.conn.QueryOne(sql)[0] headers = {'User-Agent': 'Mozilla/5.0', 'Cookie': cookie} try: for i in self.list: url = 'https://cps.kwaixiaodian.com/distribute/pc/seller/promoter/fans/info?' \ 'promoterId={promoterId}&timeRangeType={timeRangeType}'.format(promoterId=promoterId, timeRangeType=i) rep = requests.get(url=url, headers=headers) table_name = 'kwai_promoter_fans_info' data = json.loads(rep.text)["data"] promoter_fans_item = {"fansAgeFeature": json.dumps(data["fansAgeFeature"]), "fansCityFeature": json.dumps(data["fansCityFeature"]), "fansCityLevelFeature": json.dumps(data["fansCityLevelFeature"]), "fansEquipBrandFeature": json.dumps(data["fansEquipBrandFeature"]), "fansEquipPriceFeature": json.dumps(data["fansEquipPriceFeature"]), "fansGenderFeature": json.dumps(data["fansGenderFeature"]), "fansNumTrend": json.dumps(data["fansNumTrend"]), "fansProvinceFeature": json.dumps(data["fansProvinceFeature"]), "promoteAvgPrice": json.dumps(data["promoteAvgPrice"]), "promoterId": promoterId, "timeRangeType": i} insert(table_name=table_name, item=promoter_fans_item) fans_type_date_list = [] fans_base_table = 'kwai_promoter_fans_base_info' fansProvinceFeatureList = tuple(data["fansProvinceFeature"]) for fansProvinceFeature in fansProvinceFeatureList: fansProvinceFeatureItem = {"promoterId": promoterId, "timeRangeType": i, "type": 'Province', "type_date": fansProvinceFeature["provinceFeature"], "FeatureRate": fansProvinceFeature["provinceFeatureRate"]} fans_type_date_list.append(fansProvinceFeatureItem) fansAgeFeatureList = tuple(data["fansAgeFeature"]) for fansAgeFeature in fansAgeFeatureList: fansAgeFeatureItem = {"promoterId": promoterId, "timeRangeType": i, "type": 'Age', "type_date": fansAgeFeature["ageFeature"], "FeatureRate": fansAgeFeature["ageFeatureRate"]} fans_type_date_list.append(fansAgeFeatureItem) fansCityFeatureList = tuple(data["fansCityFeature"]) for fansCityFeature in fansCityFeatureList: fansCityFeatureItem = {"promoterId": promoterId, "timeRangeType": i, "type": 'City', "type_date": fansCityFeature["cityFeature"], "FeatureRate": fansCityFeature["cityFeatureRate"]} fans_type_date_list.append(fansCityFeatureItem) fansCityLevelFeatureList = tuple(data["fansCityLevelFeature"]) for fansCityLevelFeature in fansCityLevelFeatureList: fansCityLevelFeatureItem = {"promoterId": promoterId, "timeRangeType": i, "type": 'CityLevel', "type_date": fansCityLevelFeature["cityLevelFeature"], "FeatureRate": fansCityLevelFeature["cityLevelRate"]} fans_type_date_list.append(fansCityLevelFeatureItem) fansEquipBrandFeatureList = tuple(data["fansEquipBrandFeature"]) for fansEquipBrandFeature in fansEquipBrandFeatureList: fansEquipBrandFeatureItem = {"promoterId": promoterId, "timeRangeType": i, "type": 'EquipBrand', "type_date": fansEquipBrandFeature["equipBrandTitleFeature"], "FeatureRate": fansEquipBrandFeature["equipBrandFeatureRate"]} fans_type_date_list.append(fansEquipBrandFeatureItem) fansEquipPriceFeatureList = tuple(data["fansEquipPriceFeature"]) for fansEquipPriceFeature in fansEquipPriceFeatureList: fansEquipPriceFeatureItem = {"promoterId": promoterId, "timeRangeType": i, "type": 'EquipPrice', "type_date": fansEquipPriceFeature["equipPriceFeature"], "FeatureRate": fansEquipPriceFeature["equipPriceFeatureRate"]} fans_type_date_list.append(fansEquipPriceFeatureItem) promoteAvgPriceList = tuple(data["promoteAvgPrice"]) for promoteAvgPrice in promoteAvgPriceList: promoteAvgPriceItem = {"promoterId": promoterId, "timeRangeType": i, "type": 'promoteAvgPrice', "type_date": promoteAvgPrice["avgPriceFeature"], "FeatureRate": promoteAvgPrice["avgPriceFeatureRate"]} fans_type_date_list.append(promoteAvgPriceItem) fansGenderFeatureList = tuple(data["fansGenderFeature"]) for fansGenderFeature in fansGenderFeatureList: fansGenderFeatureItem = {"promoterId": promoterId, "timeRangeType": i, "type": 'Gender', "type_date": fansGenderFeature["genderFeature"], "FeatureRate": fansGenderFeature["genderFeatureRate"]} fans_type_date_list.append(fansGenderFeatureItem) batch_insert(table_name=fans_base_table, item_list=fans_type_date_list) fans_trend_list = [] if i == 3: fans_trend_table = 'kwai_promoter_fans_trend_info' fansNumTrendList = tuple(data["fansNumTrend"]) for fansNumTrend in fansNumTrendList: fansNumTrendItem = {"promoterId": promoterId, "date": fansNumTrend["date"], "fansTotalAmount": fansNumTrend["fansTotalAmount"], "fansIncreaseAmount": fansNumTrend["fansIncreaseAmount"]} fans_trend_list.append(fansNumTrendItem) batch_insert(table_name=fans_trend_table, item_list=fans_trend_list) except Exception as e: SendFeiShuMsg.send_robot_msg('请求错误请检查cookie{e}'.format(e=e)) if __name__ == '__main__': PromoterFansInfo().PromoterFansInfoHandler(1424128656)