commonFunc.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. from sqlalchemy import create_engine
  2. from urllib import parse
  3. import pandas as pd
  4. import yaml
  5. with open('config/config.yaml', mode='r', encoding='utf-8') as f:
  6. config = yaml.load(f.read(), Loader=yaml.FullLoader)
  7. def update_dict(x, y):
  8. # 用字典y,来更新x
  9. # key 存在则更新,不存在则新增
  10. x.update(y)
  11. return x
  12. def get_db_engine(db_info):
  13. db_con_str = 'mysql+pymysql://%s:%s@%s:%d/%s' % \
  14. (db_info['username'],
  15. parse.quote_plus(db_info['password']),
  16. db_info['host'],
  17. db_info['port'],
  18. db_info['database'])
  19. engine = create_engine(db_con_str, connect_args={'charset': 'utf8'})
  20. return engine
  21. def is_contains_region(val1, val2):
  22. """
  23. level1
  24. region long[] 可选 地域 传值为[]表示不限;传递上一级ID时,childrenID可以不传;不允许同时传parentID和childrenID;
  25. 仅计划的campaign_type为5时,支持设置三级地域(例:山西-大同-左云,左云是三级地域)
  26. :param val1: 推荐的城市等级组合,如 "一线城市|新一线城市|二线城市|三线城市" , "不限"
  27. :param val2: 从账户配置信息表里读取的地区信息
  28. :return:
  29. """
  30. if val1 == '不限' or val1 is None:
  31. if not val2:
  32. return True, {'region': None}
  33. else:
  34. return False, -1
  35. else:
  36. val = []
  37. sql = """
  38. select t1.city_name, t2.level, t2.region_id, t2.parent from ctop_kuaishou_city_level t1
  39. left join ctop_kuaishou_region_list_parent t2
  40. on t1.city_name = t2.name
  41. where t1.city_level in %s
  42. """ % (tuple(val1.split('|')),)
  43. city_df = pd.read_sql(sql, None)
  44. # level1
  45. if city_df[city_df.level == 1].region_id.values in eval(val2):
  46. val.extend(city_df[city_df.level == 1].region_id.values)
  47. else:
  48. return False, -1
  49. # level2
  50. # 元素的父级是否存在于 val2,如果存在则为包含于的关系
  51. # 元素本身是否存在于 val2 ,如果存在则为包含于的关系
  52. # 以上都不满足,val1 不包含于 val2
  53. for item in city_df[city_df.level == 2].region_id.values:
  54. parent = city_df[city_df.region_id == item].parent.values[0]
  55. if parent in eval(val2):
  56. val.append(item)
  57. elif item in eval(val2):
  58. val.append(item)
  59. else:
  60. return False, -1
  61. # level3
  62. for item in city_df[city_df.level == 3].region_id.values:
  63. parent = city_df[city_df.region_id == item].parent.values[0]
  64. if parent in eval(val2):
  65. val.append(item)
  66. elif item in eval(val2):
  67. val.append(item)
  68. else:
  69. return False, -1
  70. return True, {'region': val}
  71. def is_contains_gender(val1, val2):
  72. """
  73. :param val1: ctop_ai_kuaishou_signature_recommended_target_combine 中的 gender 字段: '男'、'女'、'不限'、None
  74. :param val2: ctop_ai_kuaishou_advertiser_strategy 中的 gender 字段: 1:女性, 2:男性,0表示不限
  75. :return: val1 是否为 val2的子集,以及传递给快手后台的值
  76. """
  77. is_contains = False
  78. val = None
  79. if val1 == '男' and val2 in (2, 0):
  80. is_contains = True
  81. val = val1
  82. if val1 == '女' and val2 in (1, 0):
  83. is_contains = True
  84. val = val1
  85. if val1 == '不限' and val2 == 0:
  86. is_contains = True
  87. val = val2
  88. # 如果val1为空,表示该字段没有参与定向组合运算,就直接取客户投放策略里面的值
  89. if val1 is None:
  90. is_contains = True
  91. val = val2
  92. return is_contains, {'gender': val}
  93. def is_contains_platform_os(val1, val2):
  94. """
  95. :param val1: ctop_ai_kuaishou_signature_recommended_target_combine 中的 client 字段: '男'、'女'、'不限'、None
  96. :param val2:
  97. :return:
  98. """
  99. pass
  100. def is_contains_age(val1, val2_min, val2_max, val2_range):
  101. """
  102. :param val1: 推荐的年龄等级组合,如 "31-40岁|41-49岁|50+岁" , "不限"
  103. age struct 可选 自定义年龄段 不传值表示不限,传值具体见下方表格;与ages_range不能同时传
  104. min int 必填 年龄最小限制 年龄区间最小为18岁
  105. max int 必填 年龄最大限制 年龄区间最大为55岁,且年龄最大限制须大于等于年龄最小限制
  106. :param val2_min: age_min
  107. :param val2_max: age_max
  108. :param val2_range: ctop_ai_kuaishou_advertiser_strategy 中的 ages_range [] varchar
  109. 与age不能同时传;【18:表示18-23岁】;【24:表示24-30岁】;【31:表示31-40岁】;【41:表示41-49岁】;【50:表示50-100岁】
  110. :return:
  111. """
  112. age_dict = {'18-23岁': 18, '24-30岁': 24, '31-40岁': 31, '41-49岁': 41, '50-100岁': 50}
  113. if val1 == '不限' or ((not val2_min) and (not val2_max) and (not val2_range)):
  114. return True, {'ages_range': None}
  115. else:
  116. age1 = [age_dict[item] for item in val1.split('|')]
  117. if (val2_min is None and val2_max is None) and val2_range:
  118. if age1 in eval(val2_range):
  119. return True, {'ages_range': age1}
  120. else:
  121. return False, -1
  122. if (val2_min and val2_max) and (not val2_range):
  123. if age1[0] >= val2_min and age1[-1] <= val2_max:
  124. return True, {'age_min': age1[0], 'age_max': age1[-1]}
  125. else:
  126. return False, -1
  127. def is_contains_business_interest(val1, val2):
  128. pass