XinZhiDownload.java 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.example.mdd.service;
  2. import cn.hutool.http.HttpUtil;
  3. import cn.hutool.json.JSONArray;
  4. import cn.hutool.json.JSONObject;
  5. import cn.hutool.json.JSONUtil;
  6. import com.example.mdd.entity.XinZhi;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.stereotype.Service;
  9. import java.math.BigDecimal;
  10. import java.math.RoundingMode;
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.SQLException;
  14. import java.text.SimpleDateFormat;
  15. import java.util.ArrayList;
  16. import java.util.Date;
  17. import java.util.List;
  18. @Service
  19. @Slf4j
  20. public class XinZhiDownload {
  21. public void download(Connection conn, String stationCode, String longitude, String latitude) {
  22. //逐日天气预报 三天
  23. String url = "https://api.seniverse.com/v3/weather/daily.json?key=Sw6CStXIxKnEw1uSV&location=" + latitude + ":" + longitude + "&language=zh-Hans&unit=c&start=-1&days=5";
  24. //天气实况 免费用户只有个气温 没用
  25. //String url = "https://api.seniverse.com/v3/weather/now.json?key=Sw6CStXIxKnEw1uSV&location=47.0519:131.7097&language=zh-Hans&unit=c";
  26. String body = HttpUtil.createGet(url).execute().charset("utf-8").body();
  27. JSONObject jsonObject = JSONUtil.parseObj(body);
  28. JSONArray result = JSONUtil.parseArray(jsonObject.get("results"));
  29. JSONObject all = (JSONObject) result.get(0);
  30. JSONArray daily = JSONUtil.parseArray(all.get("daily"));
  31. List<XinZhi> list = new ArrayList<>();
  32. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  33. if (!daily.isEmpty()) {
  34. for (int i = 0; i < daily.size(); i++) {
  35. JSONObject jsonObject1 = (JSONObject) daily.get(i);
  36. XinZhi xinZhi = new XinZhi();
  37. xinZhi.setStationCode(stationCode);
  38. xinZhi.setLongitude(longitude);
  39. xinZhi.setLatitude(latitude);
  40. xinZhi.setDate((String) jsonObject1.get("date"));
  41. xinZhi.setDirection((String) jsonObject1.get("wind_direction"));
  42. xinZhi.setDirectionDegree((String) jsonObject1.get("wind_direction_degree"));
  43. xinZhi.setHumidity((String) jsonObject1.get("humidity"));
  44. xinZhi.setHigh((String) jsonObject1.get("high"));
  45. xinZhi.setLow((String) jsonObject1.get("low"));
  46. xinZhi.setScale((String) jsonObject1.get("wind_scale"));
  47. xinZhi.setSpeed((String) jsonObject1.get("wind_speed"));
  48. xinZhi.setRainfall((String) jsonObject1.get("rainfall"));
  49. list.add(xinZhi);
  50. }
  51. }
  52. if (!list.isEmpty()) {
  53. String tableSql = "INSERT ignore INTO `xinzhi` ( `stationCode`, `longitude`, `latitude`, `date`, `direction`, `direction_degree`, `speed`, `scale`, `humidity`, `high`, `low`, `rainfall`,`insert_date`) VALUES" +
  54. " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);";
  55. try {
  56. PreparedStatement pstmt = conn.prepareStatement(tableSql);
  57. conn.setAutoCommit(false);
  58. log.info("心知天气:共{}天记录", list.size());
  59. list.forEach(item -> {
  60. try {
  61. pstmt.setString(1, item.getStationCode());
  62. pstmt.setString(2, item.getLongitude());
  63. pstmt.setString(3, item.getLatitude());
  64. pstmt.setString(4, item.getDate());
  65. pstmt.setString(5, item.getDirection());
  66. pstmt.setString(6, item.getDirectionDegree());
  67. pstmt.setString(7, new BigDecimal(item.getSpeed()).divide(new BigDecimal("3.6"), 2, RoundingMode.HALF_UP).toString());
  68. pstmt.setString(8, item.getScale());
  69. pstmt.setString(9, item.getHumidity());
  70. pstmt.setString(10, item.getHigh());
  71. pstmt.setString(11, item.getLow());
  72. pstmt.setString(12, item.getRainfall());
  73. pstmt.setString(13, df.format(new Date()));
  74. pstmt.addBatch();
  75. } catch (SQLException e) {
  76. throw new RuntimeException(e);
  77. }
  78. });
  79. pstmt.executeBatch();
  80. conn.commit();
  81. } catch (Exception e) {
  82. throw new RuntimeException(e);
  83. }
  84. log.info("场站编号:{} 心知天气数据下载完成", stationCode);
  85. }
  86. }
  87. }