Browse Source

Merge remote-tracking branch 'origin/test' into test

syh 5 năm trước cách đây
mục cha
commit
53412d6a66

+ 14 - 8
module-common/src/main/java/cn/com/ctop/common/module/utils/HttpClientUtils.java

@@ -24,6 +24,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.impl.cookie.BasicClientCookie;
 import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.protocol.HTTP;
 import org.apache.http.ssl.SSLContextBuilder;
 import org.apache.http.util.EntityUtils;
 
@@ -172,7 +173,7 @@ public class HttpClientUtils {
                 BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, String.valueOf(httpClientEntity.getParams().get(key)));
                 paramList.add(basicNameValuePair);
             }
-            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList);
+            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList, "UTF-8");
             httppost.setEntity(formEntity);
             CloseableHttpClient httpClient = httpClientEntity.getCloseableHttpClient();
             HttpResponse response = httpClient.execute(httppost);
@@ -239,7 +240,16 @@ public class HttpClientUtils {
         try {
             StringEntity entity = new StringEntity(json, "utf-8");
             httpPost.setEntity(entity);
+            httpPost.setProtocolVersion(HttpVersion.HTTP_1_0);
+            httpPost.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
             response = httpClient.execute(httpPost);
+//            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
+//            String line = null;
+//            StringBuilder builder = new StringBuilder();
+//            while ((line = reader.readLine()) != null) {
+//                builder.append(line);
+//            }
+            String result = EntityUtils.toString(response.getEntity(), "utf-8");
             Header[] headers = response.getHeaders("Set-Cookie");
             if (headers != null && headers.length > 0) {
                 for (Header header : headers) {
@@ -267,13 +277,9 @@ public class HttpClientUtils {
                     cookieStore.addCookie(basicClientCookie);
                 }
             }
-            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
-            String line = null;
-            StringBuilder builder = new StringBuilder();
-            while ((line = reader.readLine()) != null) {
-                builder.append(line);
-            }
-            httpClientEntity.setResult(builder.toString());
+
+//            httpClientEntity.setResult(builder.toString());
+            httpClientEntity.setResult(result);
             log.info(httpClientEntity.getResult());
         } catch (Exception e) {
             e.printStackTrace();

+ 2 - 2
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/graphql/controller/KuaishouWebController.java

@@ -117,10 +117,10 @@ public class KuaishouWebController {
     }
 
     @PostMapping(value = "/comment/add")
-    public Result<Map<String, Object>> commentAdd(String ksid, String photoId, String principalId, String content, Long replyToCommentId, Long repltTo) {
+    public Result<Map<String, Object>> commentAdd(String ksid, String photoId, String content, Long replyToCommentId, Long repltTo) {
         Result<Map<String, Object>> result = new Result<Map<String, Object>>();
         try {
-            Map<String, Object> statusMap = kuaishouWebInterfaceService.commentAdd(ksid, photoId, principalId, content, replyToCommentId, repltTo);
+            Map<String, Object> statusMap = kuaishouWebInterfaceService.commentAdd(ksid, photoId, content, replyToCommentId, repltTo);
             result.setSuccess(true);
             result.setResult(statusMap);
         } catch (Exception e) {

+ 1 - 1
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/graphql/service/IKuaishouWebInterfaceService.java

@@ -28,7 +28,7 @@ public interface IKuaishouWebInterfaceService {
 
     public Map<String, Object> commentList(String ksid, String photoId, String pcursor);
 
-    public Map<String, Object> commentAdd(String ksid, String photoId, String principalId, String content, Long replyToCommentId, Long replyTo);
+    public Map<String, Object> commentAdd(String ksid, String photoId, String content, Long replyToCommentId, Long replyTo);
 
     public Map<String, Object> subCommentList(String ksid, String photoId, Long rootCommentId, String pcursor);
 }

+ 39 - 18
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/graphql/service/impl/KuaishouWebInterfaceServiceImpl.java

@@ -134,7 +134,8 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
                 resultMap = mapper.readValue(httpClientEntity.getResult(), new TypeReference<Map<String, Object>>() {
                 });
 //                checkCaptcha(httpClientEntity.getResult());
-                if (resultMap.containsKey("captcha")) {
+                String psor = mapper.readTree(httpClientEntity.getResult()).get("data").get("publicFeeds").get("pcursor").asText();
+                if (resultMap.containsKey("captcha") || psor == null) {
                     ipPool.setStatus(2);
                     iIpPoolService.updateById(ipPool);
                     return subCommentList(ksid, photoId, rootCommentId, pcursor);
@@ -174,24 +175,28 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
             cookieStore.addCookie(basicClientCookie4);
             HttpClientEntity httpClientEntity = new HttpClientEntity();
             HttpClientUtils httpClientUtils = new HttpClientUtils();
-            httpClientEntity.setCloseableHttpClient(httpClientUtils.createSSLClientDefault(ipPool));
-            httpClientEntity.setHeaders(getHeaders());
-            httpClientEntity.setParams(params);
-            httpClientEntity.setUrl("https://live.kuaishou.com/graphql");
-            httpClientEntity.setResult("");
-            httpClientEntity.setCookieStore(cookieStore);
-            httpClientEntity = httpClientUtils.callingGraph(httpClientEntity, commentListQuery(photoId, pcursor));
             try {
+                httpClientEntity.setCloseableHttpClient(httpClientUtils.createSSLClientDefault(ipPool));
+                httpClientEntity.setHeaders(getHeaders());
+                httpClientEntity.setParams(params);
+                httpClientEntity.setUrl("https://live.kuaishou.com/graphql");
+                httpClientEntity.setResult("");
+                httpClientEntity.setCookieStore(cookieStore);
+                httpClientEntity = httpClientUtils.callingGraph(httpClientEntity, commentListQuery(photoId, pcursor));
+
                 ObjectMapper mapper = new ObjectMapper();
                 resultMap = mapper.readValue(httpClientEntity.getResult(), new TypeReference<Map<String, Object>>() {
                 });
+                String psor = mapper.readTree(httpClientEntity.getResult()).get("data").get("publicFeeds").get("pcursor").asText();
 //                checkCaptcha(httpClientEntity.getResult());
-                if (resultMap.containsKey("captcha")) {
+                if (resultMap.containsKey("captcha") || psor == null) {
                     ipPool.setStatus(2);
                     iIpPoolService.updateById(ipPool);
                     return commentList(ksid, photoId, pcursor);
                 }
             } catch (Exception e) {
+                ipPool.setStatus(2);
+                iIpPoolService.updateById(ipPool);
                 e.printStackTrace();
             }
         }
@@ -244,12 +249,13 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
             httpClientEntity.setHeaders(getHeaders());
             httpClientEntity.setUrl("https://live.kuaishou.com/graphql");
             httpClientEntity = httpClientUtils.callingGraph(httpClientEntity, videoFeeds(kuaishouUserCookie.getEid(), pcursor));
-            String result = httpClientEntity.getResult();
-            Map<String, String> map = getTTfNumber(kuaishouUserCookie);
-            for (String key : map.keySet()) {
-                result = result.replace(key, map.get(key));
-            }
             try {
+                String result = httpClientEntity.getResult();
+                Map<String, String> map = getTTfNumber(kuaishouUserCookie);
+                for (String key : map.keySet()) {
+                    result = result.replace(key, map.get(key));
+                }
+
                 ObjectMapper mapper = new ObjectMapper();
                 resultMap = mapper.readValue(result, new TypeReference<Map<String, Object>>() {
                 });
@@ -260,6 +266,8 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
                     return videoList(ksid, pcursor);
                 }
             } catch (Exception e) {
+                ipPool.setStatus(2);
+                iIpPoolService.updateById(ipPool);
                 e.printStackTrace();
             }
         }
@@ -317,6 +325,7 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
         queryWrapper.eq("ksid", ksid);
         KuaishouUserCookie kuaishouUserCookie = kuaishouUserCookieService.getOne(queryWrapper);
         if (kuaishouUserCookie != null) {
+            IpPool ipPool = iIpPoolService.getAvaliableIp();
             Map<String, Object> params = new HashMap<String, Object>();
             params.put("photoId", photoId);
             params.put("commentId", commentId);
@@ -327,7 +336,7 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
             headers.put("kpf", "PC_WEB");
             HttpClientEntity httpClientEntity = new HttpClientEntity();
             HttpClientUtils httpClientUtils = new HttpClientUtils();
-            httpClientEntity.setCloseableHttpClient(httpClientUtils.createSSLClientDefault(null));
+            httpClientEntity.setCloseableHttpClient(httpClientUtils.createSSLClientDefault(ipPool));
             httpClientEntity.setUrl("https://live.kuaishou.com/rest/wd/comment/delete");
             httpClientEntity.setResult("");
             httpClientEntity.setCookieStore(cookieTransfer(kuaishouUserCookie.getCookie()));
@@ -346,6 +355,11 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
                 ObjectMapper mapper = new ObjectMapper();
                 resultMap = mapper.readValue(httpClientEntity.getResult(), new TypeReference<Map<String, Object>>() {
                 });
+                if (((Integer) resultMap.get("result")) == 109) {
+                    ipPool.setStatus(2);
+                    iIpPoolService.updateById(ipPool);
+                    return commentDelete(ksid, photoId, commentId);
+                }
             } catch (Exception e) {
                 e.printStackTrace();
             }
@@ -353,7 +367,7 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
         return resultMap;
     }
 
-    public Map<String, Object> commentAdd(String ksid, String photoId, String principalId, String content, Long replyToCommentId, Long replyTo) {
+    public Map<String, Object> commentAdd(String ksid, String photoId, String content, Long replyToCommentId, Long replyTo) {
         LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
         QueryWrapper<KuaishouUserCookie> queryWrapper = new QueryWrapper<KuaishouUserCookie>();
         Map<String, Object> resultMap = new HashMap<>();
@@ -361,9 +375,10 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
         queryWrapper.eq("ksid", ksid);
         KuaishouUserCookie kuaishouUserCookie = kuaishouUserCookieService.getOne(queryWrapper);
         if (kuaishouUserCookie != null) {
+            IpPool ipPool = iIpPoolService.getAvaliableIp();
             Map<String, Object> params = new HashMap<String, Object>();
             params.put("photoId", photoId);
-            params.put("principalId", principalId);
+            params.put("principalId", kuaishouUserCookie.getEid());
             params.put("content", content);
             params.put("replyToCommentId", replyToCommentId);
             params.put("replyTo", replyTo);
@@ -374,9 +389,10 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
             System.out.println(new Gson().toJson(params));
             HttpClientEntity httpClientEntity = new HttpClientEntity();
             HttpClientUtils httpClientUtils = new HttpClientUtils();
-            httpClientEntity.setCloseableHttpClient(httpClientUtils.createSSLClientDefault(null));
+            httpClientEntity.setCloseableHttpClient(httpClientUtils.createSSLClientDefault(ipPool));
             httpClientEntity.setUrl("https://live.kuaishou.com/rest/wd/photo/comment/add");
             httpClientEntity.setResult("");
+            httpClientEntity.setCookieStore(cookieTransfer(kuaishouUserCookie.getCookie()));
             List<Cookie> list = httpClientEntity.getCookieStore().getCookies();
             String did = "";
             for (Cookie ck : list) {
@@ -392,6 +408,11 @@ public class KuaishouWebInterfaceServiceImpl implements IKuaishouWebInterfaceSer
                 ObjectMapper mapper = new ObjectMapper();
                 resultMap = mapper.readValue(httpClientEntity.getResult(), new TypeReference<Map<String, Object>>() {
                 });
+                if (((Integer) resultMap.get("result")) == 109) {
+                    ipPool.setStatus(2);
+                    iIpPoolService.updateById(ipPool);
+                    return commentAdd(ksid, photoId, content, replyToCommentId, replyTo);
+                }
             } catch (Exception e) {
                 e.printStackTrace();
             }