186 lines
6.1 KiB
Plaintext
186 lines
6.1 KiB
Plaintext
package com.securityControl.task.service.impl;
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.securityControl.task.mapper.WatherDao;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* 天气预警抓取
|
|
*/
|
|
@Service
|
|
public class WatherWarnService {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(WatherWarnService.class);
|
|
/**
|
|
* 搜索城市
|
|
*/
|
|
static String CITY = "合肥市@阜阳市@安庆市@滁州市@六安市@宿州市@宣城市@芜湖市@池州市@淮南市@黄山市@蚌埠市@亳州市@淮北市@铜陵市@马鞍山市";
|
|
|
|
/**
|
|
* 所有城市灾害预警列表
|
|
*/
|
|
final static String ALL_CITY_URL = "http://product.weather.com.cn/alarm/grepalarm_cn.php";
|
|
|
|
/**
|
|
* 搜索城市灾害预警数据页面
|
|
*/
|
|
final static String VALUE_URL = "http://product.weather.com.cn/alarm/webdata/";
|
|
/**
|
|
* 时间戳
|
|
*/
|
|
static long TIME_STAMP = System.currentTimeMillis();
|
|
|
|
@Autowired
|
|
private WatherDao dao;
|
|
|
|
/**
|
|
* 数据信息接口
|
|
* @param
|
|
*/
|
|
public void getCityWather(){
|
|
try{
|
|
String url = ALL_CITY_URL + "?_=" + TIME_STAMP;
|
|
String allWarningCity = getWarnCityList(url);
|
|
if (allWarningCity == null) {
|
|
return;
|
|
}
|
|
List<List<String>> data = getCityLists(allWarningCity);
|
|
List<String> citys=Arrays.asList(CITY.split("@"));
|
|
for (String city:citys) {
|
|
String realUrl = getSearchCityUrl(data,city);
|
|
if (realUrl == null || "".equals(realUrl)) {
|
|
dao.delWatherWrn(city);
|
|
} else {
|
|
String wholeUrl = VALUE_URL + realUrl + "?_=" + TIME_STAMP;
|
|
Map<String, String> map = dealNewUrl(wholeUrl);
|
|
System.out.println("预警内容: "+map.get("ISSUECONTENT")+"类型:"+map.get("SIGNALTYPE")+"等级:"+map.get("SIGNALLEVEL"));
|
|
String time=map.get("ISSUETIME");//时间
|
|
String content=map.get("ISSUECONTENT").split("(预警信息来源")[0];
|
|
String type=map.get("SIGNALTYPE")+"预警";
|
|
String level=map.get("SIGNALLEVEL");
|
|
Integer count=dao.getIsCz(city,time);
|
|
dao.delWatherWrn(city);
|
|
if(count>0){
|
|
dao.updateWarther(city,time);
|
|
}else{
|
|
dao.insertWatherWarn(city,content,type,level,time);
|
|
}
|
|
}
|
|
}
|
|
}catch (Exception e){
|
|
log.error(e.toString(),e);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 处理所有城市灾害列表页面
|
|
*
|
|
* @param url
|
|
* @return
|
|
*/
|
|
public static String getWarnCityList(String url) {
|
|
String allWeatherInfo = null;
|
|
CloseableHttpClient client;
|
|
client = HttpClientBuilder.create().build();
|
|
|
|
HttpGet get = new HttpGet(url);
|
|
RequestConfig config = RequestConfig.custom()
|
|
.setConnectTimeout(10*1000) //连接超时时间
|
|
.setConnectionRequestTimeout(6000) //从连接池中取的连接的最长时间
|
|
.setSocketTimeout(2*60*1000) //数据传输的超时时间
|
|
.build();
|
|
get.setConfig(config);
|
|
HttpResponse response;
|
|
try {
|
|
response = client.execute(get);
|
|
if (response != null) {
|
|
HttpEntity entity = response.getEntity();
|
|
allWeatherInfo = EntityUtils.toString(entity, "UTF-8");
|
|
} else {
|
|
System.out.println("全国所有城市都没有预警或者中国预警网错误");
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return allWeatherInfo;
|
|
}
|
|
|
|
/**
|
|
* 获得城市列表
|
|
* @param allWeatherInfo
|
|
* @return
|
|
*/
|
|
private static List<List<String>> getCityLists(String allWeatherInfo) {
|
|
String[] split = allWeatherInfo.split("=");
|
|
String value = split[1];
|
|
String substring = value.substring(0, value.length() - 1);
|
|
Map<String, List<List<String>>> jsonMap = JSON.parseObject(substring, Map.class);
|
|
return jsonMap.get("data");
|
|
}
|
|
|
|
/**
|
|
* 得到搜索城市的url
|
|
*
|
|
* @param data
|
|
* @return
|
|
*/
|
|
public static String getSearchCityUrl(List<List<String>> data, String city) {
|
|
String realUrl = "";
|
|
List<List<String>> sortedList = data.stream()
|
|
.filter(strings -> strings.get(0).contains(city))
|
|
.sorted(Comparator.comparing(s -> s.get(0).length()))
|
|
.limit(1)
|
|
.collect(Collectors.toList());
|
|
if (sortedList.isEmpty()) {
|
|
return realUrl;
|
|
}
|
|
realUrl = sortedList.get(0).get(1);
|
|
return realUrl;
|
|
}
|
|
|
|
/**
|
|
* 访问城市url
|
|
*
|
|
* @param url
|
|
* @return
|
|
*/
|
|
public static Map<String, String> dealNewUrl(String url) {
|
|
Map<String, String> map=new HashMap<>(16);
|
|
CloseableHttpClient client;
|
|
client = HttpClients.createDefault();
|
|
HttpGet get = new HttpGet(url);
|
|
HttpResponse response;
|
|
try {
|
|
response = client.execute(get);
|
|
HttpEntity entity = response.getEntity();
|
|
if (entity != null) {
|
|
String string = EntityUtils.toString(entity, "UTF-8");
|
|
// System.out.println("搜索城市数据: " + string);
|
|
String[] split = string.split("=");
|
|
String s = split[1];
|
|
map = JSON.parseObject(s, Map.class);
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return map;
|
|
}
|
|
|
|
}
|