Test.java 14 KB

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