commonFunc.py 5.4 KB

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