From 1d70ff56b044b4b60eb52c2e347faa458fb7f30b Mon Sep 17 00:00:00 2001
From: youngniw <1999julie@naver.com>
Date: Sun, 15 May 2022 00:20:04 +0900
Subject: [PATCH] =?UTF-8?q?[feat]=20=EC=9D=98=EC=95=BD=ED=92=88=20?=
=?UTF-8?q?=EC=84=B8=EB=B6=80=20=EC=A0=95=EB=B3=B4=20=EC=A1=B0=ED=9A=8C=20?=
=?UTF-8?q?=ED=81=AC=EB=A1=A4=EB=A7=81=20(#8)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
build.gradle | 8 +-
.../pillaroid/component/JsoupComponent.java | 95 +++++++++++++++++++
.../controller/MedicineController.java | 27 ++++++
.../nadoyagsa/pillaroid/dto/Appearance.java | 19 ++++
.../com/nadoyagsa/pillaroid/dto/Medicine.java | 16 ++++
.../pillaroid/service/MedicineService.java | 20 ++++
6 files changed, 182 insertions(+), 3 deletions(-)
create mode 100644 src/main/java/com/nadoyagsa/pillaroid/component/JsoupComponent.java
create mode 100644 src/main/java/com/nadoyagsa/pillaroid/controller/MedicineController.java
create mode 100644 src/main/java/com/nadoyagsa/pillaroid/dto/Appearance.java
create mode 100644 src/main/java/com/nadoyagsa/pillaroid/dto/Medicine.java
create mode 100644 src/main/java/com/nadoyagsa/pillaroid/service/MedicineService.java
diff --git a/build.gradle b/build.gradle
index 7efe61e..6f062ef 100644
--- a/build.gradle
+++ b/build.gradle
@@ -26,9 +26,11 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
- implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
- runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
- runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.2'
+ implementation 'org.jsoup:jsoup:1.14.3'
+
+ implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
+ runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
+ runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
diff --git a/src/main/java/com/nadoyagsa/pillaroid/component/JsoupComponent.java b/src/main/java/com/nadoyagsa/pillaroid/component/JsoupComponent.java
new file mode 100644
index 0000000..207b44e
--- /dev/null
+++ b/src/main/java/com/nadoyagsa/pillaroid/component/JsoupComponent.java
@@ -0,0 +1,95 @@
+package com.nadoyagsa.pillaroid.component;
+
+import com.nadoyagsa.pillaroid.dto.Appearance;
+import com.nadoyagsa.pillaroid.dto.Medicine;
+import org.jsoup.Connection;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+@Component
+public class JsoupComponent {
+ public Medicine getMedicineInfo() {
+ final String medicineUrl = "";
+
+ Connection conn = Jsoup.connect(medicineUrl);
+ try {
+ Document document = conn.get();
+
+ Elements nameElements = document.getElementsByClass("stress");
+ if (nameElements.size() > 0) {
+ Element parent = nameElements.get(0).parent();
+ // 네이버 의약품 검색 크롤링 중
로 인해 잘린 부분이 있을 수 있어서 다음으로 변경
+ String parentProcessed = parent.html().replace("\n", "").replace("", "");
+
+ Document documentProcessed = Jsoup.parse(parentProcessed);
+
+ return getMedicineInfo(documentProcessed);
+ }
+ return getMedicineInfo(document);
+ } catch (IOException e) { e.printStackTrace(); }
+
+ return null;
+ }
+
+ public Medicine getMedicineInfo(Document document) { // 지식백과에서의 의약품명 크롤링
+ Medicine medicine = new Medicine();
+
+ Elements nameElements = document.getElementsByClass("stress");
+ for (Element nameElement : nameElements) { // 외형정보, 성분정보, 저장방법, 효능효과, 용법용량, 사용상 주의사항
+ String topic = nameElement.text();
+ Element textElement = nameElement.nextElementSibling();
+
+ if (textElement != null) {
+ String text = textElement.html()
+ .replace("
", "\n") // 줄바꿈 모두 저장
+ .replaceAll("<[^>]*>", ""); // 태그 모두 제거
+
+ switch (topic) {
+ case "외형정보":
+ Appearance appearanceInfo = new Appearance();
+
+ String[] splitTopic = text.split("·");
+ for (String subTopic : splitTopic) {
+ String[] information = subTopic.split(":");
+
+ if (information[0].contains("성상"))
+ appearanceInfo.setAppearance(information[1].trim());
+ else if (information[0].contains("제형"))
+ appearanceInfo.setFormulation(information[1].trim());
+ else if (information[0].contains("모양"))
+ appearanceInfo.setShape(information[1].trim());
+ else if (information[0].contains("색상"))
+ appearanceInfo.setColor(information[1].trim());
+ else if (information[0].contains("분할선"))
+ appearanceInfo.setDividingLine(information[1].trim());
+ else if (information[0].contains("식별표기"))
+ appearanceInfo.setIdentificationMark(information[1].trim());
+ }
+ medicine.setAppearanceInfo(appearanceInfo);
+ break;
+ case "성분정보":
+ medicine.setIngredient(text);
+ break;
+ case "저장방법":
+ medicine.setSave(text);
+ break;
+ case "효능효과":
+ medicine.setEfficacy(text);
+ break;
+ case "용법용량":
+ medicine.setUsage(text);
+ break;
+ case "사용상 주의사항":
+ medicine.setPrecautions(text);
+ break;
+ }
+ }
+ }
+ return medicine;
+ }
+}
diff --git a/src/main/java/com/nadoyagsa/pillaroid/controller/MedicineController.java b/src/main/java/com/nadoyagsa/pillaroid/controller/MedicineController.java
new file mode 100644
index 0000000..e08b3d7
--- /dev/null
+++ b/src/main/java/com/nadoyagsa/pillaroid/controller/MedicineController.java
@@ -0,0 +1,27 @@
+package com.nadoyagsa.pillaroid.controller;
+
+import com.nadoyagsa.pillaroid.dto.Medicine;
+import com.nadoyagsa.pillaroid.service.MedicineService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+
+@RestController
+@RequestMapping(value = "/medicine")
+public class MedicineController {
+ private final MedicineService medicineService;
+
+ @Autowired
+ public MedicineController(MedicineService medicineService) {
+ this.medicineService = medicineService;
+ }
+
+ //TODO: 추후에 medicine명 혹은 품목일련번호로 전달받아야 함
+ @GetMapping("/info")
+ public Medicine getMedicineInfo(HttpServletRequest request) {
+ return medicineService.getMedicineInfo();
+ }
+}
diff --git a/src/main/java/com/nadoyagsa/pillaroid/dto/Appearance.java b/src/main/java/com/nadoyagsa/pillaroid/dto/Appearance.java
new file mode 100644
index 0000000..f337316
--- /dev/null
+++ b/src/main/java/com/nadoyagsa/pillaroid/dto/Appearance.java
@@ -0,0 +1,19 @@
+package com.nadoyagsa.pillaroid.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@Builder
+public class Appearance {
+ private String appearance; // 성상
+ private String formulation; // 제형
+ private String shape; // 모양
+ private String color; // 색상
+ private String dividingLine; // 분할선
+ private String identificationMark; // 식별표기
+}
diff --git a/src/main/java/com/nadoyagsa/pillaroid/dto/Medicine.java b/src/main/java/com/nadoyagsa/pillaroid/dto/Medicine.java
new file mode 100644
index 0000000..74baf9e
--- /dev/null
+++ b/src/main/java/com/nadoyagsa/pillaroid/dto/Medicine.java
@@ -0,0 +1,16 @@
+package com.nadoyagsa.pillaroid.dto;
+
+import lombok.*;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@Builder
+public class Medicine {
+ private Appearance appearanceInfo; // 외형정보
+ private String ingredient; // 성분정보
+ private String save; // 저장방법
+ private String efficacy; // 효능효과
+ private String usage; // 용법용량
+ private String precautions; // 사용상 주의사항
+}
diff --git a/src/main/java/com/nadoyagsa/pillaroid/service/MedicineService.java b/src/main/java/com/nadoyagsa/pillaroid/service/MedicineService.java
new file mode 100644
index 0000000..d1b88fe
--- /dev/null
+++ b/src/main/java/com/nadoyagsa/pillaroid/service/MedicineService.java
@@ -0,0 +1,20 @@
+package com.nadoyagsa.pillaroid.service;
+
+import com.nadoyagsa.pillaroid.component.JsoupComponent;
+import com.nadoyagsa.pillaroid.dto.Medicine;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class MedicineService {
+ private final JsoupComponent jsoupComponent;
+
+ @Autowired
+ public MedicineService(JsoupComponent jsoupComponent) {
+ this.jsoupComponent = jsoupComponent;
+ }
+
+ public Medicine getMedicineInfo() {
+ return jsoupComponent.getMedicineInfo();
+ }
+}