代理方式获取天气预报信息
中央气象台及图片下载网站:
http://blog.sina.com.cn/s/blog_7bac470701014tbe.html
https://wenku.baidu.com/view/822316737fd5360cba1adb80.html
局域网部署,使用代理方式获取天气预报信息:
## 气象编码同步
fire.weather.code.url=http://www.nmc.cn/f/rest/province/
fire.weather.codes=ABJ,ATJ,AHE,ASX,ANM,ALN,AJL,AHL,ASH,AJS,AZJ,AAH,AFJ,AJX,ASD,AHA,AHB,AHN,AGD,AGX,AHI,ACQ,ASC,AGZ,AYN,AXZ,ASN,AGS,AQH,ANX,AXJ,AXG,AAM,ATW
## 中央气象台官网 实时天气
fire.weather.url=http://www.nmc.cn/f/rest/real
## 定时任务是否抓取气象数据
fire.weather.switch=true
## 气象编码同步(凭祥)
fire.station.code=59419
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.gsafety.firefighting.service.impl; import static com.google.common.collect.Iterables.toArray; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Proxy; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; 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.util.EntityUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.base.Strings; import com.gsafety.firefighting.po.WeatherInfo; import com.gsafety.firefighting.repository.IWeatherInfoRepository; import com.gsafety.firefighting.service.WeatherInfoService; @Service public class WeatherInfoServiceImpl implements WeatherInfoService { private Logger logger = LoggerFactory.getLogger(WeatherInfoServiceImpl.class); private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @Value("${fire.weather.url}") private String weatherUrl; @Value("${fire.weather.code.url}") private String weatheCodeUrl; @Value("${fire.station.code}") private String stationCode; @Value("${http_proxy_ip}") private String httpProxyIp; @Value("${http_proxy_port}") private Integer httpProxyPort; @Autowired private IWeatherInfoRepository weatherInfoRepository; @Autowired private JdbcTemplate jdbcTemplate; /** * 根据气象编号获取实时天气 */ @Override public void catchWeatherByCode(String weatherCode) { JSONObject realJson = new JSONObject(); String postUrl = weatherUrl + "/" + weatherCode + "?_" + System.currentTimeMillis(); HttpClientBuilder builder = HttpClientBuilder.create(); CloseableHttpClient client = builder.build(); HttpGet get = new HttpGet(postUrl); get.addHeader("Accept", "application/json"); get.addHeader("Content-Type", "application/json;charset=utf-8"); if(!Strings.isNullOrEmpty(httpProxyIp)&&null!=httpProxyPort){ HttpHost proxy = new HttpHost(httpProxyIp, httpProxyPort, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); get.setConfig(config); } CloseableHttpResponse response = null; HttpEntity entity = null; String rtnMsg = ""; try { response = client.execute(get); entity = response.getEntity(); rtnMsg = EntityUtils.toString(entity,"UTF-8"); realJson = JSONObject.parseObject(rtnMsg); logger.info("中央气象台官网接口调用成功!"); JSONObject stationJson = realJson.getJSONObject("station"); JSONObject weatherJson = realJson.getJSONObject("weather"); JSONObject windJson = realJson.getJSONObject("wind"); String stationCode = stationJson.getString("code"); String stationCity = stationJson.getString("city"); String staticonProvince = stationJson.getString("province"); String publishTime = realJson.getString("publish_time"); String weatherTemperature = weatherJson.getString("temperature"); String weatherTemperaturediff = weatherJson.getString("temperatureDiff"); String weatherAirpressure = weatherJson.getString("airpressure"); String weatherHumidity = weatherJson.getString("humidity"); String weatherRain = weatherJson.getString("rain"); String weatherRcomfort = weatherJson.getString("rcomfort"); String weatherIcomfort = weatherJson.getString("icomfort"); String weatherInfo = weatherJson.getString("info"); String weatherImg = weatherJson.getString("img"); String weatherFeelst = weatherJson.getString("feelst"); String windDirect = windJson.getString("direct"); String windPower = windJson.getString("power"); String windSpeed = windJson.getString("speed"); WeatherInfo info = new WeatherInfo(); info.setStationCode(stationCode); info.setStationCity(stationCity); info.setStaticonProvince(staticonProvince); info.setPublishTime(SDF.parse(publishTime)); info.setWeatherTemperature(weatherTemperature); info.setWeatherTemperatureDiff(weatherTemperaturediff); info.setWeatherAirpressure(weatherAirpressure); info.setWeatherHumidity(weatherHumidity); info.setWeatherRain(weatherRain); info.setWeatherRcomfort(weatherRcomfort); info.setWeatherIcomfort(weatherIcomfort); info.setWeatherInfo(weatherInfo); info.setWeatherImg(weatherImg); info.setWeatherFeelst(weatherFeelst); info.setWindDirect(windDirect); info.setWindPower(windPower); info.setWindSpeed(windSpeed); info.setCreateTime(new Date()); info.setLastUpdateTime(new Date()); weatherInfoRepository.saveAndFlush(info); } catch (IOException e) { logger.error("抓取失败", e); } catch (ParseException e) { logger.error("日期转换失败", e); } } /** * 抓取组织机构 有行政区划编码的,且行政区划编码有对应气象编码的数据 */ @Override public void catchWeatherAll() { try { StringBuffer sb = new StringBuffer(); sb.append(" select distinct ct.weather_code as weatherCode from code_bas_district ct ") .append(" where ct.weather_code is not null and ct.weather_code <>'' "); List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString()); for (Map<String, Object> map : list) { String weatherCode = (String)map.get("weatherCode"); catchWeatherByCode(weatherCode); } }catch(Exception e) { logger.error("",e); throw new RuntimeException("数据抓取失败catchWeatherAll()",e); } } /** * 同步气象编码 */ @Override public void syncWeatherCode(String[] provinceCodeArr) { try { for (String provinceCod : provinceCodeArr) { Document doc = Jsoup.connect(weatheCodeUrl + "/" + provinceCod).ignoreContentType(true).get(); Elements elements = Jsoup.parse(doc.toString()).select("body"); for (Element element : elements) { String text = element.text(); JSONArray jsonArr = JSON.parseArray(text); updateWeatherCode(jsonArr); } } }catch(Exception e) { logger.error("",e); } } private void updateWeatherCode(JSONArray jsonArr) { List<String> sqlList = new ArrayList<>(); for (Object object : jsonArr) { JSONObject json = (JSONObject)object; String code = json.getString("code"); String city = json.getString("city"); sqlList.add(" update code_bas_district t set t.weather_code='"+code+"',t.weather_city='"+city+"' where t.dist_name like '%"+city+"%' "); } jdbcTemplate.batchUpdate(toArray(sqlList,String.class)); } /* * 根据气象站点号,获取实时天气(non-Javadoc) */ @Override public JSONObject getweatherdata(String weatherCode) { List<WeatherInfo> weatherInfoList = weatherInfoRepository.queryWeatherInfoByStationCode(weatherCode); JSONObject result = new JSONObject(); if(null!=weatherInfoList && weatherInfoList.size()>0) { WeatherInfo weatherInfo = weatherInfoList.get(0); result.put("cityname", weatherInfo.getStationCity()==null?"":weatherInfo.getStationCity()); result.put("city", weatherInfo.getStationCode()==null?"":weatherInfo.getStationCode()); result.put("temp", weatherInfo.getWeatherTemperature()==null?"":weatherInfo.getWeatherTemperature()); result.put("tempf", weatherInfo.getWeatherTemperatureDiff()==null?"":weatherInfo.getWeatherTemperatureDiff()); result.put("WD", weatherInfo.getWindDirect()==null?"": weatherInfo.getWindDirect()); result.put("WS", weatherInfo.getWindPower()==null?"":weatherInfo.getWindPower()); result.put("wse", weatherInfo.getWindSpeed()==null?"":weatherInfo.getWindSpeed()); result.put("weather", weatherInfo.getWeatherInfo()==null?"":weatherInfo.getWeatherInfo()); result.put("weathercode", weatherInfo.getWeatherImg()==null?"":weatherInfo.getWeatherImg()); }else { result.put("cityname", ""); result.put("city", ""); result.put("temp", ""); result.put("tempf", ""); result.put("WD", ""); result.put("WS", ""); result.put("wse", ""); result.put("weather", ""); result.put("weathercode", ""); } return result; } }