|
@@ -0,0 +1,77 @@
|
|
|
+package com.tzld.longarticle.recommend.server.remote;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.tzld.longarticle.recommend.server.common.HttpPoolFactory;
|
|
|
+import com.tzld.longarticle.recommend.server.model.remote.Article;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.StatusLine;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class ArticleListRemoteService {
|
|
|
+
|
|
|
+ private final CloseableHttpClient client = HttpPoolFactory.defaultPool();
|
|
|
+ private static final String articleListUrl = "http://192.168.100.31:8179/artlce_list";
|
|
|
+
|
|
|
+ public List<Article> articleList(String accountName, List<Integer> indexList) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ List<String> keys = Arrays.asList("Title",
|
|
|
+ "ItemIndex",
|
|
|
+ "show_view_count",
|
|
|
+ "ContentUrl",
|
|
|
+ "msg_time");
|
|
|
+ JSONObject bodyParam = new JSONObject();
|
|
|
+ bodyParam.put("account_nickname", accountName);
|
|
|
+ bodyParam.put("filter_same", false);
|
|
|
+ bodyParam.put("filter_sensitive", false);
|
|
|
+ bodyParam.put("index_list", indexList);
|
|
|
+ bodyParam.put("keys", keys);
|
|
|
+ bodyParam.put("msg_type", 9);
|
|
|
+ bodyParam.put("rate", 0.1);
|
|
|
+ bodyParam.put("reverse", true);
|
|
|
+ bodyParam.put("top_n", 10000);
|
|
|
+ bodyParam.put("use_max_time", true);
|
|
|
+ bodyParam.put("use_min_time", true);
|
|
|
+ try {
|
|
|
+ HttpPost httpPost = new HttpPost(articleListUrl);
|
|
|
+ StringEntity stringEntity = new StringEntity(bodyParam.toJSONString(), StandardCharsets.UTF_8);
|
|
|
+ httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ httpPost.setEntity(stringEntity);
|
|
|
+ CloseableHttpResponse response = client.execute(httpPost);
|
|
|
+ StatusLine statusLine = response.getStatusLine();
|
|
|
+ if (statusLine.getStatusCode() == 200) {
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
+ if (Objects.nonNull(responseEntity)) {
|
|
|
+ String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
|
|
|
+ log.info("articleList 返回的数据:{}", responseBody);
|
|
|
+ JSONObject articleListResponse = JSONObject.parseObject(responseBody);
|
|
|
+ if (Objects.nonNull(articleListResponse)) {
|
|
|
+ JSONArray articleList = articleListResponse.getJSONArray("article_list");
|
|
|
+ if (CollectionUtils.isNotEmpty(articleList)) {
|
|
|
+ return articleList.toJavaList(Article.class);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("articleList error", e);
|
|
|
+ }
|
|
|
+ log.info("articleList耗时:{}", System.currentTimeMillis() - start);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|