Guest User

Untitled

a guest
Dec 1st, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.02 KB | None | 0 0
  1. package org.example;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.nio.charset.StandardCharsets;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12. import java.util.Iterator;
  13. import java.util.Objects;
  14. import org.json.JSONArray;
  15. import org.json.JSONObject;
  16.  
  17. public class Main {
  18.  
  19.     public static final String FOLDER_PATH = "src/main/resources/";
  20.     public static final String BASE_JSON = FOLDER_PATH + "숙박정보조회.txt";
  21.     public static final String SERVICE_KEY = "tn2EFIkIHocOuW2a6abdTLb3OO%2FP49vXjVrlGkQ5IP0aSicl%2BiLpXYVjb9yE%2BeRpXJqCd1zOIpyVZjPMoSkUpQ%3D%3D";
  22.  
  23.     public static void main(String[] args) throws IOException, InterruptedException {
  24.         JSONArray fullJson = new JSONArray(readJson());
  25.  
  26.         int idx = 0;
  27.         for (Iterator<Object> it = fullJson.iterator(); it.hasNext(); ) {
  28.             JSONObject jsonObject = (JSONObject) it.next();
  29.             String contentid = jsonObject.getString("contentid");
  30.             Path path = Paths.get(FOLDER_PATH + contentid + ".txt");
  31.             if (Files.exists(path)) {
  32.                 idx++;
  33.                 continue;
  34.             }
  35.  
  36.             Object commonDetail = getCommonDetail(contentid);
  37.             Object introDetail = getIntroDetail(contentid);
  38.             Object moreDetail = getMoreDetail(contentid);
  39.  
  40.             jsonObject.put("common_detail_body", commonDetail);
  41.             jsonObject.put("intro_detail_body", introDetail);
  42.             jsonObject.put("more_detail_body", moreDetail);
  43.  
  44.             System.out.printf("====%d====\n", idx++);
  45.             Files.writeString(path, jsonObject.toString());
  46.         }
  47.     }
  48.  
  49.     private static Object getCommonDetail(String contentid) throws InterruptedException {
  50.         String source = Objects.requireNonNull(sendGetRequest(
  51.             "https://apis.data.go.kr/B551011/KorService1/detailCommon1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
  52.                 + contentid +
  53.                 "&serviceKey=" + SERVICE_KEY + "&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y"));
  54.         while (source.contains("HTTP ROUTING ERROR") || source.contains("UNKNOWN ERROR")) {
  55.             Thread.sleep(30000);
  56.             source = Objects.requireNonNull(sendGetRequest(
  57.                 "https://apis.data.go.kr/B551011/KorService1/detailCommon1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
  58.                     + contentid +
  59.                     "&serviceKey=" + SERVICE_KEY + "&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y"));
  60.         }
  61.  
  62.         return new JSONObject(source)
  63.             .getJSONObject("response").getJSONObject("body");
  64.     }
  65.  
  66.     private static Object getIntroDetail(String contentid) throws InterruptedException {
  67.         String source = Objects.requireNonNull(sendGetRequest(
  68.             "https://apis.data.go.kr/B551011/KorService1/detailIntro1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
  69.                 + contentid +
  70.                 "&contentTypeId=32&serviceKey=" + SERVICE_KEY));
  71.         while (source.contains("HTTP ROUTING ERROR") || source.contains("UNKNOWN ERROR")) {
  72.             Thread.sleep(30000);
  73.             source = Objects.requireNonNull(sendGetRequest(
  74.                 "https://apis.data.go.kr/B551011/KorService1/detailIntro1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
  75.                     + contentid +
  76.                     "&contentTypeId=32&serviceKey=" + SERVICE_KEY));
  77.         }
  78.  
  79.         return new JSONObject(source)
  80.             .getJSONObject("response").getJSONObject("body");
  81.     }
  82.  
  83.     private static Object getMoreDetail(String contentid) throws InterruptedException {
  84.         String source = Objects.requireNonNull(sendGetRequest(
  85.             "https://apis.data.go.kr/B551011/KorService1/detailInfo1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
  86.                 + contentid +
  87.                 "&contentTypeId=32&serviceKey=" + SERVICE_KEY));
  88.  
  89.  
  90.         while (source.contains("HTTP ROUTING ERROR") || source.contains("UNKNOWN ERROR")) {
  91.             Thread.sleep(30000);
  92.             source = Objects.requireNonNull(sendGetRequest(
  93.                 "https://apis.data.go.kr/B551011/KorService1/detailInfo1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
  94.                     + contentid +
  95.                     "&contentTypeId=32&serviceKey=" + SERVICE_KEY));
  96.         }
  97.  
  98.         return new JSONObject(source)
  99.             .getJSONObject("response").getJSONObject("body");
  100.     }
  101.  
  102.     private static String readJson() {
  103.         try {
  104.             String filePath = BASE_JSON;
  105.             return Files.readString(Paths.get(filePath));
  106.         } catch (IOException e) {
  107.             throw new RuntimeException(e);
  108.         }
  109.     }
  110.  
  111.     public static String sendGetRequest(String targetUrl) {
  112.         HttpURLConnection connection = null;
  113.  
  114.         try {
  115.             // URL 설정 및 연결
  116.             URL url = new URL(targetUrl);
  117.             connection = (HttpURLConnection) url.openConnection();
  118.  
  119.             // GET 방식으로 요청
  120.             connection.setRequestMethod("GET");
  121.  
  122.             // 응답 코드 확인 (옵션)
  123.             int responseCode = connection.getResponseCode();
  124.  
  125.             // 응답 읽기
  126.             BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
  127.             String inputLine;
  128.             StringBuilder response = new StringBuilder();
  129.  
  130.             while ((inputLine = in.readLine()) != null) {
  131.                 response.append(inputLine);
  132.             }
  133.             in.close();
  134.  
  135.             System.out.println(response.toString());
  136.             return response.toString();
  137.  
  138.         } catch (Exception e) {
  139.             throw new RuntimeException(e);
  140.         } finally {
  141.             if (connection != null) {
  142.                 connection.disconnect();
  143.             }
  144.         }
  145.     }
  146. }
  147.  
Add Comment
Please, Sign In to add comment