ad_worth_6.1.4.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # coding=utf-8
  2. import os
  3. import json
  4. import sys
  5. import urllib2
  6. def GetFromWeb(package_name, tmp_response_txt_path):
  7. if False:
  8. with open(tmp_response_txt_path) as f:
  9. return json.load(f)
  10. url = "http://usertrack.appcpi.net/jinxihua/ec.php?packagename={}".format(package_name)
  11. print "请求网址: {}".format(url)
  12. print "请求线上ecpm中..."
  13. res_data = urllib2.urlopen(url)
  14. ret = res_data.read()
  15. if ret == "":
  16. sys.stderr.write("未能获取到数据!请检查包名、网络情况、是否在ROI系统内、是否是新上线的项目")
  17. return {}
  18. with open(tmp_response_txt_path, "w") as f:
  19. f.write(ret)
  20. print "请求结束,返回数据已写入:{}".format(tmp_response_txt_path)
  21. with open(tmp_response_txt_path) as ff:
  22. ad_config_json = json.load(ff)
  23. return ad_config_json
  24. def ParseAdConfig(ad_config_json):
  25. dic_counties = {}
  26. for ad_info in ad_config_json:
  27. ecpm = ad_config_json[ad_info]
  28. if ecpm > 0:
  29. arr = ad_info.split("_")
  30. ad_id = arr[0]
  31. country = arr[1]
  32. dic_country = {}
  33. if dic_counties.has_key(country):
  34. dic_country = dic_counties[country]
  35. else:
  36. dic_counties[country] = dic_country
  37. dic_country[ad_id] = ecpm
  38. return dic_counties
  39. def PrintConfigInfo(ad_worth):
  40. admobs = []
  41. facebooks = []
  42. for country in ad_worth:
  43. for ad_id in ad_worth[country]:
  44. if ad_id.startswith("ca-app"):
  45. if not admobs.__contains__(ad_id):
  46. admobs.append(ad_id)
  47. else:
  48. if not facebooks.__contains__(ad_id):
  49. facebooks.append(ad_id)
  50. print "共获取到Admob广告ID {} 个".format(len(admobs))
  51. for ad_id in admobs:
  52. print "\t{}".format(ad_id)
  53. print "共获取到Facebook广告ID {} 个".format(len(facebooks))
  54. for ad_id in facebooks:
  55. print "\t{}".format(ad_id)
  56. print "共获取到国家 {} 个".format(len(ad_worth))
  57. def main():
  58. package_name = sys.argv[1]
  59. current_dir = os.path.dirname(__file__)
  60. tmp_response_txt_path = os.path.join(current_dir, "response.txt")
  61. ad_config_json = GetFromWeb(package_name, tmp_response_txt_path)
  62. if len(ad_config_json) == 0:
  63. return
  64. ad_worth = ParseAdConfig(ad_config_json)
  65. PrintConfigInfo(ad_worth)
  66. ad_worth = {
  67. "countries": ad_worth
  68. }
  69. write_to_path = os.path.join(current_dir, "ad_worth.json")
  70. with open(write_to_path, "w") as ff:
  71. json.dump(ad_worth, ff)
  72. print "ad_worth.json生成完成: {}".format(write_to_path)
  73. if len(sys.argv) < 2:
  74. sys.stderr.write("输入有误")
  75. print "使用方法:python ad_worth_6.1.4.py com.your.package"
  76. print "注:iOS需要传入字符串的bundle id,而非数字版的app id!!!"
  77. else:
  78. main()