AdWorthJsonGenerator.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // AdWorthJsonGenerator.m
  3. // UUTUtils2.1
  4. //
  5. // Created by zhuge on 2020/9/2.
  6. // Copyright © 2020 dym. All rights reserved.
  7. //
  8. #import "AdWorthJsonGenerator.h"
  9. #import "ProjectInfo.h"
  10. #import "DataUtils.h"
  11. #import "ZGWebUtils.h"
  12. #import "ZGJsonUtils.h"
  13. #import "ProjectInfo_Server_AdId.h"
  14. @interface AdWorthJsonGenerator()
  15. @property (strong) NSString *facebookAdIdPrefix;
  16. @end
  17. @implementation AdWorthJsonGenerator
  18. - (void)start {
  19. // 初始化
  20. [self initWithProjectInfo];
  21. // 获取ECPM
  22. NSDictionary *ecpmDic = [self getEcpm];
  23. // 生成配置文件
  24. [self genJson:ecpmDic];
  25. }
  26. - (void) initWithProjectInfo {
  27. [self.projectInfo loadServerData];
  28. [self.projectInfo.server initWithDic:nil];
  29. // 初始化Facebook前缀名
  30. self.facebookAdIdPrefix = @"";
  31. for (ProjectInfo_Server_AdId * adId in self.projectInfo.server.xibAdIdArray) {
  32. if ([adId isFacebook]) {
  33. self.facebookAdIdPrefix = [[adId.adId componentsSeparatedByString:@"_"] firstObject];
  34. break;
  35. }
  36. }
  37. }
  38. /**
  39. 导出文件格式为:
  40. {
  41. {
  42. "countries": {
  43. "VI": {
  44. "802": 200.98,
  45. "501": 0.02,
  46. "701": 0.6,
  47. "702": 0.8
  48. },
  49. }
  50. */
  51. - (void) genJson:(NSDictionary *)ecpmDic {
  52. NSMutableDictionary *mdCountries = [NSMutableDictionary dictionaryWithCapacity:0];
  53. NSMutableDictionary *mdInvalidAdIds = [NSMutableDictionary dictionaryWithCapacity:0];
  54. for (NSString *country in ecpmDic) {
  55. for (NSString *adId in ecpmDic[country]) {
  56. // 查找验证无效的广告ID
  57. NSString *adKey = [self indexKeyForAdId:adId];
  58. if (!adKey) {
  59. mdInvalidAdIds[adId] = adId;
  60. // continue;
  61. }
  62. // 生成配置文件
  63. float value = [ecpmDic[country][adId] floatValue];
  64. NSMutableDictionary *mdPrices = mdCountries[country];
  65. if (mdPrices == nil) {
  66. mdPrices = [NSMutableDictionary dictionaryWithCapacity:0];
  67. mdCountries[country] = mdPrices;
  68. }
  69. mdPrices[adId] = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.4f", value]];
  70. }
  71. }
  72. NSDictionary *dic = @{
  73. @"countries": mdCountries,
  74. };
  75. NSString *json = [ZGJsonUtils dictionaryToString:dic];
  76. // [json writeToFile:@"/Users/zhuge/Temp/20200902/ad_worth.json" atomically:YES encoding:NSUTF8StringEncoding error:nil];
  77. [json writeToFile:self.projectInfo.path.adWorthJsonPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  78. NSLog(@"无效广告Id: %@", mdInvalidAdIds.allKeys);
  79. }
  80. - (NSString *)GetFullAdId:(NSString *)ad_id {
  81. if ([ad_id hasPrefix:@"cap-"]) {
  82. // admob
  83. return ad_id;
  84. }
  85. if (ad_id.length < 5) {
  86. // bulldog
  87. return ad_id;
  88. }
  89. return [NSString stringWithFormat:@"%@-%@", self.facebookAdIdPrefix, ad_id];
  90. }
  91. /**
  92. ca-app-pub-1903708571143046\/1040889471_AD --> 201
  93. */
  94. - (NSString*) indexKeyForAdId:(NSString*)key {
  95. for (ProjectInfo_Server_AdId * adId in self.projectInfo.server.xibAdIdArray) {
  96. if ([adId.adId isEqual:key]) {
  97. return adId.idIndex;
  98. } else if ([adId.adId isEqualToString:[NSString stringWithFormat:@"%@_%@", self.facebookAdIdPrefix, key]]) {
  99. return adId.idIndex;
  100. }
  101. }
  102. return nil;
  103. }
  104. /* 从Json中读取
  105. 注意Facebook的ID木有前缀
  106. {
  107. "ca-app-pub-1903708571143046\/1040889471_AD":0,
  108. "ca-app-pub-1903708571143046\/2938819723_AD":0,
  109. "129348u3498132:0,
  110. }
  111. 组织成字典: {国家: {版位: ecpm},}
  112. */
  113. - (NSDictionary *) getEcpm {
  114. NSString *response = @"";
  115. BOOL testReadFromFile = NO;
  116. if (testReadFromFile) {
  117. response = [NSString stringWithContentsOfFile:@"/Users/zhuge/Temp/20200902/test.json" encoding:NSUTF8StringEncoding error:nil];
  118. } else {
  119. NSString *url = [NSString stringWithFormat:@"http://usertrack.appcpi.net/jinxihua/ec.php?packagename=%@", self.projectInfo.package];
  120. response = [ZGWebUtils stringOfURL:url];
  121. NSLog(@"URL:%@", url);
  122. NSLog(@"URL返回: %@", response);
  123. }
  124. NSDictionary *dic = [ZGJsonUtils stringToObject:response];
  125. NSMutableDictionary *md = [NSMutableDictionary dictionaryWithCapacity:0];
  126. for (NSString *key in dic) {
  127. float value = [dic[key] floatValue];
  128. if (value > 0) {
  129. NSArray *arr = [key componentsSeparatedByString:@"_"];
  130. NSString *adId = arr[0];
  131. NSString *country = arr[1];
  132. NSMutableDictionary *mdCountry = md[country];
  133. if (mdCountry == nil) {
  134. mdCountry = [NSMutableDictionary dictionaryWithCapacity:0];
  135. md[country] = mdCountry;
  136. }
  137. mdCountry[adId] = @(value);
  138. }
  139. }
  140. return md;
  141. }
  142. @end