From b514fb0b021be3c65091251a27e5eabf38287df5 Mon Sep 17 00:00:00 2001 From: in-seo Date: Mon, 26 Aug 2024 10:49:58 +0900 Subject: [PATCH] feat: slack notifier when okky doesn't parsed --- SouP/build.gradle | 1 + .../Matching/SouP/common/SlackNotifier.java | 35 +++++++++++++++++++ .../SouP/crawler/okky/OkkyService.java | 7 +++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 SouP/src/main/java/Matching/SouP/common/SlackNotifier.java diff --git a/SouP/build.gradle b/SouP/build.gradle index b6034d5..fcb3f93 100644 --- a/SouP/build.gradle +++ b/SouP/build.gradle @@ -50,6 +50,7 @@ dependencies { exclude group: "org.hamcrest", module: "hamcrest-core" } implementation 'org.hibernate.validator:hibernate-validator:6.0.7.Final' + implementation 'com.squareup.okhttp3:okhttp:4.9.3' } def frontendDir = "$projectDir/../soup-frontend" diff --git a/SouP/src/main/java/Matching/SouP/common/SlackNotifier.java b/SouP/src/main/java/Matching/SouP/common/SlackNotifier.java new file mode 100644 index 0000000..414b0bb --- /dev/null +++ b/SouP/src/main/java/Matching/SouP/common/SlackNotifier.java @@ -0,0 +1,35 @@ +package Matching.SouP.common; + +import Matching.SouP.service.PropertyUtil; +import lombok.extern.slf4j.Slf4j; +import okhttp3.*; + + +@Slf4j +public class SlackNotifier { + private static final OkHttpClient client = new OkHttpClient(); + + public void sendMessageToSlack() { + String webHookURL = PropertyUtil.getProperty("webhook.url"); + String message = "OKKY 파싱 에러"; + + RequestBody body = RequestBody.create( + MediaType.parse("application/json; charset=utf-8"), + "{\"text\":\"" + message + "\"}" + ); + + Request request = new Request.Builder() + .url(webHookURL) + .post(body) + .build(); + + try (Response response = client.newCall(request).execute()) { + if (!response.isSuccessful()) { + throw new RuntimeException("Unexpected code " + response); + } + log.warn("Message sent successfully: " + response.body().string()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/SouP/src/main/java/Matching/SouP/crawler/okky/OkkyService.java b/SouP/src/main/java/Matching/SouP/crawler/okky/OkkyService.java index fdc5af4..afc9c34 100644 --- a/SouP/src/main/java/Matching/SouP/crawler/okky/OkkyService.java +++ b/SouP/src/main/java/Matching/SouP/crawler/okky/OkkyService.java @@ -1,5 +1,6 @@ package Matching.SouP.crawler.okky; +import Matching.SouP.common.SlackNotifier; import Matching.SouP.crawler.ConvertToPost; import Matching.SouP.crawler.CrawlerService; import Matching.SouP.crawler.Selenium; @@ -101,7 +102,9 @@ private int startPage(WebDriver driver, int start) throws StringIndexOutOfBounds */ int cnt = 2; while(true){ - if (page > 5) { + if (page > 5 || cnt > 6) { + SlackNotifier slackNotifier = new SlackNotifier(); + slackNotifier.sendMessageToSlack(); throw new IllegalStateException("오키 파싱 에러"); } driver.get(urlOkky + "?page=" + page); @@ -117,6 +120,8 @@ private int startPage(WebDriver driver, int start) throws StringIndexOutOfBounds }catch (StringIndexOutOfBoundsException | NullPointerException e){ cnt++; log.info("StringIndexOutOfBoundsException"); + SlackNotifier slackNotifier = new SlackNotifier(); + slackNotifier.sendMessageToSlack(); continue; } if(num