모락메뉴 api 추가

This commit is contained in:
hylee 2024-03-20 10:57:09 +09:00
parent 2e5c30b930
commit f68c5dc876
7 changed files with 319 additions and 0 deletions

18
pom.xml
View File

@ -118,6 +118,24 @@
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
</dependencies>

View File

@ -0,0 +1,32 @@
package com.itn.admin.cmn.msg;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.time.LocalDateTime;
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class RestResponse {
private String resultCode = "0";
private Object data;
private LocalDateTime localDateTime = LocalDateTime.now();
/**
* 성공 생성자
* 성공은 resultCode = 0
*/
public RestResponse(Object data) {
this.data=data;
}
}

View File

@ -0,0 +1,32 @@
package com.itn.admin.cmn.util.scheduled;
import com.itn.admin.etc.crawling.morak.service.MorakService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* packageName : com.itn.mjonApi.cmn.Scheduled
* fileName : ScheduledTasks
* author : hylee
* date : 2023-08-28
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2023-08-28 hylee 최초 생성
*/
@Service
public class ScheduledTasks {
@Autowired
private MorakService morakService;
@Scheduled(cron = "0 40 10 * * MON-FRI")
public void noonJob() throws IOException {
System.out.println("It's noon!");
morakService.morakMenu();
}
}

View File

@ -0,0 +1,57 @@
package com.itn.admin.cmn.util.slack;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.simple.JSONObject;
import java.io.IOException;
/**
* packageName : com.itn.mjonApi.util.slack
* fileName : Slack
* author : hylee
* date : 2023-08-28
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2023-08-28 hylee 최초 생성
*/
public class SlackUtil {
public static void sendMorakMenuToSlack(String sendMsg) {
String url = "https://hooks.slack.com/services/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";
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
JSONObject json = new JSONObject();
try {
String munjaText = sendMsg;
json.put("channel", "모락메뉴api");
json.put("text", munjaText);
// json.put("icon_emoji", ":원하는 아이콘:"); //커스터마이징으로 아이콘 만들수도 있다!
json.put("username", "모락 메뉴");
post.addParameter("payload", json.toString());
// 처음에 utf-8로 content-type안넣어주니까 한글은 깨져서 content-type넣어줌
post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
int responseCode = client.executeMethod(post);
String response = post.getResponseBodyAsString();
if (responseCode != HttpStatus.SC_OK) {
System.out.println("Response: " + response);
}
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException posting to Slack " + e);
} catch (IOException e) {
System.out.println("IOException posting to Slack " + e);
} finally {
post.releaseConnection();
}
}
}

View File

@ -0,0 +1,22 @@
package com.itn.admin.etc.crawling.morak.service;
import com.itn.admin.cmn.msg.RestResponse;
import java.io.IOException;
/**
* packageName : com.itn.mjonApi.etc.ganpandaum.service
* fileName : GdService
* author : hylee
* date : 2023-06-07
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2023-06-07 hylee 최초 생성
*/
public interface MorakService {
RestResponse morakMenu() throws IOException;
// void morakMenu() throws IOException;
}

View File

@ -0,0 +1,109 @@
package com.itn.admin.etc.crawling.morak.service.impl;
import com.itn.admin.cmn.msg.RestResponse;
import com.itn.admin.cmn.util.slack.SlackUtil;
import com.itn.admin.etc.crawling.morak.service.MorakService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;
import java.sql.SQLOutput;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* packageName : com.itn.mjonApi.etc.ganpandaum.service.impl
* fileName : GdServiceImpl
* author : hylee
* date : 2023-06-07
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2023-06-07 hylee 최초 생성
*/
@Service
@Slf4j
public class MorakServiceImpl implements MorakService {
@Override
public RestResponse morakMenu() {
// public void morakMenu() {
String imgUrl = "";
try {
while (true) {
// 웹사이트에 연결
Document doc = Jsoup.connect("https://m.blog.naver.com/jh06200/223266997482")
.timeout(60000) // 타임아웃 시간을 60초로 설정
.get();
// img가 있는 태그 가져오기
Element mainDoc = doc.select(".se-component.se-image.se-l-default").first();
// img에서 큰사이즈의 이미지 URL 가져오기
imgUrl = mainDoc.select("img").attr("src");
imgUrl = imgUrl.replace("w80_blur", "w800");
System.out.println("imgUrl : "+ imgUrl);
// 현재 날짜와 비교
// true면 오늘 날짜가 맞음
System.out.println(" befor this.dateComparison(imgUrl) :: "+ this.dateComparison(imgUrl));
if (this.dateComparison(imgUrl)) {
SlackUtil.sendMorakMenuToSlack(imgUrl);
System.out.println("this.dateComparison(imgUrl) :: "+ this.dateComparison(imgUrl));
break;
} else {
// 11시 30분이 지났으면 break;
if (isCurrentTime1130()) {
break;
}
Thread.sleep(100000);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return new RestResponse(imgUrl);
}
private boolean isCurrentTime1130() {
LocalTime now = LocalTime.now();
LocalTime targetTime = LocalTime.of(11, 30);
return now.isAfter(targetTime);
}
private Boolean dateComparison(String imgUrl) {
Boolean result = false;
// imgUrl에서 추출할 날짜 패턴 정수 8개 설정
Pattern pattern = Pattern.compile("(\\d{8})");
// 날짜 패턴 추출
Matcher matcher = pattern.matcher(imgUrl);
// 찾았으면 if true
if (matcher.find()) {
String extractedDate = matcher.group(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate dateFromText = LocalDate.parse(extractedDate, formatter);
LocalDate currentDate = LocalDate.now();
if (dateFromText.equals(currentDate)) {
result = true;
}
} else {
System.out.println("No date found in the text.");
}
return result;
}
}

View File

@ -0,0 +1,49 @@
package com.itn.admin.etc.crawling.morak.web;
import com.itn.admin.cmn.msg.RestResponse;
import com.itn.admin.etc.crawling.morak.service.MorakService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* packageName : com.itn.mjonApi.etc.ganpandaum.web
* fileName : SendRestController
* author : hylee
* date : 2023-02-15
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2023-02-15 hylee 최초 생성
*/
// 치환문자가 있으면 , => § 치환
@Slf4j
@RestController
public class MorakController {
@Autowired
private MorakService morakService;
/**
*
* @Discription 모락 메뉴 크롤링 컨트롤러
* @return
*/
@CrossOrigin("*") // 모든 요청에 접근 허용
@GetMapping("/etc/morak/morakMenu")
public ResponseEntity<RestResponse> morakMenu() throws Exception {
return ResponseEntity.ok().body(morakService.morakMenu());
}
}