Test.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package cn.com.ctop.toutiao.test;
  2. import cn.com.ctop.common.module.utils.HttpUtils;
  3. import cn.com.ctop.crawler.modules.core.util.FateadmHttpUtil;
  4. import cn.com.ctop.crawler.modules.core.util.FateadmUtil;
  5. import com.fasterxml.jackson.databind.DeserializationFeature;
  6. import com.fasterxml.jackson.databind.JsonNode;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import org.apache.http.impl.cookie.BasicClientCookie;
  9. import org.openqa.selenium.Cookie;
  10. import org.openqa.selenium.WebDriver;
  11. import org.openqa.selenium.chrome.ChromeDriver;
  12. import org.openqa.selenium.chrome.ChromeOptions;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import java.text.SimpleDateFormat;
  15. import java.util.*;
  16. public class Test {
  17. @Value("${jeecg.path.chrome-driver}")
  18. private String chromeDriver;
  19. public void test(String url) {
  20. System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
  21. try {
  22. ChromeOptions chromeOptions = new ChromeOptions();
  23. WebDriver webDriver = new ChromeDriver(chromeOptions);
  24. chromeOptions.addArguments("--headless");
  25. chromeOptions.addArguments("--incognito");
  26. chromeOptions.addArguments("--no-sandbox");
  27. chromeOptions.addArguments("--disable-gpu");
  28. chromeOptions.addArguments("--window-size=1290,1080");
  29. chromeOptions.addArguments("--user-agent=" + HttpUtils.USER_AGENT);
  30. chromeOptions.setAcceptInsecureCerts(true);
  31. //获取登录页面
  32. webDriver.get(url);
  33. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  34. for (Cookie cookie : webDriver.manage().getCookies()) {
  35. BasicClientCookie ck = new BasicClientCookie(cookie.getName(), cookie.getValue());
  36. ck.setDomain("ad.oceanengine.com");
  37. ck.setExpiryDate(cookie.getExpiry());
  38. ck.setPath(cookie.getPath());
  39. HttpUtils.COOKIESTORE.addCookie(ck);
  40. }
  41. String html = webDriver.getPageSource();
  42. Map<String, String> header = new HashMap<String, String>();
  43. header.put("Accept", "*/*");
  44. header.put("Connection", "keep-alive");
  45. header.put("Host", "sso.toutiao.com");
  46. header.put("Content-Type", "application/x-www-form-urlencoded");
  47. header.put("Origin", "https://ad.oceanengine.com");
  48. header.put("Referer", "https://ad.oceanengine.com/pages/login/index.html");
  49. Map<String, Object> param = new HashMap<String, Object>();
  50. param.put("mobile", "");
  51. param.put("code", "");
  52. /* param.put("account","guoyanling@c-top.com.cn");
  53. param.put("password","Hcstad-567");*/
  54. param.put("account", "dcd_ad@bytedance.com");
  55. param.put("password", "typdDCD@2018");
  56. param.put("captcha", "hqde");
  57. param.put("is_30_days_no_login", "true");
  58. param.put("service", "https://ad.oceanengine.com");
  59. String res = HttpUtils.httpPostParamRequest("https://sso.toutiao.com/account_login/", param, header);
  60. ObjectMapper mapper = new ObjectMapper();
  61. mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  62. JsonNode jsonNode = mapper.readTree(res);
  63. Integer errorCode = jsonNode.get("error_code").asInt();
  64. String orderId = null;
  65. while (errorCode != 0) {
  66. FateadmUtil fateadmUtil = new FateadmUtil();
  67. fateadmUtil.init();
  68. if (orderId != null) {
  69. fateadmUtil.Justice(orderId);
  70. }
  71. String captcha = jsonNode.get("captcha").asText();
  72. Base64.Decoder decoder = Base64.getDecoder();
  73. FateadmHttpUtil.HttpResp resp = fateadmUtil.Predict("30400", decoder.decode(captcha));
  74. param.put("captcha", resp.pred_resl);
  75. orderId = resp.req_id;
  76. res = HttpUtils.httpPostParamRequest("https://sso.toutiao.com/account_login/", param, header);
  77. jsonNode = mapper.readTree(res);
  78. errorCode = jsonNode.get("error_code").asInt();
  79. }
  80. if (errorCode == 0) {
  81. String csrftoken = "";
  82. List<org.apache.http.cookie.Cookie> cookies = HttpUtils.COOKIESTORE.getCookies();
  83. for (org.apache.http.cookie.Cookie ck : cookies) {
  84. Cookie cookie = new Cookie(ck.getName(), ck.getValue(), "." + ck.getDomain(), ck.getPath(), ck.getExpiryDate());
  85. webDriver.manage().addCookie(cookie);
  86. }
  87. //登录成功 进入推广页面
  88. webDriver.get("https://ad.oceanengine.com/pages/promotion.html#/ad");
  89. //创建广告组页面
  90. webDriver.get("https://ad.oceanengine.com/pages/campaign/create.html");
  91. // HttpUtils.httpGetRequest("https://ad.toutiao.com/pages/promotion.html#/ad");
  92. // HttpUtils.httpGetRequest("https://ad.toutiao.com/pages/campaign/create.html");
  93. Map<String, Object> param2 = new HashMap<>();
  94. Map<String, Object> param3 = new HashMap<>();
  95. param3.put("budget", 0);
  96. param3.put("budget_mode", -1);
  97. param2.put("campaign_id", "");
  98. param2.put("campaign_name", "自动创建测试444444");
  99. param2.put("landing_type", 3);
  100. param2.put("campaign_type", 1);
  101. param2.put("budget", param3);
  102. for (Cookie cookie : webDriver.manage().getCookies()) {
  103. if ("csrftoken".equals(cookie.getName())) {
  104. csrftoken = cookie.getValue();
  105. }
  106. BasicClientCookie ck = new BasicClientCookie(cookie.getName(), cookie.getValue());
  107. ck.setDomain("ad.oceanengine.com");
  108. ck.setExpiryDate(cookie.getExpiry());
  109. ck.setPath(cookie.getPath());
  110. HttpUtils.COOKIESTORE.addCookie(ck);
  111. }
  112. header.put("Accept", "application/json, text/javascript, */*; q=0.01");
  113. header.put("Connection", "keep-alive");
  114. header.put("Host", "ad.oceanengine.com");
  115. header.put("Content-Type", "application/json");
  116. header.put("Origin", "https://ad.oceanengine.com");
  117. header.put("X-CSRFToken", csrftoken);
  118. header.put("X-Requested-With", "XMLHttpRequest");
  119. header.put("Referer", "https://ad.oceanengine.com/pages/campaign/create.html");
  120. // 创建广告组返回信息
  121. res = HttpUtils.httpPostRequest("https://ad.oceanengine.com/overture/campaign/create/", param2, header);
  122. JsonNode node = mapper.readTree(res);
  123. //{"status": "fail", "msg": "\u5e7f\u544a\u7ec4\u4e0d\u80fd\u91cd\u590d", "type": "form_error", "name": "campaign_name"}
  124. //广告组id
  125. String campaignId = node.get("data").get("campaign_id").asText();
  126. String url2 = "https://ad.oceanengine.com/pages/ad/create.html?campaign_id=" + campaignId + "&landing_type=3&temp_id=ADvQb";
  127. webDriver.get(url2);
  128. header.put("Referer", url2);
  129. for (Cookie cookie : webDriver.manage().getCookies()) {
  130. BasicClientCookie ck = new BasicClientCookie(cookie.getName(), cookie.getValue());
  131. ck.setDomain("ad.oceanengine.com");
  132. ck.setExpiryDate(cookie.getExpiry());
  133. ck.setPath(cookie.getPath());
  134. HttpUtils.COOKIESTORE.addCookie(ck);
  135. }
  136. String siteStr = HttpUtils.httpGetRequest("https://ad.oceanengine.com/overture/api/adv_site/get_site_list/?_=" + String.valueOf(System.currentTimeMillis()));
  137. String convStr = HttpUtils.httpGetRequest("https://ad.oceanengine.com/overture/api/get_tetris_action/?external_url=&type=external&pricing=9&landing_type=3&_=" + String.valueOf(System.currentTimeMillis()));
  138. JsonNode siteNode = mapper.readTree(siteStr);
  139. JsonNode convNode = mapper.readTree(convStr);
  140. Map<String, Object> param4 = new HashMap<String, Object>();
  141. param4.put("location_type", 4);
  142. param4.put("aweme_account_fans", 0);
  143. String tokenStr = HttpUtils.httpPostRequest("https://ad.oceanengine.com/api/v1/hl/ad/audience/estimate/", param4, header);
  144. JsonNode tokenNode = mapper.readTree(tokenStr);
  145. Map<String, Object> param5 = new HashMap<String, Object>();
  146. // 投放范围
  147. param5.put("delivery_range", 1);
  148. //广告类别 5 内广
  149. param5.put("classify", 5);
  150. param5.put("projectid", "");
  151. //下载方式 1下载链接 2包含下载链接的落地页
  152. param5.put("download_type", 2);
  153. param5.put("localGeolocation", new ArrayList<String>());
  154. // 落地页链接
  155. param5.put("external_url", "https://ad.oceanengine.com/tetris/page/" + siteNode.get("data").get(0).get("siteId").asText());
  156. // 转换id
  157. param5.put("convert_id", convNode.get("action_data").get(0).get("id").asLong());
  158. // 应用直达链接
  159. param5.put("open_url", "");
  160. param5.put("style_type", 0);
  161. //受众位置类型
  162. param5.put("location_type", 4);
  163. // 过滤已安装,当推广目标为安卓应用下载时可填,0表示不过滤,1表示过滤,默认为不过滤
  164. param5.put("hide_if_exists", 0);
  165. //兴趣分类
  166. param5.put("interest_tag_mode", 0);
  167. param5.put("token", tokenNode.get("data").get("token").asText());
  168. //头条预估数据
  169. param5.put("toutiao", 100000005);
  170. //抖音预估数据
  171. param5.put("aweme", 150000005);
  172. //西瓜预估数据
  173. param5.put("video_app", 40000005);
  174. //火山预估数据
  175. param5.put("hotsoon", 40000005);
  176. //账号粉丝相似人群
  177. param5.put("aweme_account_fans", 0);
  178. // 智能放量????
  179. param5.put("auto_extend_enabled", 0);
  180. // 广告预算类型 0 日预算 1 总预算
  181. param5.put("budget_mode", 0);
  182. //广告预算
  183. param5.put("budget", 300);
  184. //广告投放时间类型 默认1 1:不限 2:按时间???
  185. param5.put("schedule_type", 1);
  186. //广告投放起始时间
  187. param5.put("start_time", "2019-07-02 00:00");
  188. //广告投放结束时间
  189. param5.put("end_time", "2019-07-03 23:59");
  190. //投放时段
  191. param5.put("week_schedule", new ArrayList<String>());
  192. //投放时段
  193. param5.put("week_time", new ArrayList<ArrayList<Integer>>());
  194. //计划出价类型 9 按展示付费 7 按点击付费 8 按转化付费
  195. param5.put("pricing", 9);
  196. //出价为OCPC时,计划处于哪个阶段??
  197. param5.put("cpa_phrase", 2);
  198. param5.put("is_union_slot", "");
  199. //过滤已转化用户类型字段,只有转化为目标时可填 优化目标为转化才展示
  200. param5.put("hide_if_converted", 1);
  201. //投放速度/投放方式使用字段
  202. param5.put("flow_control_mode", 0);
  203. param5.put("cpa_bid", 1);
  204. //自动出价类型,OCPM支持
  205. param5.put("smart_bid_type", 0);
  206. //是否调整自动出价 允许值: "0", "1" 默认值: 0
  207. param5.put("adjust_cpa", 0);
  208. param5.put("app_bid_ratio", new Object());
  209. //广计划名称
  210. param5.put("name", "自动创建测试4");
  211. param5.put("campaign_id", campaignId);
  212. String createResult = HttpUtils.httpPostRequest("https://ad.oceanengine.com/overture/ad/create/", param5, header);
  213. }
  214. // webDriver.close();
  215. } catch (Exception e) {
  216. e.printStackTrace();
  217. }
  218. }
  219. }