浏览代码

日的气象数据 报错了也继续接。 修改log 报错的时候打印请求内容

xiaowang 3 天之前
父节点
当前提交
9ee3af7519
共有 1 个文件被更改,包括 25 次插入17 次删除
  1. 25 17
      src/main/java/com/example/getweather/service/GetWeatherGeovisearth.java

+ 25 - 17
src/main/java/com/example/getweather/service/GetWeatherGeovisearth.java

@@ -28,6 +28,8 @@ import java.time.LocalDateTime;
 import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 @Service
@@ -124,8 +126,8 @@ public class GetWeatherGeovisearth {
         }
 
         // 使用正则表达式匹配数字
-        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("(\\d+)");
-        java.util.regex.Matcher matcher = pattern.matcher(levelStr);
+        Pattern pattern = Pattern.compile("(\\d+)");
+        Matcher matcher = pattern.matcher(levelStr);
 
         if (matcher.find()) {
             return Integer.parseInt(matcher.group(1));
@@ -184,11 +186,11 @@ public class GetWeatherGeovisearth {
             for (String code : areaCodes) {
                 //中科星图 只接甘肃
                 if (code.equals("620000")) {
-                    if (isApiQuotaExhausted) break; // 检查标志变量
+                    //if (isApiQuotaExhausted) break; // 检查标志变量
                     List<DistrictCodeDO> shiList = districtCodeDOList.stream().filter(item -> item.getPid().toString().equals(code)).collect(Collectors.toList());
                     //市
                     for (DistrictCodeDO d : shiList) {
-                        if (isApiQuotaExhausted) break; // 检查标志变量
+                        //if (isApiQuotaExhausted) break; // 检查标志变量
                         List<DistrictCodeDO> quList = districtCodeDOList.stream().filter(item -> item.getPid().toString().equals(d.getCode())).collect(Collectors.toList());
                         String url;
                         String url120;
@@ -199,16 +201,16 @@ public class GetWeatherGeovisearth {
                         url120 = "https://api.open.geovisearth.com/v2/cn/hourly/professional?token=" + token + "&location=" + location;
                         xmoUrl = "https://weather-api.xm-opt.com/v1/forecastDaily?latitude=" + d.getLat() + "&longitude=" + d.getLng() + "&daily=wind_speed_10m_max,shortwave_radiation_sum&timezone=Asia%2FShanghai&start_date=" + startDate + "&end_date=" + endDate;
                         isApiQuotaExhausted = analysisGeovisearth(url, url120, xmoUrl, d.getCode(), d.getName(), conn); // 传递标志变量
-                        if (isApiQuotaExhausted) break; // 检查标志变量
+                        //if (isApiQuotaExhausted) break; // 检查标志变量
                         //区
                         for (DistrictCodeDO q : quList) {
-                            if (isApiQuotaExhausted) break; // 检查标志变量
+                            //if (isApiQuotaExhausted) break; // 检查标志变量
                             location = q.getLng() + "," + q.getLat();
                             url = "https://api.open.geovisearth.com/v2/cn/city/professional?token=" + token + "&location=" + location + "&yesterday=false";
                             url120 = "https://api.open.geovisearth.com/v2/cn/hourly/professional?token=" + token + "&location=" + location;
                             xmoUrl = "https://weather-api.xm-opt.com/v1/forecastDaily?latitude=" + q.getLat() + "&longitude=" + q.getLng() + "&daily=wind_speed_10m_max,shortwave_radiation_sum&timezone=Asia%2FShanghai&start_date=" + startDate + "&end_date=" + endDate;
                             isApiQuotaExhausted = analysisGeovisearth(url, url120, xmoUrl, q.getCode(), q.getName(), conn); // 传递标志变量
-                            if (isApiQuotaExhausted) break; // 检查标志变量
+                            //if (isApiQuotaExhausted) break; // 检查标志变量
                             //线程睡5秒 防止请求过快
                             //花钱了 硬气 不管他
                             //Thread.sleep(5000);
@@ -220,16 +222,16 @@ public class GetWeatherGeovisearth {
                         String url = "https://api.open.geovisearth.com/v2/cn/city/professional?token=" + token + "&location=" + location + "&yesterday=false";
                         String url120 = "https://api.open.geovisearth.com/v2/cn/hourly/professional?token=" + token + "&location=" + location;
                         String xmoUrl = "https://weather-api.xm-opt.com/v1/forecastDaily?latitude=" + d.getLat() + "&longitude=" + d.getLng() + "&daily=wind_speed_10m_max,shortwave_radiation_sum&timezone=Asia%2FShanghai&start_date=" + startDate + "&end_date=" + endDate;
-                        if (isApiQuotaExhausted) break; // 检查标志变量
+                        //if (isApiQuotaExhausted) break; // 检查标志变量
                         isApiQuotaExhausted = analysisGeovisearth(url, url120, xmoUrl, d.getCode(), d.getName(), conn); // 传递标志变量
-                        if (isApiQuotaExhausted) break; // 检查标志变量
+                        //if (isApiQuotaExhausted) break; // 检查标志变量
                     }
                 } else {
-                    if (isApiQuotaExhausted) break; // 检查标志变量
+                    //if (isApiQuotaExhausted) break; // 检查标志变量
                     List<DistrictCodeDO> shiList = districtCodeDOList.stream().filter(item -> item.getPid().toString().equals(code)).collect(Collectors.toList());
                     //市
                     for (DistrictCodeDO d : shiList) {
-                        if (isApiQuotaExhausted) break; // 检查标志变量
+                        //if (isApiQuotaExhausted) break; // 检查标志变量
                         List<DistrictCodeDO> quList = districtCodeDOList.stream().filter(item -> item.getPid().toString().equals(d.getCode())).collect(Collectors.toList());
                         String url;
                         String url120;
@@ -240,16 +242,16 @@ public class GetWeatherGeovisearth {
                         url120 = "https://api.open.geovisearth.com/v2/cn/hourly/professional?token=" + token + "&location=" + location;
                         xmoUrl = "https://weather-api.xm-opt.com/v1/forecastDaily?latitude=" + d.getLat() + "&longitude=" + d.getLng() + "&daily=wind_speed_10m_max,shortwave_radiation_sum&timezone=Asia%2FShanghai&start_date=" + startDate + "&end_date=" + endDate;
                         isApiQuotaExhausted = analysisGeovisearth(url, url120, xmoUrl, d.getCode(), d.getName(), conn); // 传递标志变量
-                        if (isApiQuotaExhausted) break; // 检查标志变量
+                        //if (isApiQuotaExhausted) break; // 检查标志变量
                         //区
                         for (DistrictCodeDO q : quList) {
-                            if (isApiQuotaExhausted) break; // 检查标志变量
+                            //if (isApiQuotaExhausted) break; // 检查标志变量
                             location = q.getLng() + "," + q.getLat();
                             url = "https://api.open.geovisearth.com/v2/cn/city/professional?token=" + token + "&location=" + location + "&yesterday=false";
                             url120 = "https://api.open.geovisearth.com/v2/cn/hourly/professional?token=" + token + "&location=" + location;
                             xmoUrl = "https://weather-api.xm-opt.com/v1/forecastDaily?latitude=" + q.getLat() + "&longitude=" + q.getLng() + "&daily=wind_speed_10m_max,shortwave_radiation_sum&timezone=Asia%2FShanghai&start_date=" + startDate + "&end_date=" + endDate;
                             isApiQuotaExhausted = analysisGeovisearth(url, url120, xmoUrl, q.getCode(), q.getName(), conn); // 传递标志变量
-                            if (isApiQuotaExhausted) break; // 检查标志变量
+                            //if (isApiQuotaExhausted) break; // 检查标志变量
                             //线程睡5秒 防止请求过快
                             //花钱了 硬气 不管他
                             //Thread.sleep(5000);
@@ -286,15 +288,18 @@ public class GetWeatherGeovisearth {
      * @return
      */
     public boolean analysisGeovisearth(String url, String url120, String xmoUrl, String areaCode, String name, Connection conn) {
+        JSONObject jsonObject = new JSONObject();
+        JSONObject jsonObject120 = new JSONObject();
+        JSONObject jsonObjectXmo = new JSONObject();
         log.info("开始下载地区:{} 的天气数据", name);
         try {
             Statement ps = conn.createStatement();
             String body = HttpUtil.createGet(url).execute().charset("utf-8").body();
             String body120 = HttpUtil.createGet(url120).execute().charset("utf-8").body();
-            JSONObject jsonObject = JSONUtil.parseObj(body);
-            JSONObject jsonObject120 = JSONUtil.parseObj(body120);
+            jsonObject = JSONUtil.parseObj(body);
+            jsonObject120 = JSONUtil.parseObj(body120);
             String bodyXmo = HttpUtil.createGet(xmoUrl).header("apikey", "c615a8f6-f5dd-4a81-86fe-db2a42862c15").execute().charset("utf-8").body();
-            JSONObject jsonObjectXmo = JSONUtil.parseObj(bodyXmo);
+            jsonObjectXmo = JSONUtil.parseObj(bodyXmo);
             //日数据 7d
             JSONObject xmoDaily = JSONUtil.parseObj(jsonObjectXmo.get("daily"));
             //日期
@@ -561,6 +566,9 @@ public class GetWeatherGeovisearth {
             if (ps != null)
                 ps.close();
         } catch (Exception e) {
+            log.error("星图  " + jsonObject);
+            log.error("星图120  " + jsonObject120);
+            log.error("曦谋  " + jsonObjectXmo);
             log.error("星图天气,地区:{} 日天气数据下载失败JSON格式化错误", name);
             return true;
         }