import java.net.URI; import java.util.Arrays; import java.util.LinkedHashMap; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; public class BaijiahaoSupport { public static QueryBjhResponse getArtList(String uriStr) { QueryBjhRequest request = new QueryBjhRequest(); request.setApp_id("****"); request.setApp_token("****"); request.setCollection("publish"); RestTemplate template = new RestTemplate(); ClientHttpRequestFactory clientFactory = new HttpComponentsClientHttpRequestFactory(); template.setRequestFactory(clientFactory); URI uri = UriComponentsBuilder.fromUriString(uriStr) // .build().encode().toUri(); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON_UTF8)); requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); @SuppressWarnings({ "rawtypes", "unchecked" }) HttpEntity<QueryBjhRequest> requestEntity = new HttpEntity(request,requestHeaders); ResponseEntity<QueryBjhResponse> responseEntity = template.exchange(uri, HttpMethod.POST, requestEntity, QueryBjhResponse.class); QueryBjhResponse body = responseEntity.getBody(); return body; } public static String withDrawArt(String uriStr,String id) { WdBjhRequest request = new WdBjhRequest(); request.setApp_id("1614170592916180"); request.setApp_token("f026f8a50b1bcbe2440fa7f2a691c96a"); request.setArticle_id(id); RestTemplate template = new RestTemplate(); ClientHttpRequestFactory clientFactory = new HttpComponentsClientHttpRequestFactory(); template.setRequestFactory(clientFactory); URI uri = UriComponentsBuilder.fromUriString(uriStr) // .build().encode().toUri(); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON_UTF8)); requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); @SuppressWarnings({ "rawtypes", "unchecked" }) HttpEntity<WdBjhRequest> requestEntity = new HttpEntity(request,requestHeaders); ResponseEntity<String> responseEntity = template.exchange(uri, HttpMethod.POST, requestEntity, String.class); String body = responseEntity.getBody(); return body; } public static void main(String[] args) { runBjh(); } private static void runBjh() { String list_url = "https://baijiahao.baidu.com/builderinner/open/resource/query/articleListall"; String wd_url = "https://baijiahao.baidu.com/builderinner/open/resource/article/withdraw"; QueryBjhResponse resp = getArtList(list_url); QueryBjhResponse.DataBean data = resp.getData(); QueryBjhResponse.DataBean.PageBean page = data.getPage(); if(page.getItems_count() > 0) { LinkedHashMap<String, ArticleBean> items = data.getItems(); items.keySet().forEach(id->{ withDrawArt(wd_url,id); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }); } if(page.isHas_next()) { runBjh(); } } }
public class QueryBjhRequest { private String app_token;// string 是 授权密钥 private String app_id;// string 是 作者帐号ID private String collection;// string 否 文章状态集,不传默认查询所有支持的文章状态集 // draft-草稿、publish-已发布、pre_publish-待发布、withdraw-已撤回、rejected-未通过 public String getApp_token() { return app_token; } public void setApp_token(String app_token) { this.app_token = app_token; } public String getApp_id() { return app_id; } public void setApp_id(String app_id) { this.app_id = app_id; } public String getCollection() { return collection; } public void setCollection(String collection) { this.collection = collection; } /** * private String start_time;// string 否 支持按照年月日格式(2019-06-01)进行查询,仅支持查询到日维度的数据 private String end_time;// string 否 支持按照年月日格式(2019-07-01)进行查询,仅支持查询到日维度的数据 private int page_no;// int 否 查询页码,不传默认为1 private int page_size;// int 否 查询条数,不能超过20,不传默认为20 private String article_type;// string 否 文章类型,news-图文、gallery-图集、video-视频,不传默认查询所有支持的文章类型 public String getStart_time() { return start_time; } public void setStart_time(String start_time) { this.start_time = start_time; } public String getEnd_time() { return end_time; } public void setEnd_time(String end_time) { this.end_time = end_time; } public int getPage_no() { return page_no; } public void setPage_no(int page_no) { this.page_no = page_no; } public int getPage_size() { return page_size; } public void setPage_size(int page_size) { this.page_size = page_size; } public String getArticle_type() { return article_type; } public void setArticle_type(String article_type) { this.article_type = article_type; } * */ }
import java.util.LinkedHashMap; public class QueryBjhResponse { /** * errno : 0 errmsg : 成功 data : * {"page":{"page_no":1,"page_size":2,"items_count":2,"has_next":true},"items":{"1111111111":{"article_id":"1111111111","app_id":"1111111111","nid":"11111111112","updated_at":1540886721,"type":"news","status":"analyze","collection":"pre_publish","title":"示例标题","article_url":"http://baijiahao.baidu.com/s?id=1111111111"},"22222222":{"article_id":"22222222","app_id":"22222222","nid":"222222223","updated_at":1540883519,"type":"news","status":"audit","collection":"pre_publish","title":"示例标题2","article_url":"http://baijiahao.baidu.com/s?id=22222222"}}} */ private int errno; private String errmsg; private DataBean data; public int getErrno() { return errno; } public void setErrno(int errno) { this.errno = errno; } public String getErrmsg() { return errmsg; } public void setErrmsg(String errmsg) { this.errmsg = errmsg; } public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class DataBean { /** * page : {"page_no":1,"page_size":2,"items_count":2,"has_next":true} items : * {"1111111111":{"article_id":"1111111111","app_id":"1111111111","nid":"11111111112","updated_at":1540886721,"type":"news","status":"analyze","collection":"pre_publish","title":"示例标题","article_url":"http://baijiahao.baidu.com/s?id=1111111111"},"22222222":{"article_id":"22222222","app_id":"22222222","nid":"222222223","updated_at":1540883519,"type":"news","status":"audit","collection":"pre_publish","title":"示例标题2","article_url":"http://baijiahao.baidu.com/s?id=22222222"}} */ private PageBean page; public DataBean() { super(); } private LinkedHashMap<String, ArticleBean> items; public PageBean getPage() { return page; } public void setPage(PageBean page) { this.page = page; } public LinkedHashMap<String, ArticleBean> getItems() { return items; } public void setItems(LinkedHashMap<String, ArticleBean> items) { this.items = items; } public static class PageBean { /** * page_no : 1 page_size : 2 items_count : 2 has_next : true */ private int page_no; public PageBean() { super(); } private int page_size; private int items_count; private boolean has_next; public int getPage_no() { return page_no; } public void setPage_no(int page_no) { this.page_no = page_no; } public int getPage_size() { return page_size; } public void setPage_size(int page_size) { this.page_size = page_size; } public int getItems_count() { return items_count; } public void setItems_count(int items_count) { this.items_count = items_count; } public boolean isHas_next() { return has_next; } public void setHas_next(boolean has_next) { this.has_next = has_next; } } public static class ArticleBean { /** * article_id : 22222222 app_id : 22222222 nid : 222222223 updated_at : * 1540883519 type : news status : audit collection : pre_publish title : 示例标题2 * article_url : http://baijiahao.baidu.com/s?id=22222222 */ private String article_id; public ArticleBean() { } private String app_id; private String nid; private int updated_at; private String type; private String status; private String collection; private String title; private String article_url; public String getArticle_id() { return article_id; } public void setArticle_id(String article_id) { this.article_id = article_id; } public String getApp_id() { return app_id; } public void setApp_id(String app_id) { this.app_id = app_id; } public String getNid() { return nid; } public void setNid(String nid) { this.nid = nid; } public int getUpdated_at() { return updated_at; } public void setUpdated_at(int updated_at) { this.updated_at = updated_at; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getCollection() { return collection; } public void setCollection(String collection) { this.collection = collection; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getArticle_url() { return article_url; } public void setArticle_url(String article_url) { this.article_url = article_url; } } } }
public class WdBjhRequest { private String app_token;// string 是 授权密钥 private String app_id;// string 是 作者帐号ID private String article_id;// string 否 文章状态集,不传默认查询所有支持的文章状态集 // draft-草稿、publish-已发布、pre_publish-待发布、withdraw-已撤回、rejected-未通过 public String getApp_token() { return app_token; } public void setApp_token(String app_token) { this.app_token = app_token; } public String getApp_id() { return app_id; } public void setApp_id(String app_id) { this.app_id = app_id; } public String getArticle_id() { return article_id; } public void setArticle_id(String article_id) { this.article_id = article_id; } }