// // AdWorthJsonGenerator.m // UUTUtils2.1 // // Created by zhuge on 2020/9/2. // Copyright © 2020 dym. All rights reserved. // #import "AdWorthJsonGenerator.h" #import "ProjectInfo.h" #import "DataUtils.h" #import "ZGWebUtils.h" #import "ZGJsonUtils.h" #import "ProjectInfo_Server_AdId.h" @interface AdWorthJsonGenerator() @property (strong) NSString *facebookAdIdPrefix; @end @implementation AdWorthJsonGenerator - (void)start { // 初始化 [self initWithProjectInfo]; // 获取ECPM NSDictionary *ecpmDic = [self getEcpm]; // 生成配置文件 [self genJson:ecpmDic]; } - (void) initWithProjectInfo { [self.projectInfo loadServerData]; [self.projectInfo.server initWithDic:nil]; // 初始化Facebook前缀名 self.facebookAdIdPrefix = @""; for (ProjectInfo_Server_AdId * adId in self.projectInfo.server.xibAdIdArray) { if ([adId isFacebook]) { self.facebookAdIdPrefix = [[adId.adId componentsSeparatedByString:@"_"] firstObject]; break; } } } /** 导出文件格式为: { { "countries": { "VI": { "802": 200.98, "501": 0.02, "701": 0.6, "702": 0.8 }, } */ - (void) genJson:(NSDictionary *)ecpmDic { NSMutableDictionary *mdCountries = [NSMutableDictionary dictionaryWithCapacity:0]; NSMutableDictionary *mdInvalidAdIds = [NSMutableDictionary dictionaryWithCapacity:0]; for (NSString *country in ecpmDic) { for (NSString *adId in ecpmDic[country]) { // 查找验证无效的广告ID NSString *adKey = [self indexKeyForAdId:adId]; if (!adKey) { mdInvalidAdIds[adId] = adId; // continue; } // 生成配置文件 float value = [ecpmDic[country][adId] floatValue]; NSMutableDictionary *mdPrices = mdCountries[country]; if (mdPrices == nil) { mdPrices = [NSMutableDictionary dictionaryWithCapacity:0]; mdCountries[country] = mdPrices; } mdPrices[adId] = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.4f", value]]; } } NSDictionary *dic = @{ @"countries": mdCountries, }; NSString *json = [ZGJsonUtils dictionaryToString:dic]; // [json writeToFile:@"/Users/zhuge/Temp/20200902/ad_worth.json" atomically:YES encoding:NSUTF8StringEncoding error:nil]; [json writeToFile:self.projectInfo.path.adWorthJsonPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSLog(@"无效广告Id: %@", mdInvalidAdIds.allKeys); } - (NSString *)GetFullAdId:(NSString *)ad_id { if ([ad_id hasPrefix:@"cap-"]) { // admob return ad_id; } if (ad_id.length < 5) { // bulldog return ad_id; } return [NSString stringWithFormat:@"%@-%@", self.facebookAdIdPrefix, ad_id]; } /** ca-app-pub-1903708571143046\/1040889471_AD --> 201 */ - (NSString*) indexKeyForAdId:(NSString*)key { for (ProjectInfo_Server_AdId * adId in self.projectInfo.server.xibAdIdArray) { if ([adId.adId isEqual:key]) { return adId.idIndex; } else if ([adId.adId isEqualToString:[NSString stringWithFormat:@"%@_%@", self.facebookAdIdPrefix, key]]) { return adId.idIndex; } } return nil; } /* 从Json中读取 注意Facebook的ID木有前缀 { "ca-app-pub-1903708571143046\/1040889471_AD":0, "ca-app-pub-1903708571143046\/2938819723_AD":0, "129348u3498132:0, } 组织成字典: {国家: {版位: ecpm},} */ - (NSDictionary *) getEcpm { NSString *response = @""; BOOL testReadFromFile = NO; if (testReadFromFile) { response = [NSString stringWithContentsOfFile:@"/Users/zhuge/Temp/20200902/test.json" encoding:NSUTF8StringEncoding error:nil]; } else { NSString *url = [NSString stringWithFormat:@"http://usertrack.appcpi.net/jinxihua/ec.php?packagename=%@", self.projectInfo.package]; response = [ZGWebUtils stringOfURL:url]; NSLog(@"URL:%@", url); NSLog(@"URL返回: %@", response); } NSDictionary *dic = [ZGJsonUtils stringToObject:response]; NSMutableDictionary *md = [NSMutableDictionary dictionaryWithCapacity:0]; for (NSString *key in dic) { float value = [dic[key] floatValue]; if (value > 0) { NSArray *arr = [key componentsSeparatedByString:@"_"]; NSString *adId = arr[0]; NSString *country = arr[1]; NSMutableDictionary *mdCountry = md[country]; if (mdCountry == nil) { mdCountry = [NSMutableDictionary dictionaryWithCapacity:0]; md[country] = mdCountry; } mdCountry[adId] = @(value); } } return md; } @end