Prechádzať zdrojové kódy

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	jeecg-boot-module-system/src/test/java/org/jeecg/SampleTest.java
syh 5 rokov pred
rodič
commit
9e3d2aa217

+ 32 - 4
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/controller/TestController.java

@@ -191,6 +191,34 @@ public class TestController {
         thread.start();
     }
 
+
+    @GetMapping(value = "/getPlanAndGroupByAccount")
+    public void getPlanAndGroupByAccount(Long accountId) {
+        QueryWrapper<CtopOauthToken> oauthTokenQueryWrapper = new QueryWrapper<>();
+        oauthTokenQueryWrapper.eq("media_id", 2);
+        oauthTokenQueryWrapper.eq("account_id", accountId);
+        oauthTokenQueryWrapper.last("limit 1");
+        CtopOauthToken token = oauthTokenMapper.selectOne(oauthTokenQueryWrapper);
+        executorService.submit(new Runnable() {
+            @Override
+            public void run() {
+                try {
+
+                    kuaishouInterfaceService.getCampaignList(token, null, null);
+                    //1:获取全量广告组数据
+                    kuaishouInterfaceService.getGroupList(token, null, null);
+
+                    Thread.sleep(1 * 200);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+
+
+    }
+
+
     @GetMapping(value = "/gerCreativeByAccount")
     public void gerCreative(Long accountId) {
         QueryWrapper<CtopOauthToken> oauthTokenQueryWrapper = new QueryWrapper<>();
@@ -203,12 +231,12 @@ public class TestController {
             public void run() {
                 try {
                     System.err.println("获取创意,accountId:" + token.getAccountId());
+                    kuaishouInterfaceService.getCampaignList(token, null, null);
+                    //1:获取全量广告组数据
+                    kuaishouInterfaceService.getGroupList(token, null, null);
                     kuaishouInterfaceService.getCreativeList(token, null, null);
-                    //  kuaishouInterfaceService.getVideoList2(token, null, null,1);
-                    //3: 获取图片信息数据
-                    // kuaishouInterfaceService.getImageList2(token, null, null,1);
+
                     Thread.sleep(1 * 200);
-                    //  kuaishouInterfaceService.getAppList(token.getAccountId(), token.getAccessToken());
                 } catch (Exception e) {
                     e.printStackTrace();
                 }

+ 102 - 14
jeecg-boot-module-system/src/test/java/org/jeecg/SampleTest.java

@@ -1,18 +1,15 @@
 package org.jeecg;
 
-import cn.com.ctop.common.module.entity.CtopOauthToken;
-import cn.com.ctop.common.module.service.ICtopOauthTokenService;
-import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
 import lombok.extern.slf4j.Slf4j;
-import org.jeecg.common.util.DateUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
-import java.util.Date;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
@@ -20,16 +17,107 @@ import java.util.Date;
 public class SampleTest {
     @Value("${jeecg.path.chrome-driver}")
     private String chromeDriver;
-    @Autowired
-    private ICtopOauthTokenService tokenService;
-    @Autowired
-    private IKuaishouInterfaceService kuaishouInterfaceService;
 
     @Test
-    public void loadKuaishouData() {
-        Date getDate = DateUtils.addDay(new Date(), -1);
-        CtopOauthToken token = tokenService.getTokenByAccountId(3652066L);
-        kuaishouInterfaceService.getAdvertiserReportDaily(token, getDate, getDate);
+    public void kuaisShouReport() {
+
+
+        try {
+          /*  String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/create";
+            Map<String, String> headers = new HashMap<>();
+            headers.put("Content-Type", " application/json");
+            headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
+            JSONObject param = new JSONObject();
+            param.put("advertiser_id", 142402);
+            param.put("task_name", "142402-test");
+
+            JSONObject taskParams = new JSONObject();
+            taskParams.put("start_date", "2020-01-01");
+            taskParams.put("end_date", "2020-03-05");
+            taskParams.put("view_type", 1);
+            param.put("task_params",taskParams);
+
+
+
+            String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
+            JSONObject resultJson = JSONObject.parseObject(result);
+            System.err.println(resultJson);*/
+
+            // 1229917  142402
+
+
+
+         /*   String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/list";
+            Map<String, String> headers = new HashMap<>();
+            headers.put("Content-Type", " application/json");
+            headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
+            JSONObject param = new JSONObject();
+            param.put("advertiser_id", 142402);
+            JSONArray taskIds = new JSONArray();
+            taskIds.add(1229917);
+
+            param.put("task_ids", taskIds);
+            String result = HttpUtils.kuaiShouhttpPostRequest(url, param.toJSONString(), headers);
+            JSONObject resultJson = JSONObject.parseObject(result);
+            System.err.println(resultJson);*/
+
+
+          /*  String url = "https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download";
+
+            Map<String, String> headers = new HashMap<>();
+            headers.put("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
+
+            Map<String,Object> param = new HashMap<>();
+            param.put("advertiser_id", 142402);
+            param.put("task_id", "1229917");
+            String result = HttpUtils.KuaiShouttpGetRequest(url, param,headers);*/
+
+
+            URL url = new URL("https://ad.e.kuaishou.com/rest/openapi/v1/async_task/download?task_id=1229917&advertiser_id=142402");
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+            //设置超时间为3秒
+            conn.setConnectTimeout(10 * 1000);
+            //防止屏蔽程序抓取而返回403错误
+            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+            conn.setRequestProperty("Access-Token", "eb9a0d18072eabc339ee26ad010ecd6b");
+            //得到输入流
+            InputStream inputStream = conn.getInputStream();
+            //获取自己数组
+            byte[] getData = readInputStream(inputStream);
+
+            //文件保存位置
+            File saveDir = new File("D:\\file");
+            if (!saveDir.exists()) {
+                saveDir.mkdirs();
+            }
+            File file = new File(saveDir + File.separator + "qqww.csv");
+            FileOutputStream fos = new FileOutputStream(file);
+            fos.write(getData);
+            if (fos != null) {
+                fos.close();
+            }
+            if (inputStream != null) {
+                inputStream.close();
+            }
+
+
+        } catch (Exception e) {
+            e.printStackTrace();
+
+        }
+
+
+    }
+
+    public static byte[] readInputStream(InputStream inputStream) throws IOException {
+        byte[] buffer = new byte[1024];
+        int len = 0;
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        while ((len = inputStream.read(buffer)) != -1) {
+            bos.write(buffer, 0, len);
+        }
+        bos.close();
+        return bos.toByteArray();
     }
 }
 

+ 90 - 4
module-common/src/main/java/cn/com/ctop/common/module/utils/HttpUtils.java

@@ -167,7 +167,7 @@ public class HttpUtils {
         String strReturn = "";
         try {
             HttpPost httppost = new HttpPost(url);
-            if(headers != null){
+            if (headers != null) {
                 for (String key : headers.keySet()) {
                     httppost.setHeader(key, headers.get(key));
                 }
@@ -207,7 +207,7 @@ public class HttpUtils {
         try {
             HttpPost httppost = new HttpPost(url);
             httppost.setHeader("User-Agent", USER_AGENT);
-            if(headers != null){
+            if (headers != null) {
                 for (String key : headers.keySet()) {
                     httppost.setHeader(key, headers.get(key));
                 }
@@ -257,8 +257,8 @@ public class HttpUtils {
             if (param != null && param.size() > 0) {
                 httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8")));
             }
-            RequestConfig requestConfig =  RequestConfig.custom().setSocketTimeout(120 * 1000).setConnectTimeout(120 * 1000).build();
-                httppost.setConfig(requestConfig);
+            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120 * 1000).setConnectTimeout(120 * 1000).build();
+            httppost.setConfig(requestConfig);
             HttpEntity respentity;
 
             HttpResponse response = httpClient.execute(httppost);
@@ -685,4 +685,90 @@ public class HttpUtils {
         }
         return result;
     }
+
+
+    public static JSONObject kuaiShouHttpGetRequest(String accessToken, String url, JSONObject params) {
+        // 构造请求
+        HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
+            @Override
+            public String getMethod() {
+                return "GET";
+            }
+        };
+        httpEntity.setHeader("Access-Token", accessToken);
+        CloseableHttpResponse response = null;
+        CloseableHttpClient client = null;
+        try {
+            client = HttpClientBuilder.create().build();
+            httpEntity.setURI(URI.create(url));
+            httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
+            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
+            httpEntity.setConfig(requestConfig);
+            response = client.execute(httpEntity);
+            if (response != null && response.getStatusLine().getStatusCode() == 200) {
+                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
+                StringBuilder result = new StringBuilder();
+                String line = "";
+                while ((line = bufferedReader.readLine()) != null) {
+                    result.append(line);
+                }
+                bufferedReader.close();
+                return JSONObject.parseObject(result.toString());
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                if (response != null) {
+                    response.close();
+                }
+                if (client != null) {
+                    client.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+
+
+    public static String KuaiShouttpGetRequest(String url, Map<String, Object> params, Map<String, String> headers) throws Exception {
+        HttpClient httpclient = getHttpclient();
+        String strReturn = "";
+        try {
+
+
+            StringBuilder postBody = new StringBuilder();
+            for (Map.Entry<String, Object> entry : params.entrySet()) {
+                if (entry.getValue() == null) {
+                    continue;
+                }
+                postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(),
+                        "utf-8")).append("&");
+            }
+            if (!params.isEmpty()) {
+                postBody.deleteCharAt(postBody.length() - 1);
+            }
+
+            HttpGet httpget = new HttpGet(url + "?" + postBody);
+            System.err.println(url + postBody);
+            if (headers != null) {
+                Iterator<String> keyIter = headers.keySet().iterator();
+                while (keyIter.hasNext()) {
+                    String curKey = keyIter.next();
+                    if (curKey != null && headers.get(curKey) != null) {
+                        httpget.addHeader(curKey, headers.get(curKey));
+                    }
+                }
+            }
+            HttpEntity respentity;
+            HttpResponse response = httpclient.execute(httpget);
+            respentity = response.getEntity();
+            strReturn = EntityUtils.toString(respentity);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return strReturn;
+    }
 }