Skip to content

Commit

Permalink
feat: slack notifier when okky doesn't parsed (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
in-seo authored Aug 26, 2024
2 parents 1beb381 + b514fb0 commit 5dacd84
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions SouP/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 35 additions & 0 deletions SouP/src/main/java/Matching/SouP/common/SlackNotifier.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<start){
Expand Down

0 comments on commit 5dacd84

Please sign in to comment.