You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
7.1 KiB
190 lines
7.1 KiB
package com.zc.business.controller;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
import com.zc.business.enums.UniversalEnum;
|
|
import com.zc.common.core.httpclient.OkHttp;
|
|
import com.zc.common.core.httpclient.exception.HttpException;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import okhttp3.Response;
|
|
import org.jsoup.Jsoup;
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.nodes.Element;
|
|
import org.jsoup.select.Elements;
|
|
import org.junit.Test;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.io.IOException;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* 全国气象数据
|
|
*
|
|
* @author xiepufeng
|
|
*/
|
|
@Api(tags = "全国气象数据")
|
|
@RestController
|
|
@RequestMapping("/business/nmc")
|
|
public class DcNmcController {
|
|
|
|
/**
|
|
* 雷达数据
|
|
*/
|
|
@ApiOperation("雷达数据")
|
|
@GetMapping("/radar")
|
|
public AjaxResult radar() throws HttpException, IOException {
|
|
|
|
OkHttp okHttp = new OkHttp();
|
|
Response response // 请求响应
|
|
= okHttp
|
|
.url(UniversalEnum.RADAR_DATA.getValue()) // 请求地址
|
|
.get(); // 请求方法
|
|
|
|
String html = response.body().string();
|
|
|
|
return AjaxResult.success(parseHtmlContent(html));
|
|
}
|
|
|
|
|
|
/**
|
|
* 解析HTML内容,提取特定标签下的数据。
|
|
*
|
|
* @param htmlContent 要解析的HTML字符串。
|
|
* @return 返回一个JSONArray对象,包含解析得到的数据项。如果无法找到指定内容或解析失败,则返回空数组。
|
|
*/
|
|
public static JSONArray parseHtmlContent(String htmlContent) {
|
|
// 使用Jsoup解析HTML字符串
|
|
Document doc = Jsoup.parse(htmlContent, UniversalEnum.UTF_8.getValue());
|
|
|
|
// 尝试获取指定ID的元素
|
|
Element tabContent = doc.getElementById("myTabContent");
|
|
if (tabContent == null) {
|
|
// 如果找不到指定ID的元素,直接返回空数组
|
|
return new JSONArray();
|
|
}
|
|
|
|
// 获取目标元素列表
|
|
Elements targetElements = tabContent.child(UniversalEnum.ZERO.getNumber()).child(UniversalEnum.ZERO.getNumber()).children();
|
|
|
|
// 如果目标元素列表为空,同样返回空数组
|
|
if (targetElements.isEmpty()) {
|
|
return new JSONArray();
|
|
}
|
|
|
|
// 初始化结果数组
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
|
// 遍历目标元素列表,提取每项数据
|
|
for (Element targetElement : targetElements) {
|
|
// 提取"data-img"和"data-time"属性值
|
|
String dataImg = targetElement.attr("data-img");
|
|
String dataTime = targetElement.attr("data-time");
|
|
|
|
// 创建JSONObject,将提取到的数据项添加进去
|
|
JSONObject jsonObject = new JSONObject();
|
|
jsonObject.put("img", dataImg);
|
|
jsonObject.put("time", dataTime);
|
|
|
|
// 将JSONObject添加到结果数组
|
|
jsonArray.add(jsonObject);
|
|
}
|
|
|
|
// 返回填充好的结果数组
|
|
return jsonArray;
|
|
}
|
|
|
|
|
|
/**
|
|
* 雷达图
|
|
*/
|
|
@Test
|
|
@ApiOperation("雷达图")
|
|
@GetMapping("/radarMap")
|
|
public AjaxResult radarMap() throws HttpException, IOException, ParseException {
|
|
|
|
List<Map<String,Object>> result = new ArrayList<>();
|
|
|
|
Date now = new Date();
|
|
String url = UniversalEnum.RADAR_MAP.getValue() + now.getTime();
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
headers.put("Referer", UniversalEnum.RADAR_MAP_WEATHER.getValue());
|
|
|
|
OkHttp okHttp = new OkHttp();
|
|
Response response // 请求响应
|
|
= okHttp.headers(headers)
|
|
.url(url) // 请求地址
|
|
.get(); // 请求方法
|
|
String body = response.body().string();
|
|
if (StringUtils.isNotEmpty(body)){
|
|
/*var fc40 = {
|
|
"stime":"2024-06-21 13:50",
|
|
"value":[
|
|
{
|
|
"date":["2024062115"],
|
|
"path":[
|
|
"5m/QPFRef_202406211540.png",
|
|
"5m/QPFRef_202406211530.png",
|
|
"5m/QPFRef_202406211520.png",
|
|
"5m/QPFRef_202406211510.png",
|
|
"5m/QPFRef_202406211500.png"
|
|
],
|
|
"time":["1540","1530","1520","1510","1500"]
|
|
},
|
|
{
|
|
"date":["2024062114"],
|
|
"path":[
|
|
"5m/QPFRef_202406211450.png",
|
|
"5m/QPFRef_202406211440.png",
|
|
"5m/QPFRef_202406211430.png",
|
|
"5m/QPFRef_202406211420.png",
|
|
"5m/QPFRef_202406211410.png",
|
|
"5m/QPFRef_202406211400.png"],
|
|
"time":["1450","1440","1430","1420","1410","1400"]
|
|
},
|
|
...
|
|
...
|
|
]}*/
|
|
SimpleDateFormat sf = new SimpleDateFormat(UniversalEnum.TIME_FORMAT_ALL.getValue());
|
|
|
|
|
|
body = body.substring(UniversalEnum.ELEVEN.getNumber());
|
|
JSONObject jsonObject = JSON.parseObject(body);
|
|
List<Map<String,Object>> list = (List<Map<String, Object>>) jsonObject.get("value");
|
|
for (Map<String, Object> map : list) {
|
|
List<String> dateList = (List<String>) map.get("date");
|
|
String date = dateList.get(UniversalEnum.ZERO.getNumber()).substring(UniversalEnum.ZERO.getNumber(), dateList.get(UniversalEnum.ZERO.getNumber()).length() - UniversalEnum.TWO.getNumber());
|
|
|
|
List<String> pathList = (List<String>) map.get("path");
|
|
List<String> timeList = (List<String>) map.get("time");
|
|
for (int i = UniversalEnum.ZERO.getNumber(); i < timeList.size(); i++) {
|
|
String[] timeArr = {timeList.get(i).substring(UniversalEnum.ZERO.getNumber(),UniversalEnum.TWO.getNumber()),timeList.get(i).substring(UniversalEnum.TWO.getNumber())};
|
|
|
|
Map<String,Object> item = new HashMap<>();
|
|
item.put("time",date + UniversalEnum.BLANK_SPACE.getValue() + timeArr[UniversalEnum.ZERO.getNumber()] + UniversalEnum.IN_THE_COLON.getValue() + timeArr[UniversalEnum.ONE.getNumber()] + ":00");
|
|
item.put("path",UniversalEnum.RADAR_MAP_BACK.getValue() + pathList.get(i));
|
|
result.add(item);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
result = result.stream()
|
|
.sorted(Comparator.comparing(map -> map.get("time").toString()))
|
|
.collect(Collectors.toList());
|
|
return AjaxResult.success(result);
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|