依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
</dependencies>
代码
package com.xiaobai.demo.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xiaobai.demo.entity.Article;
import com.xiaobai.demo.entity.HistoryToday;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping(value = "/api/",method = {RequestMethod.POST,RequestMethod.GET})
@CrossOrigin
public class BingController {
/**
* @Author xiaodingfeng
* @Description 获取bing每日一图
* @Date 13:28 2021/3/15
**/
@GetMapping("bing")
@PostMapping("bing")
public void getBingImage(HttpServletResponse response) throws IOException {
String URL= "https://cn.bing.com";
Document document = connection(URL);
String url = document.selectFirst("#bgLink").attr("href");
String title = document.selectFirst("#sh_cp").attr("title");
response.sendRedirect(URL+url);
}
/**
* @Author xiaodingfeng
* @Description 获取随机暖心话语
* @Date 13:25 2021/3/15
**/
@GetMapping("sweet")
@PostMapping("sweet")
public String getSweet() throws IOException {
String URL = "https://api.lovelive.tools/api/SweetNothings";
return connection(URL).body().text();
}
/**
* @Author xiaodingfeng
* @Description 每日一文
* @Date 13:44 2021/3/15
**/
@GetMapping("meiriyiwen")
@PostMapping("meiriyiwen")
public Article getMeiriyiwen() throws IOException {
String URL= "https://meiriyiwen.com/";
Document document = connection(URL);
Element element = document.selectFirst("#article_show");
String title = element.selectFirst("h1").text();
String author = element.selectFirst("p.article_author").text();
Element content = element.selectFirst("div.article_text");
Article article = new Article(title, author, content.html(), content.text().length());
return article;
}
/**
* @Author xiaodingfeng
* @Description 随机输出高清头像图片
* @Date 14:05 2021/3/15
**/
@GetMapping("portrait")
@PostMapping("portrait")
public String getPortrait(String type,String format,HttpServletResponse response) throws IOException {
String URL = "https://api.66mz8.com/api/rand.portrait.php?type="+type+"&format="+format;
String document = connection(URL).text();
JSONObject jsonObject=JSONObject.parseObject(document);
if (jsonObject!=null&&jsonObject.getString("code").equals("200"))
response.sendRedirect(jsonObject.getString("pic_url"));
return null;
}
/**
* @Author xiaodingfeng
* @Description 历史上的今天
* @Date 15:52 2021/3/15
**/
@GetMapping("historytoday")
@PostMapping("historytoday")
public List<HistoryToday> getHistoryToday() throws IOException {
String URL = "https://api.asilu.com/today";
List<HistoryToday> list = new ArrayList<>();
Document connection = connection(URL);
JSONObject parse = JSONObject.parseObject(connection.text());
JSONArray objects = JSONArray.parseArray(parse.getString("data"));
for (int i = 0; i < objects.size(); i++) {
JSONObject jsonObject = objects.getJSONObject(i);
String year = jsonObject.getString("year");
String title = jsonObject.getString("title");
String link = jsonObject.getString("link");
String type = jsonObject.getString("type");
list.add(new HistoryToday(year,title,link,type));
}
return list;
}
/**
* @Author xiaodingfeng
* @Description json格式化
* @Date 15:13 2021/3/15
**/
@GetMapping("stringtojson")
@PostMapping("stringtojson")
public String stringTojson(String str){
System.out.println(str);
JSONArray object = JSONArray.parseArray(str);
String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
return pretty;
}
/**
* @Author xiaodingfeng
* @Description 统一连接池
* @Date 14:06 2021/3/15
**/
public static Document connection(String url) throws IOException {
return Jsoup.connect(url).
ignoreContentType(true)
.userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36")
.timeout(5000)
.get();
}
}
评论区