ad_ecpm_config_txt.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # coding=utf-8
  2. """ Ecpm 底价配置文件 """
  3. import io
  4. import ast
  5. from common.country_code_utils import CountryCodeUtils
  6. def _is_country_line(line):
  7. """ 判断一行是否是国家
  8. 用 逗号 分隔出来的个数进行判断
  9. :param line: string
  10. :return: bool
  11. """
  12. length = len(line.split(","))
  13. if length == 5 or length == 8:
  14. return False
  15. if line == "":
  16. return False
  17. return True
  18. def _is_price_line(line):
  19. """ 判断一行是否是价格行
  20. 用 逗号 分隔出来的个数进行判断
  21. :param line: string
  22. :return: bool
  23. """
  24. length = len(line.split(","))
  25. if length == 5 or length == 8:
  26. return True
  27. return False
  28. #
  29. # 格式解析文本文件 至 字典
  30. #
  31. #
  32. # 文本文件格式
  33. # ---------------------------
  34. # 日本
  35. # 38, 63, 127, 205, 337
  36. # 牙买加
  37. # 10, 18, 40, 67, 112
  38. # ---------------------------
  39. #
  40. # 生成字典 -> dic {
  41. # '日本': [38, 95, 205, 337], '牙买加': [10, 29, 67, 112]
  42. # }
  43. def _parse_country_price_text(config_txt_path, isBanner):
  44. """ 解析文本配置文件
  45. 如果是Banner的话,需要用平均数合并(2,3 | 3,4)
  46. 文本格式
  47. 日本
  48. 38, 63, 127, 205, 337
  49. 牙买加
  50. 10, 18, 40, 67, 112
  51. :param config_txt_path: string
  52. :param isBanner: bool 是否banner
  53. :return: dic key:国家名 value: ecpm(100*美金)
  54. """
  55. dic = {}
  56. with io.open(config_txt_path, "r") as f:
  57. lines = f.readlines()
  58. for i in range(0, len(lines)):
  59. line = lines[i]
  60. line = line.strip()
  61. line = line.encode(encoding='utf-8')
  62. if _is_country_line(line):
  63. # 正常读到一个国家
  64. country = line
  65. # 下面一行应该紧跟的是该国家的价格配置
  66. price_list_line = lines[i + 1]
  67. price_list_line = price_list_line.strip()
  68. if _is_price_line(price_list_line):
  69. price_list = ast.literal_eval("[{}]".format(price_list_line))
  70. if isBanner:
  71. # 特殊处理,第2个和第3个取平均数
  72. price1 = (price_list[1] + price_list[2]) / 2
  73. price2 = (price_list[2] + price_list[3]) / 2
  74. price_list.pop(1)
  75. price_list.pop(1)
  76. price_list.pop(1)
  77. price_list.insert(1, price1)
  78. price_list.insert(2, price2)
  79. dic[country] = price_list
  80. return dic
  81. class AdEcpmConfigTxt(object):
  82. """ Ecpm 底价配置文件
  83. country_name_dic: 国家名 配置内容
  84. country_code_dic: 国家码 配置内容
  85. country_name_arr: 国家名
  86. country_code_arr: 国家码
  87. country_codes_arr: 总国家总个数
  88. price_index_count: 价格总个数
  89. """
  90. def __init__(self):
  91. self.country_name_dic = {}
  92. self.country_code_dic = {}
  93. self.country_name_arr = []
  94. self.country_code_arr = []
  95. self.country_count = 0
  96. self.price_index_count = 0
  97. def read(self, path, is_banner=False):
  98. self.country_name_dic = _parse_country_price_text(path, is_banner)
  99. self.country_count = len(self.country_name_dic)
  100. for country_name in self.country_name_dic:
  101. price_arr = self.country_name_dic[country_name]
  102. # 价格是100倍的美金存储的,转一下
  103. price_arr = [p / 100.0 for p in price_arr]
  104. if not self.country_name_arr.__contains__(country_name):
  105. self.country_name_arr.append(country_name)
  106. country_code = CountryCodeUtils.get_country_code(country_name)
  107. if not self.country_code_arr.__contains__(country_code):
  108. self.country_code_arr.append(country_code)
  109. self.country_name_dic[country_name] = price_arr
  110. self.country_code_dic[country_code] = price_arr
  111. self.price_index_count = len(self.country_name_dic[self.country_name_arr[0]])
  112. def get_price(self, country_code, index):
  113. if not self.country_code_dic.has_key(country_code):
  114. return None
  115. if index < len(self.country_code_dic[country_code]):
  116. return self.country_code_dic[country_code][index]
  117. else:
  118. return None
  119. def get_country_name_index_arr(self):
  120. """ 按国家名重新组织数据
  121. 把price按index分成数组
  122. :return: arr [{'美国': 1.0}, {'美国': 2.0}]
  123. """
  124. arr = []
  125. for index in range(0, self.price_index_count):
  126. dic = {}
  127. for country_name in self.country_name_dic:
  128. price_arr = self.country_name_dic[country_name]
  129. dic[country_name] = price_arr[index]
  130. arr.append(dic)
  131. return arr
  132. if __name__ == '__main__':
  133. ad_worth_config_txt_path = "/Users/zhuge/Temp/20201222/FruitHeroes-点消-IOS_inter_summary.txt"
  134. ad_worth_config_txt = AdEcpmConfigTxt()
  135. ad_worth_config_txt.read(ad_worth_config_txt_path)
  136. print ad_worth_config_txt.country_name_arr
  137. print ad_worth_config_txt.country_code_arr
  138. print ad_worth_config_txt.get_price("US", 7)
  139. print "总国家数: {}".format(ad_worth_config_txt.country_count)
  140. print "总价格数: {}".format(ad_worth_config_txt.price_index_count)
  141. print "第一层价格: {}".format(ad_worth_config_txt.get_country_name_index_arr()[7]["美国"])