Springboot整合邮件推送天气数据
下面是通过springboot整合邮件,然后获取天气数据,通过邮件推送天气数据。
1.Springboot整合邮件
导入邮件依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
写一个发送邮件的service
@Slf4j
@Service
public class MailSendService {
@Resource
private JavaMailSender javaMailSender;
/**
* 发送邮件
* @param sendMailDTO
*/
public boolean sendAttachmentMail(SendMailDTO sendMailDTO) {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(sendMailDTO.getFromMail());
helper.setTo(sendMailDTO.getToMail());
helper.setSubject(sendMailDTO.getTitle());
helper.setText(sendMailDTO.getContent());
javaMailSender.send(mimeMessage);
return true;
} catch (Exception e) {
log.error("sendAttachmentMail error:{}", e.getMessage(), e);
return false;
}
}
}
2.获取天气数据
总共四个接口:
- 获取所有城市id接口:https://j.i8tq.com/weather2020/search/city.js
- 获取实况天气数据:http://d1.weather.com.cn/sk_2d/101020100.html
- 获取当天天气数据接口:http://d1.weather.com.cn/dingzhi/101020100.html
- 获取当天天气指数接口:http://d1.weather.com.cn/zs_index/101020100.html
需要在头参数列表添加参数:Referer:http://www.weather.com.cn/。获取天气接口中城市id需要代码拼接。
编写获取天气数据工具类:
/**
* 获取所有城市AreaId
* @return
*/
public static List<CityInfoModel> getCityList(){
String cityjs="https://j.i8tq.com/weather2020/search/city.js";
//发送请求获取数据
String cityStr = HttpUtil.createGet(cityjs).header("Referer", "http://www.weather.com.cn/").execute().body();
//需要解析数据
String cityJson = cityStr.replace("var city_data =", "");
//创建集合存储数据
//使用 NAMECN:AREAID
JSONObject entries = JSONUtil.parseObj(cityJson);
Set<String> keys = entries.keySet();
List<CityInfoModel> cityInfoModelList = new ArrayList<>();
CityInfoModel cityInfoModel = null;
for (String key : keys) {
Set<String> citys = entries.getJSONObject(key).keySet();
for (String city : citys) {
Set<String> areas = entries.getJSONObject(key).getJSONObject(city).keySet();
for (String area : areas) {
JSONObject jsonObject = entries.getJSONObject(key).getJSONObject(city).getJSONObject(area);
cityInfoModel = new CityInfoModel();
cityInfoModel.setProvinceName(key);
cityInfoModel.setCityName(city);
cityInfoModel.setCountyName(jsonObject.getStr("NAMECN"));
cityInfoModel.setCityNumber(Math.toIntExact(jsonObject.getLong("AREAID")));
cityInfoModelList.add(cityInfoModel);
}
}
}
//把map数据转换成json
return cityInfoModelList;
}
/**
* 获取实时城市天气
* @param city
* @return
*/
public static JSONObject getRealWeather(String cityId){
//实时天气的地址
String REALWEATHER_URL="http://d1.weather.com.cn/sk_2d/";
//拼接请求地址
String uri=REALWEATHER_URL+cityId+".html";
String res = HttpUtil.createGet(uri).timeout(10000).header("Referer", "http://www.weather.com.cn/").execute().body();
//替换数据不然会影响json的解析
String json = res.replace("var dataSK=", "");
return JSONUtil.parseObj(json);
}
/**
* 获取当天城市天气
* @param city
* @return
*/
public static JSONObject getWeather(String cityId){
String WEATHER_URL="http://d1.weather.com.cn/dingzhi/";
String uri=WEATHER_URL+cityId+".html";
String res = HttpUtil.createGet(uri).timeout(10000).header("Referer", "http://www.weather.com.cn/").execute().body();
String resR = res.replace("var cityDZ"+cityId+" =", "");
String[] split = resR.split(";");
return JSONUtil.parseObj(split[0]).getJSONObject("weatherinfo");
}
/**
* 天气生活助手: 天气指数
* http://www.weather.com.cn/life/
* @return
*/
public static JSONObject getWeatherZs(String cityId){
String ZS_URL="http://d1.weather.com.cn/zs_index/";
String uri=ZS_URL+cityId+".html";
String res = HttpUtil.createGet(uri).timeout(10000).header("Referer", "http://www.weather.com.cn/").execute().body();
String resR = res.replace("var dataZS=", "");
return JSONUtil.parseObj(resR).getJSONObject("zs");
}
3.邮件推送
public void selectCity(String city){
JSONObject realWeather = WeatherUtil.getRealWeather(city);
JSONObject weather = WeatherUtil.getWeather(city);
JSONObject weatherZs = WeatherUtil.getWeatherZs(city);
StringBuilder str = new StringBuilder();
str.append("======实况天气=====\n")
.append(realWeather.getStr("cityname")).append("-天气:").append(realWeather.getStr("weather")).append("\t")
.append("温度:").append(realWeather.getStr("temp")).append("\t")
.append("湿度:").append(realWeather.getStr("sd")).append("\t")
.append("Pm2.5:").append(realWeather.getStr("aqi_pm25")).append("\n");
str.append("=====当天天气=====\n")
.append(weather.getStr("cityname")).append("-温度:").append(weather.getStr("tempn"))
.append("-").append(weather.getStr("temp")).append("\t")
.append("风向:").append(weather.getStr("wd")).append("\t")
.append("风速:").append(weather.getStr("ws")).append("\n");
str.append("温馨提示:").append(weather.getStr("cityname")).append("\n")
.append("紫外线:").append(weatherZs.getStr("uv_hint")).append("\n")
.append("运动指数:").append(weatherZs.getStr("yd_hint")).append("\n")
.append("干燥指数:").append(weatherZs.getStr("gz_des_s")).append("\n")
.append("约会指数:").append(weatherZs.getStr("yh_hint")).append("\n")
.append("理由:").append(weatherZs.getStr("yh_des_s")).append("\n");
log.info("===>info:{}",str.toString());
mailSendService.sendAttachmentMail(new SendMailDTO("天气预报-test",str.toString(),"****",new String[]{"****"}));
}
至此邮件推送天气数据功能完工!
部分代码出处:https://gitee.com/ItmeiCode/weather/tree/master/