# coding=utf-8 import os from sdk.tool.ad_ecpm_config_txt import AdEcpmConfigTxt _AdmobCountryRenameDic = { "圣文森特岛": "圣文森特和格林纳丁斯", "香港特别行政区": "香港", "安道尔共和国": "安道尔", "科特迪瓦共和国": "科特迪瓦", "吉尔吉斯坦": "吉尔吉斯斯坦", "蒙特塞拉特岛": "蒙特塞拉特", "百慕大群岛": "百慕大", "安圭拉岛": "安圭拉", } _AdmobSkipCountryList = { "刚果", "朝鲜", "古巴", "南斯拉夫", "扎伊尔", } _FacebookCountryRenameDic = { "圣文森特岛": "圣文森特和格林纳丁斯", "吉尔吉斯坦": "吉尔吉斯斯坦", "阿拉伯联合酋长国": "阿联酋", "香港特别行政区": "中国香港", "捷克": "捷克共和国", "百慕大群岛": "百慕大", "安道尔共和国": "安道尔", "刚果": "刚果共和国", "蒙特塞拉特岛": "蒙塞拉特岛", "安圭拉岛": "安圭拉", } class GAME_STORE: GOOGLE_PLAY, IOS = range(2) @staticmethod def global_price_mul(game_store): return { GAME_STORE.GOOGLE_PLAY: 1, GAME_STORE.IOS: 1.7 }[game_store] @staticmethod def platform_from_string(game_store_str): return { "android": GAME_STORE.GOOGLE_PLAY, "ios": GAME_STORE.IOS }[game_store_str] class SCRIPT_PLATFORM(object): ADMOB, FACEBOOK = range(2) @staticmethod def script_template_name(type): return { SCRIPT_PLATFORM.ADMOB: "Template_Android_Admob_Banner.js", SCRIPT_PLATFORM.FACEBOOK: "Template_Android_FB_Interstitial.js", }[type] @staticmethod def all_platforms(): return [SCRIPT_PLATFORM.ADMOB, SCRIPT_PLATFORM.FACEBOOK] class SCRIPT_AD_TYPE(object): BANNER, INTERSTITIAL = range(2) @staticmethod def global_base_price(type, index): return { SCRIPT_AD_TYPE.BANNER: [0.3, 0.6, 1, 1.6], SCRIPT_AD_TYPE.INTERSTITIAL: [1.05, 2.1, 2.8, 3.5, 5.6, 7, 8.4, 11.2] }[type][index] @staticmethod def script_file_name(type, index): """ 写入的文件名 :param type: SCRIPT_AD_TYPE :param index: int :return: string """ return { SCRIPT_AD_TYPE.BANNER:[ "AM_B1_lv2.js", "AM_B2_lv1.js", "AM_B2_lv2.js", "AM_B2_lv3.js" ], SCRIPT_AD_TYPE.INTERSTITIAL: [ "AM_I1_lv2.js", "FB_I1_lv1.js", "AM_I2_lv1.js", "FB_I1_lv2.js", "AM_I2_lv2.js", "FB_I2_lv1.js", "AM_I2_lv3.js", "FB_I2_lv2.js" ] }[type][index] @staticmethod def script_file_platform(type, index): return { SCRIPT_AD_TYPE.BANNER:[ SCRIPT_PLATFORM.ADMOB, SCRIPT_PLATFORM.ADMOB, SCRIPT_PLATFORM.ADMOB, SCRIPT_PLATFORM.ADMOB ], SCRIPT_AD_TYPE.INTERSTITIAL: [ SCRIPT_PLATFORM.ADMOB, SCRIPT_PLATFORM.FACEBOOK, SCRIPT_PLATFORM.ADMOB, SCRIPT_PLATFORM.FACEBOOK, SCRIPT_PLATFORM.ADMOB, SCRIPT_PLATFORM.FACEBOOK, SCRIPT_PLATFORM.ADMOB, SCRIPT_PLATFORM.FACEBOOK ] }[type][index] def _gen_script(script_platform, global_price, country_lines, write_to_file): """ 生成脚本配置文件 :param script_platform: _SCRIPT_PLATFORM :param global_price: number :param country_lines: string 国家array字符口中 :param write_to_file: string 写入文件 """ template_file_name = SCRIPT_PLATFORM.script_template_name(script_platform) current_path = os.path.dirname(os.path.realpath(__file__)) # 当前文件路径 template_file_path = os.path.join(current_path, "script_templates", template_file_name) with open(template_file_path) as f: template_str = f.read() template_str = template_str.replace("$ZG_country_configs_ZG$", country_lines) template_str = template_str.replace("$ZG_global_price_ZG$", "{:.2f}".format(global_price)) with open(write_to_file, "w") as ff: ff.write(template_str) def _gen_script_array_string(price_country_dic): """ 生成脚本价格配置字符串 :param price_country_dic: {2.13: ['美国', '法国']} :return: string arr["2.13"] = Array("美国", "法国"); """ ret_str = "" for price in price_country_dic: arr_country = price_country_dic[price] country_str = '","'.join(arr_country) country_str = '"{}"'.format(country_str) ret_str = '{}\tarr["{:.2f}"] = Array({});\n'.format(ret_str, price, country_str) return ret_str def _replace_country_names(country_name_dic, script_platform): """按需替换国家名字 脚本中的国家名字,每个平台都有不一样的名字 :param country_name_dic: {'美国':2.13, '日本':2.13, '法国':1.23} :param script_platform: _SCRIPT_PLATFORM :return: """ if script_platform == SCRIPT_PLATFORM.ADMOB: for country_name in _AdmobCountryRenameDic: if country_name_dic.has_key(country_name): price = country_name_dic[country_name] country_name_dic.__delitem__(country_name) country_name_dic[_AdmobCountryRenameDic[country_name]] = price for country_name in _AdmobSkipCountryList: if country_name_dic.has_key(country_name): country_name_dic.__delitem__(country_name) if script_platform == SCRIPT_PLATFORM.FACEBOOK: for country_name in _FacebookCountryRenameDic: if country_name_dic.has_key(country_name): price = country_name_dic[country_name] country_name_dic.__delitem__(country_name) country_name_dic[_FacebookCountryRenameDic[country_name]] = price return country_name_dic def _country_dic_2_price_dic(country_name_dic): """ 国家名字典 转 价格字典 :param country_name_dic: {'美国':2.13, '日本':2.13, '法国':1.23} :return: {2.13: ['美国', '日本'], 1.23: ['法国']} """ price_dic = {} for country_name in country_name_dic: price = country_name_dic[country_name] price_country_name_arr = [] if price_dic.has_key(price): price_country_name_arr = price_dic[price] price_country_name_arr.append(country_name) price_dic[price] = price_country_name_arr return price_dic class PriceScriptMaker: """ 脚本生成 参数列表: """ def __init__(self, game_store): self.game_store = game_store self.banner_txt = AdEcpmConfigTxt() self.inter_txt = AdEcpmConfigTxt() def read(self, banner_txt_path, inter_txt_path): if banner_txt_path is None: self.banner_txt = None else: self.banner_txt.read(banner_txt_path, is_banner=True) if inter_txt_path is None: self.inter_txt = None else: self.inter_txt.read(inter_txt_path) def _gen_script(self, script_ad_type, txt, write_to_dir): game_store_mul = GAME_STORE.global_price_mul(self.game_store) country_name_index_arr = txt.get_country_name_index_arr() for index in range(0, len(country_name_index_arr)): # 每个序号对应的 platform 都是不一样的 script_platform = SCRIPT_AD_TYPE.script_file_platform(script_ad_type, index) # 国家名字根据平台需要重新替换 country_name_price_dic = country_name_index_arr[index] country_name_price_dic = _replace_country_names(country_name_price_dic, script_platform) # 按平台重新计算全球底价 base_global_price = SCRIPT_AD_TYPE.global_base_price(script_ad_type, index) base_global_price *= game_store_mul # 按平台需要重新计算价格 # for country_name in country_name_price_dic: # price = country_name_price_dic[country_name] # country_name_price_dic[country_name] = price * game_store_mul # 转换价格 price_dic = _country_dic_2_price_dic(country_name_price_dic) # 生成脚本需要的代码 script_array_string = _gen_script_array_string(price_dic) # 生成脚本并写入文件 write_to_path = os.path.join(write_to_dir, SCRIPT_AD_TYPE.script_file_name(script_ad_type, index)) _gen_script(script_platform, base_global_price, script_array_string, write_to_path) def gen_scripts(self, write_to_dir): if self.banner_txt is not None: self._gen_script(SCRIPT_AD_TYPE.BANNER, self.banner_txt, write_to_dir) if self.inter_txt is not None: self._gen_script(SCRIPT_AD_TYPE.INTERSTITIAL, self.inter_txt, write_to_dir) if __name__ == '__main__': write_to_dir = "/Users/zhuge/Temp/20201224/test/scripts" # _gen_script(SCRIPT_PLATFORM.ADMOB, 2.13, "wo shi yi hang", "/Users/zhuge/Temp/20201224/test/scripts/test.js") game_store = GAME_STORE.IOS maker = PriceScriptMaker(game_store) maker.read("/Users/zhuge/Temp/20201224/test/FunnyFruitSplash-连消-Android_banner_summary.txt", "/Users/zhuge/Temp/20201224/test/wood_inter_20200813_22_summary.txt") maker.gen_scripts(write_to_dir) # print maker._genScript(7, "admob", 1, 1.8, "/Users/zhuge/Temp/20201224/wood_3d_1010/t.js") # maker.genScriptFiles_interstitial(GAME_STORE.GOOGLE_PLAY, "/Users/zhuge/Temp/20201224/wood_3d_1010/scripts")