|
@@ -23,70 +23,75 @@ import java.util.List;
|
|
|
public class XinZhiDownload {
|
|
|
|
|
|
public void download(Connection conn, String stationCode, String longitude, String latitude) {
|
|
|
- //逐日天气预报 三天
|
|
|
+ //逐日天气预报 三天 心知访问有的时候会报配额不够
|
|
|
String url = "https://api.seniverse.com/v3/weather/daily.json?key=Sw6CStXIxKnEw1uSV&location=" + latitude + ":" + longitude + "&language=zh-Hans&unit=c&start=-1&days=5";
|
|
|
//天气实况 免费用户只有个气温 没用
|
|
|
//String url = "https://api.seniverse.com/v3/weather/now.json?key=Sw6CStXIxKnEw1uSV&location=47.0519:131.7097&language=zh-Hans&unit=c";
|
|
|
String body = HttpUtil.createGet(url).execute().charset("utf-8").body();
|
|
|
JSONObject jsonObject = JSONUtil.parseObj(body);
|
|
|
JSONArray result = JSONUtil.parseArray(jsonObject.get("results"));
|
|
|
- JSONObject all = (JSONObject) result.get(0);
|
|
|
- JSONArray daily = JSONUtil.parseArray(all.get("daily"));
|
|
|
- List<XinZhi> list = new ArrayList<>();
|
|
|
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- if (!daily.isEmpty()) {
|
|
|
- for (int i = 0; i < daily.size(); i++) {
|
|
|
- JSONObject jsonObject1 = (JSONObject) daily.get(i);
|
|
|
- XinZhi xinZhi = new XinZhi();
|
|
|
- xinZhi.setStationCode(stationCode);
|
|
|
- xinZhi.setLongitude(longitude);
|
|
|
- xinZhi.setLatitude(latitude);
|
|
|
- xinZhi.setDate((String) jsonObject1.get("date"));
|
|
|
- xinZhi.setDirection((String) jsonObject1.get("wind_direction"));
|
|
|
- xinZhi.setDirectionDegree((String) jsonObject1.get("wind_direction_degree"));
|
|
|
- xinZhi.setHumidity((String) jsonObject1.get("humidity"));
|
|
|
- xinZhi.setHigh((String) jsonObject1.get("high"));
|
|
|
- xinZhi.setLow((String) jsonObject1.get("low"));
|
|
|
- xinZhi.setScale((String) jsonObject1.get("wind_scale"));
|
|
|
- xinZhi.setSpeed((String) jsonObject1.get("wind_speed"));
|
|
|
- xinZhi.setRainfall((String) jsonObject1.get("rainfall"));
|
|
|
- list.add(xinZhi);
|
|
|
- }
|
|
|
+ if (!result.isEmpty()) {
|
|
|
+ JSONObject all = (JSONObject) result.get(0);
|
|
|
+ JSONArray daily = JSONUtil.parseArray(all.get("daily"));
|
|
|
+ List<XinZhi> list = new ArrayList<>();
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ if (!daily.isEmpty()) {
|
|
|
+ for (int i = 0; i < daily.size(); i++) {
|
|
|
+ JSONObject jsonObject1 = (JSONObject) daily.get(i);
|
|
|
+ XinZhi xinZhi = new XinZhi();
|
|
|
+ xinZhi.setStationCode(stationCode);
|
|
|
+ xinZhi.setLongitude(longitude);
|
|
|
+ xinZhi.setLatitude(latitude);
|
|
|
+ xinZhi.setDate((String) jsonObject1.get("date"));
|
|
|
+ xinZhi.setDirection((String) jsonObject1.get("wind_direction"));
|
|
|
+ xinZhi.setDirectionDegree((String) jsonObject1.get("wind_direction_degree"));
|
|
|
+ xinZhi.setHumidity((String) jsonObject1.get("humidity"));
|
|
|
+ xinZhi.setHigh((String) jsonObject1.get("high"));
|
|
|
+ xinZhi.setLow((String) jsonObject1.get("low"));
|
|
|
+ xinZhi.setScale((String) jsonObject1.get("wind_scale"));
|
|
|
+ xinZhi.setSpeed((String) jsonObject1.get("wind_speed"));
|
|
|
+ xinZhi.setRainfall((String) jsonObject1.get("rainfall"));
|
|
|
+ list.add(xinZhi);
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
- if (!list.isEmpty()) {
|
|
|
- String tableSql = "INSERT ignore INTO `xinzhi` ( `stationCode`, `longitude`, `latitude`, `date`, `direction`, `direction_degree`, `speed`, `scale`, `humidity`, `high`, `low`, `rainfall`,`insert_date`) VALUES" +
|
|
|
- " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);";
|
|
|
- try {
|
|
|
- PreparedStatement pstmt = conn.prepareStatement(tableSql);
|
|
|
- conn.setAutoCommit(false);
|
|
|
- log.info("心知天气:共{}天记录", list.size());
|
|
|
- list.forEach(item -> {
|
|
|
- try {
|
|
|
- pstmt.setString(1, item.getStationCode());
|
|
|
- pstmt.setString(2, item.getLongitude());
|
|
|
- pstmt.setString(3, item.getLatitude());
|
|
|
- pstmt.setString(4, item.getDate());
|
|
|
- pstmt.setString(5, item.getDirection());
|
|
|
- pstmt.setString(6, item.getDirectionDegree());
|
|
|
- pstmt.setString(7, new BigDecimal(item.getSpeed()).divide(new BigDecimal("3.6"), 2, RoundingMode.HALF_UP).toString());
|
|
|
- pstmt.setString(8, item.getScale());
|
|
|
- pstmt.setString(9, item.getHumidity());
|
|
|
- pstmt.setString(10, item.getHigh());
|
|
|
- pstmt.setString(11, item.getLow());
|
|
|
- pstmt.setString(12, item.getRainfall());
|
|
|
- pstmt.setString(13, df.format(new Date()));
|
|
|
- pstmt.addBatch();
|
|
|
- } catch (SQLException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- });
|
|
|
- pstmt.executeBatch();
|
|
|
- conn.commit();
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
}
|
|
|
- log.info("场站编号:{} 心知天气数据下载完成", stationCode);
|
|
|
+ if (!list.isEmpty()) {
|
|
|
+ String tableSql = "INSERT ignore INTO `xinzhi` ( `stationCode`, `longitude`, `latitude`, `date`, `direction`, `direction_degree`, `speed`, `scale`, `humidity`, `high`, `low`, `rainfall`,`insert_date`) VALUES" +
|
|
|
+ " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);";
|
|
|
+ try {
|
|
|
+ PreparedStatement pstmt = conn.prepareStatement(tableSql);
|
|
|
+ conn.setAutoCommit(false);
|
|
|
+ log.info("心知天气:共{}天记录", list.size());
|
|
|
+ list.forEach(item -> {
|
|
|
+ try {
|
|
|
+ pstmt.setString(1, item.getStationCode());
|
|
|
+ pstmt.setString(2, item.getLongitude());
|
|
|
+ pstmt.setString(3, item.getLatitude());
|
|
|
+ pstmt.setString(4, item.getDate());
|
|
|
+ pstmt.setString(5, item.getDirection());
|
|
|
+ pstmt.setString(6, item.getDirectionDegree());
|
|
|
+ pstmt.setString(7, new BigDecimal(item.getSpeed()).divide(new BigDecimal("3.6"), 2, RoundingMode.HALF_UP).toString());
|
|
|
+ pstmt.setString(8, item.getScale());
|
|
|
+ pstmt.setString(9, item.getHumidity());
|
|
|
+ pstmt.setString(10, item.getHigh());
|
|
|
+ pstmt.setString(11, item.getLow());
|
|
|
+ pstmt.setString(12, item.getRainfall());
|
|
|
+ pstmt.setString(13, df.format(new Date()));
|
|
|
+ pstmt.addBatch();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ pstmt.executeBatch();
|
|
|
+ conn.commit();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ log.info("场站编号:{} 心知天气数据下载完成", stationCode);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("场站编号:{} 没有获取到心知天气数据", stationCode);
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|