Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Iterator;
- import java.util.Objects;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public class Main {
- public static final String FOLDER_PATH = "src/main/resources/";
- public static final String BASE_JSON = FOLDER_PATH + "숙박정보조회.txt";
- public static final String SERVICE_KEY = "tn2EFIkIHocOuW2a6abdTLb3OO%2FP49vXjVrlGkQ5IP0aSicl%2BiLpXYVjb9yE%2BeRpXJqCd1zOIpyVZjPMoSkUpQ%3D%3D";
- public static void main(String[] args) throws IOException, InterruptedException {
- JSONArray fullJson = new JSONArray(readJson());
- int idx = 0;
- for (Iterator<Object> it = fullJson.iterator(); it.hasNext(); ) {
- JSONObject jsonObject = (JSONObject) it.next();
- String contentid = jsonObject.getString("contentid");
- Path path = Paths.get(FOLDER_PATH + contentid + ".txt");
- if (Files.exists(path)) {
- idx++;
- continue;
- }
- Object commonDetail = getCommonDetail(contentid);
- Object introDetail = getIntroDetail(contentid);
- Object moreDetail = getMoreDetail(contentid);
- jsonObject.put("common_detail_body", commonDetail);
- jsonObject.put("intro_detail_body", introDetail);
- jsonObject.put("more_detail_body", moreDetail);
- System.out.printf("====%d====\n", idx++);
- Files.writeString(path, jsonObject.toString());
- }
- }
- private static Object getCommonDetail(String contentid) throws InterruptedException {
- String source = Objects.requireNonNull(sendGetRequest(
- "https://apis.data.go.kr/B551011/KorService1/detailCommon1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
- + contentid +
- "&serviceKey=" + SERVICE_KEY + "&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y"));
- while (source.contains("HTTP ROUTING ERROR") || source.contains("UNKNOWN ERROR")) {
- Thread.sleep(30000);
- source = Objects.requireNonNull(sendGetRequest(
- "https://apis.data.go.kr/B551011/KorService1/detailCommon1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
- + contentid +
- "&serviceKey=" + SERVICE_KEY + "&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y"));
- }
- return new JSONObject(source)
- .getJSONObject("response").getJSONObject("body");
- }
- private static Object getIntroDetail(String contentid) throws InterruptedException {
- String source = Objects.requireNonNull(sendGetRequest(
- "https://apis.data.go.kr/B551011/KorService1/detailIntro1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
- + contentid +
- "&contentTypeId=32&serviceKey=" + SERVICE_KEY));
- while (source.contains("HTTP ROUTING ERROR") || source.contains("UNKNOWN ERROR")) {
- Thread.sleep(30000);
- source = Objects.requireNonNull(sendGetRequest(
- "https://apis.data.go.kr/B551011/KorService1/detailIntro1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
- + contentid +
- "&contentTypeId=32&serviceKey=" + SERVICE_KEY));
- }
- return new JSONObject(source)
- .getJSONObject("response").getJSONObject("body");
- }
- private static Object getMoreDetail(String contentid) throws InterruptedException {
- String source = Objects.requireNonNull(sendGetRequest(
- "https://apis.data.go.kr/B551011/KorService1/detailInfo1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
- + contentid +
- "&contentTypeId=32&serviceKey=" + SERVICE_KEY));
- while (source.contains("HTTP ROUTING ERROR") || source.contains("UNKNOWN ERROR")) {
- Thread.sleep(30000);
- source = Objects.requireNonNull(sendGetRequest(
- "https://apis.data.go.kr/B551011/KorService1/detailInfo1?MobileOS=ETC&MobileApp=appapp&_type=json&contentId="
- + contentid +
- "&contentTypeId=32&serviceKey=" + SERVICE_KEY));
- }
- return new JSONObject(source)
- .getJSONObject("response").getJSONObject("body");
- }
- private static String readJson() {
- try {
- String filePath = BASE_JSON;
- return Files.readString(Paths.get(filePath));
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- public static String sendGetRequest(String targetUrl) {
- HttpURLConnection connection = null;
- try {
- // URL 설정 및 연결
- URL url = new URL(targetUrl);
- connection = (HttpURLConnection) url.openConnection();
- // GET 방식으로 요청
- connection.setRequestMethod("GET");
- // 응답 코드 확인 (옵션)
- int responseCode = connection.getResponseCode();
- // 응답 읽기
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- System.out.println(response.toString());
- return response.toString();
- } catch (Exception e) {
- throw new RuntimeException(e);
- } finally {
- if (connection != null) {
- connection.disconnect();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment