commit
This commit is contained in:
@@ -27,6 +27,7 @@ import org.nutz.dao.util.Daos;
|
||||
import org.nutz.integration.jedis.RedisService;
|
||||
import org.nutz.ioc.loader.annotation.Inject;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
import org.nutz.json.Json;
|
||||
import org.nutz.lang.Lang;
|
||||
import org.nutz.lang.Strings;
|
||||
import org.nutz.lang.util.NutMap;
|
||||
@@ -38,10 +39,9 @@ import org.nutz.mvc.annotation.Param;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@IocBean
|
||||
@@ -263,76 +263,31 @@ public class SysHomeController {
|
||||
// @CacheDefaults(cacheName = RedisConstant.PLATFORM_REDIS_WKCACHE_PREFIX + "web_news", isHash = true)
|
||||
// @CacheResult(cacheKey = "news", ignoreNull = true, cacheLiveTime = 60 * 60 * 24)
|
||||
public Result getNews() {
|
||||
// 定义要爬取的URL
|
||||
String domain = "https://gh.whcp.edu.cn";
|
||||
try {
|
||||
// 使用Jsoup连接到URL并获取页面内容
|
||||
Document doc = Jsoup.connect(domain).get();
|
||||
try {
|
||||
String url = "https://xgh.cug.edu.cn/";
|
||||
Document document = Jsoup.connect(url).get();
|
||||
Elements homeElements = document.select(".gh,.jc,.fc,.jg");
|
||||
|
||||
JSONObject root = new JSONObject();
|
||||
|
||||
// 1. 新闻快讯(来自 .list1)
|
||||
/* List<JSONObject> newsArray = new ArrayList<>();
|
||||
Elements newsItems = doc.select(".ml .main_page2 .news_list li");
|
||||
for (Element item : newsItems) {
|
||||
String date = item.select(".date").text().trim();
|
||||
Element link = item.select("a.text").first();
|
||||
String title = link.select(".title").text().trim();
|
||||
String url = link.attr("href").trim();
|
||||
|
||||
JSONObject node = new JSONObject();
|
||||
node.set("title", title);
|
||||
node.set("date", date);
|
||||
node.set("url", domain + url);
|
||||
newsArray.add(node);
|
||||
}
|
||||
root.set("news", newsArray);*/
|
||||
|
||||
// 2. 通知公告(来自 .list2)
|
||||
List<JSONObject> noticesArray = new ArrayList<>();
|
||||
Elements noticeItems = doc.select(".tu_text .news_list li");
|
||||
for (Element item : noticeItems) {
|
||||
String date = item.select(".news_meta").text().trim();
|
||||
Element link = item.select(".news_title a").first();
|
||||
String title = link.text().trim();
|
||||
String url = link.attr("href").trim();
|
||||
JSONObject node = new JSONObject();
|
||||
node.set("title", title);
|
||||
node.set("date", date);
|
||||
node.set("url", domain + url);
|
||||
noticesArray.add(node);
|
||||
}
|
||||
root.set("notices", noticesArray);
|
||||
|
||||
// 3. 基层风采(来自 .pic-list1)
|
||||
/* List<JSONObject> grassrootsArray = new ArrayList<>();
|
||||
Elements grassrootsItems = doc.select(".group3 .pic-list1 li");
|
||||
for (Element item : grassrootsItems) {
|
||||
Element link = item.select("a.img-scale").first();
|
||||
String date = link.select(".date").text().trim();
|
||||
String title = link.select(".title").text().trim();
|
||||
String url = link.attr("href").trim();
|
||||
String summary = link.select(".info").text().trim();
|
||||
|
||||
String imageUrl = "";
|
||||
Element img = link.select("img").first();
|
||||
if (img != null) {
|
||||
imageUrl = img.attr("src").trim();
|
||||
}
|
||||
|
||||
JSONObject node = new JSONObject();
|
||||
node.set("title", title);
|
||||
node.set("date", date);
|
||||
node.set("url", domain + url);
|
||||
node.set("summary", summary);
|
||||
node.set("image", domain + imageUrl);
|
||||
grassrootsArray.add(node);
|
||||
}
|
||||
root.set("grassroots", grassrootsArray);*/
|
||||
return Result.success(root);
|
||||
} catch (Exception e) {
|
||||
log.error(e);
|
||||
return Result.success();
|
||||
}
|
||||
List<Map<String, Object>> news = homeElements.stream().map(homeElement -> {
|
||||
//获取板块名称
|
||||
String name = homeElement.selectFirst(".title").text();
|
||||
Elements liElements = homeElement.select("li");
|
||||
List<NutMap> ele = liElements.stream().map(liElement -> {
|
||||
NutMap row = NutMap.NEW();
|
||||
String href = liElement.selectFirst("a").attr("href");
|
||||
String time = liElement.selectFirst("span").text();
|
||||
String title = liElement.selectFirst("a").attr("title");
|
||||
row.put("href", href);
|
||||
row.put("time", time);
|
||||
row.put("text", title);
|
||||
return row;
|
||||
}).toList();
|
||||
return Map.of("label", name, "value", ele);
|
||||
}).collect(Collectors.toList());
|
||||
redisService.setex("gonghui_news", 60 * 60 * 24, Json.toJson(news));
|
||||
return Result.success(news);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user