Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/1] 리펙토링 #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .sdkmanrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java=17.0.5-amzn
77 changes: 0 additions & 77 deletions src/main/java/org/example/blog/convert/DateFormatConverter.java

This file was deleted.

24 changes: 20 additions & 4 deletions src/main/java/org/example/blog/scheduler/BlogCheckScheduler.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.example.blog.scheduler;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import org.example.blog.service.blog.BlogService;
import org.example.blog.service.message.MessageService;
import org.example.blog.service.scraping.ScrapingService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
Expand All @@ -14,17 +17,30 @@
public class BlogCheckScheduler {
private final Map<String, BlogService> blogServiceMap;
private final MessageService messageService;
private final ScrapingService scrapingService;
@Value("${blog.type}")
private String blogType;
@Value("${blog.url}")
private String blogUrl;

@Scheduled(cron = "0 0 0 ? * MON")
public void checkBlog() {
boolean isWrite = blogServiceMap.get(blogType.toLowerCase())
.isBetween7DaysBlogWrite(blogUrl, LocalDate.now());
if(!isWrite) {
messageService.send();
try {
LocalDate now = LocalDate.now();
String html = scrapingService.getHtml(blogUrl);
BlogService blogService = blogServiceMap.get(blogType.toLowerCase());
LocalDate lastWroteAt = blogService.findLastWroteAt(html);
if(now.isAfter(lastWroteAt.plusDays(7))) {
messageService.send();
}
} catch (Exception e) {
if (e instanceof IOException) {
System.out.println("HTML을 가져오는데 실패했습니다.");
e.printStackTrace();
} else {
System.out.println("알 수 없는 에러가 발생했습니다.");
e.printStackTrace();
}
}
}
}
18 changes: 1 addition & 17 deletions src/main/java/org/example/blog/service/blog/BlogService.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
package org.example.blog.service.blog;

import java.time.LocalDate;
import org.jsoup.Connection;
import org.jsoup.Jsoup;


public interface BlogService {
default String getContent(String blogUrl) {
try {
Connection connect = Jsoup.connect(blogUrl);
return connect.get().body().toString();

}catch (Exception e) {
e.printStackTrace();
}
return null;
}

boolean isBetween7DaysBlogWrite(String blogUrl,LocalDate now);

boolean isWrite(LocalDate now, String element);
LocalDate findLastWroteAt(String html);
}
19 changes: 19 additions & 0 deletions src/main/java/org/example/blog/service/blog/TistoryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.example.blog.service.blog;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

@Service(value = "tistory")
public class TistoryService implements BlogService {
@Override
public LocalDate findLastWroteAt(String html) {
Document document = Jsoup.parse(html);
String lastWroteAt = Objects.requireNonNull(document.selectFirst("#wrap > section > div > div > div:nth-child(1) > div > div > div.index-item-text > span")).text();
return DateTimeFormatter.ofPattern("yyyy.MM.dd").parse(lastWroteAt, LocalDate::from);
}
}
30 changes: 0 additions & 30 deletions src/main/java/org/example/blog/service/blog/Tistoryservice.java

This file was deleted.

30 changes: 10 additions & 20 deletions src/main/java/org/example/blog/service/blog/VelogService.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package org.example.blog.service.blog;

import java.time.LocalDate;
import java.util.List;
import org.example.blog.convert.DateFormatConverter;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

@Service(value = "velog")
public class VelogService implements BlogService{

@Override
public boolean isBetween7DaysBlogWrite(String blogUrl, LocalDate now) {
String content = getContent(blogUrl);
return isWrite(now, content);
}

public class VelogService implements BlogService {
@Override
public boolean isWrite(LocalDate now, String element) {
for (int i = 1; i <= 7; i++) {
LocalDate localDate = now.minusDays(i);
List<String> convertDates = DateFormatConverter.convertVelogDateToString(localDate);
boolean isWrite = convertDates.stream().filter(element::contains).count() > 0;
if (isWrite) {
return true;
}
}
return false;
public LocalDate findLastWroteAt(String html) {
Document document = Jsoup.parse(html);
String lastWroteAt = Objects.requireNonNull(document.selectFirst("body > div > div.BasicLayout_block__6bmSl > div.responsive_mainResponsive___uG64 > main > div > section > div.VelogPosts_block__nfCQF > div.FlatPostCardList_block__VoFQe > div:nth-child(1) > div.FlatPostCard_subInfo__cT3J6 > span:nth-child(1)")).text();
return DateTimeFormatter.ofPattern("yyyy년 M월 d일").parse(lastWroteAt, LocalDate::from);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.blog.service.scraping;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ScrapingService {
public String getHtml(String blogUrl) throws IOException {
Connection connect = Jsoup.connect(blogUrl);
return connect.get().body().toString();
}
}