// // EcpmConfigInitExporter.m // UUTUtils2.1 // // Created by zhuge on 2020/7/14. // Copyright © 2020 dym. All rights reserved. // #import "EcpmConfigInitExporter.h" #import "AdIdNameInCSVUtils.h" #import "ProjectInfo.h" #import "ProjectInfo_Server_AdId.h" #import "NSString+NSString_CountryCode.h" #import "ZGJsonUtils.h" #define AdNameInCSV NSString #define AdKey NSString #define AdCountry NSString @interface EcpmConfigInitExporter() @property (strong) NSDictionary *dicAdKeys; @property (strong) NSDictionary *dicCountryCodes; @end @implementation EcpmConfigInitExporter - (NSString *) doExport:(ProjectInfo *)project admobFilePath:(NSString *)admobFilePath facebookFilePath:(NSString *)facebookFilePath { self.projectInfo = project; self.admobFilePath = admobFilePath; self.facebookFilePath = facebookFilePath; NSString *ret = @""; self.dicAdKeys = [self genAdKeyDic]; // 开始读取文件 NSMutableDictionary *md = [NSMutableDictionary dictionaryWithCapacity:0]; { // 读取admob NSError *err = nil; NSString *content = [NSString stringWithContentsOfFile:self.admobFilePath encoding:NSUTF8StringEncoding error:&err]; if (err) { NSLog(@"%@", err.localizedDescription); } NSArray *arrLines = [content componentsSeparatedByString:@"\n"]; for (NSString *line in arrLines) { if (line.length == 0) { continue; } if ([line hasPrefix:@"国家/地区"]) { continue; } if ([line containsString:@"未知地区"]) { continue; } EcpmCountryAdInfo *cai = [[EcpmCountryAdInfo alloc] initWithAdmobLine:line]; if (cai.ecpm < 0.0001) { // 精度太少,会被忽略掉 continue; } EcpmCountryInfo *countryInfo = md[cai.country]; if (!countryInfo) { countryInfo = [[EcpmCountryInfo alloc] init]; countryInfo.country = cai.country; md[cai.country] = countryInfo; } [countryInfo addCountryAdInfo:cai]; } } { // 读取facebook NSError *err = nil; NSString *content = [NSString stringWithContentsOfFile:self.facebookFilePath encoding:NSUTF8StringEncoding error:&err]; if (err) { NSLog(@"%@", err.localizedDescription); } NSArray *arrLines = [content componentsSeparatedByString:@"\n"]; for (NSString *line in arrLines) { if (line.length == 0) { continue; } if ([line hasPrefix:@"国家"]) { continue; } EcpmCountryAdInfo *cai = [[EcpmCountryAdInfo alloc] initWithFacebookLine:line]; if (cai.ecpm < 0.0001) { // 精度太少,会被忽略掉 continue; } EcpmCountryInfo *countryInfo = md[cai.country]; if (!countryInfo) { countryInfo = [[EcpmCountryInfo alloc] init]; countryInfo.country = cai.country; md[cai.country] = countryInfo; } [countryInfo addCountryAdInfo:cai]; } } // 按ecpm排序国家 NSMutableArray *maCountry = [NSMutableArray arrayWithCapacity:0]; for (NSString *country in md) { [maCountry addObject:md[country]]; } // 排序国家 [maCountry sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { EcpmCountryInfo *c1 = obj1; EcpmCountryInfo *c2 = obj2; return [c1 avgEcpm] < [c2 avgEcpm]; }]; // 输出 NSMutableDictionary*> *mdOut = [NSMutableDictionary dictionaryWithCapacity:0]; for (int i = 0; i < maCountry.count; i++) { EcpmCountryInfo *c = maCountry[i]; NSMutableDictionary *mdCountry = [NSMutableDictionary dictionaryWithCapacity:0]; for (EcpmCountryAdInfo *l in c.maCountryAdInfos) { NSString *adKey = self.dicAdKeys[l.adName]; if (adKey == nil) { NSLog(@"ad key not found: %@", l.adName); continue; } NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%0.4f", l.ecpm]]; mdCountry[adKey] = dn; } mdOut[c.country] = mdCountry; } NSDictionary *d = @{ @"countries": mdOut }; ret = [ZGJsonUtils dictionaryToString:d]; return ret; } - (NSDictionary *) genAdKeyDic { NSMutableDictionary *md = [NSMutableDictionary dictionaryWithCapacity:0]; for (ProjectInfo_Server_AdId *adid in self.projectInfo.server.xibAdIdArray) { NSString *adKey = adid.idIndex; NSString *adName = [[AdIdNameInCSVUtils shared] adNameForAdIdIndex:adid.idIndex]; if (adName != nil) { md[adName] = adKey; } } return md; } @end #pragma mark - Admob国家版本信息 @implementation EcpmCountryAdInfo - (instancetype)initWithAdmobLine:(NSString *)line { NSArray *arr = [line componentsSeparatedByString:@","]; NSAssert(arr.count == 3, line); self.type = ADMOB; self.country = arr[0]; self.adName = arr[1]; self.ecpm = [arr[2] floatValue]; NSString *s = [NSString getFBCountryCode:self.country]; if (!s || [s isEqualToString:@""] || [s isEqualToString:self.country]) { NSAssert(NO, @"无匹配国家: %@", s); } self.country = s; return self; } - (instancetype)initWithFacebookLine:(NSString *)line { NSArray *arr = [line componentsSeparatedByString:@","]; NSAssert(arr.count == 3, line); self.type = FACEBOOK; self.country = arr[0]; self.adName = arr[1]; self.ecpm = [arr[2] floatValue]; return self; } @end @implementation EcpmCountryInfo - (instancetype)init { self = [super init]; if (self) { self.maCountryAdInfos = [NSMutableArray arrayWithCapacity:0]; } return self; } - (void)addCountryAdInfo:(EcpmCountryAdInfo *)countryAdInfo { for (EcpmCountryAdInfo *cai in self.maCountryAdInfos) { NSAssert(!(cai.type == countryAdInfo.type && [cai.adName isEqualToString:countryAdInfo.adName]) , @"有问题:广告ID有重复: %@, %@", cai.adName, countryAdInfo.adName); } [self.maCountryAdInfos addObject:countryAdInfo]; } - (float)avgEcpm { int len = (int)self.maCountryAdInfos.count; if (len > 0) { float total = 0; for (EcpmCountryAdInfo *cai in self.maCountryAdInfos) { total += cai.ecpm; } return total / len; } return 0; } @end