查面灵器喵的
public void test() {
String baseUrl = "https://m.weibo.cn/api/container/getIndex";
HttpRequest httpRequest = HttpRequest.get(baseUrl).form("type", "uid").form("value", "7629946033");
JSON json = JSONUtil.parse(httpRequest.execute().body());
String name = UnicodeUtil.toString(json.getByPath("data.userInfo.screen_name", String.class));
String containerId = json.getByPath("data.tabsInfo.tabs[1].containerid", String.class);
String sinceId = json.getByPath("data.cardlistInfo.since_id", String.class);
List<JSONArray> cardsList = new ArrayList<>();
int i = 1;
while (i < 2) {
//第一条不需要sinceId
if (1 == i) {
httpRequest.form("containerid", containerId);
JSON json2 = JSONUtil.parse(httpRequest.execute().body());
Integer ok = json2.getByPath("ok", Integer.class);
if (0 == ok) break;
JSONArray cards = json2.getByPath("data.cards", JSONArray.class);
cardsList.add(cards);
} else {
httpRequest.form("since_id", sinceId);
JSON json3 = JSONUtil.parse(httpRequest.execute().body());
Integer ok = json3.getByPath("ok", Integer.class);
if (0 == ok) break;
sinceId = json3.getByPath("data.cardlistInfo.since_id", String.class);
JSONArray cards = json3.getByPath("data.cards", JSONArray.class);
cardsList.add(cards);
}
i++;
}
List<Card> cardList = new ArrayList<>();
for (JSONArray jsonArray : cardsList) {
List<Card> cards = jsonArray.toList(Card.class);
cardList.addAll(cards);
}
cardList.forEach(item -> {
item.getMblog().format();
System.out.println(item.getMblog().getText());
});
}
@Data
public static class Card {
private Integer card_type;
private Mblog mblog;
}
@Data
public static class Mblog {
private String text;
private String created_at;
private LocalDateTime createTime;
private void format() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
this.createTime = LocalDateTime.parse(this.created_at.replace("+0800", "UTC"), formatter);
StringBuilder sb = new StringBuilder(
this.text.trim().replace("<br /> <br />", "\n")
.replace("<br /><br />", "\n")
.replace("<br />", "\n")
.replace("阴阳师手游", ""));
//去除<>包裹的
int count = StrUtil.count(sb, "<");
for (int i = 0; i < count; i++) {
sb.delete(sb.indexOf("<"), sb.indexOf(">") + 1);
}
//去除##包裹的
int count2 = StrUtil.count(sb, "#") / 2;
for (int i = 0; i < count2; i++) {
sb.delete(sb.indexOf("#"), sb.indexOf("#", sb.indexOf("#") + 1) + 1);
}
//去除完再清理一遍换行符
if (0 == sb.indexOf("\n")) {
sb.delete(0, 1);
}
if (sb.length() == sb.lastIndexOf("\n") +1) {
sb.delete(sb.length() -1, sb.length());
}
this.text = sb.toString();
}
}