From 8eb431a81271f204d4e6f31d10132815dbe2df02 Mon Sep 17 00:00:00 2001 From: "woo-jin.shin" Date: Sat, 4 Dec 2021 01:29:32 +0900 Subject: [PATCH] Prettify section message --- .../controller/CrawlAPIController.java | 4 +- .../processor/dto/PpomppuArticleParser.java | 2 +- .../dto/PpomppuArticleTransformer.java | 11 +++-- .../service/MessageSenderService.java | 6 ++- .../MessageSenderAPIController.java | 18 ++++--- .../ppomppu/sender/dto/SlackMessageDTO.java | 3 ++ .../ppomppu/support/dto/BlockMessageDTO.java | 7 ++- .../support/dto/code/PpomppuBoardName.java | 48 +++++++++---------- 8 files changed, 60 insertions(+), 39 deletions(-) diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/controller/CrawlAPIController.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/controller/CrawlAPIController.java index 8858a36..6a0ac18 100644 --- a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/controller/CrawlAPIController.java +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/controller/CrawlAPIController.java @@ -51,7 +51,7 @@ public class CrawlAPIController { .map(e -> ppomppuArticleService.filterOnlyNewArticles(boardName, e)) .map(e -> ppomppuArticleService.save(boardName, e)) .filter(e -> !e.isEmpty()) - .flatMap(messageSenderService::sendBlockMessageToSlack); + .flatMap(e -> messageSenderService.sendBlockMessageToSlack(boardName, e)); return publishedMessages.then(Mono.just(APIResponse.success(result.done()))); } @@ -62,7 +62,7 @@ public class CrawlAPIController { log.info("got request... {}", boardName); Mono publishedMessages = ppomppuRSSFeedService.getArticles(boardName) - .flatMap(messageSenderService::sendBlockMessageToSlack); + .flatMap(e -> messageSenderService.sendBlockMessageToSlack(boardName, e)); return publishedMessages.map(APIResponse::success); } diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleParser.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleParser.java index 63da010..60a85eb 100644 --- a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleParser.java +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleParser.java @@ -48,7 +48,7 @@ public final class PpomppuArticleParser { } public static String parseTitle(Element td) { - return td.getElementsByTag("a").text(); // TODO cdn image extracting + return td.getElementsByTag("a").text(); } public static String parseArticleUrl(Element td) { diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleTransformer.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleTransformer.java index b0284b7..d1fe521 100644 --- a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleTransformer.java +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleTransformer.java @@ -10,6 +10,7 @@ import java.util.stream.Collectors; import com.myoa.engineering.crawl.ppomppu.processor.domain.PpomppuArticle; import com.myoa.engineering.crawl.ppomppu.support.dto.BlockMessageDTO; import com.myoa.engineering.crawl.ppomppu.support.dto.SimpleMessageDTO; +import com.myoa.engineering.crawl.ppomppu.support.dto.code.PpomppuBoardName; /** * PpomppuArticleTransformer @@ -22,6 +23,8 @@ public final class PpomppuArticleTransformer { private PpomppuArticleTransformer() {} private static final String MESSAGE_FORMAT_V1 = "%s)) <%s|LINK> `%s` "; + private static final String MESSAGE_FORMAT_V2 = "%s *<%s|LINK>*\n%s"; + private static final String TITLE_FORMAT_V1 = "_*:hearts: %s | %s*_"; private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") .withZone(ZoneId.of("Asia/Seoul")); @@ -49,7 +52,7 @@ public final class PpomppuArticleTransformer { .build(); } - public static BlockMessageDTO transformToBlockMessage(List articles) { + public static BlockMessageDTO transformToBlockMessage(PpomppuBoardName boardName, List articles) { Instant requestedAt = Instant.now(); List body = articles.stream() .map(e -> BlockMessageDTO.createBlock(convertToInlineMessage(e), @@ -57,13 +60,15 @@ public final class PpomppuArticleTransformer { .collect(Collectors.toList()); return BlockMessageDTO.builder() .requestedAt(requestedAt) - .title(DATE_TIME_FORMATTER.format(requestedAt)) + .title(String.format(TITLE_FORMAT_V1, + boardName.getMenuName(), + DATE_TIME_FORMATTER.format(requestedAt))) .blocks(body) .build(); } public static String convertToInlineMessage(PpomppuArticle article) { - return String.format(MESSAGE_FORMAT_V1, + return String.format(MESSAGE_FORMAT_V2, article.getBoardName().getMenuName(), article.getArticleUrl(), article.getTitle()); } } diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/service/MessageSenderService.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/service/MessageSenderService.java index 6c6f48f..c76d5f0 100644 --- a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/service/MessageSenderService.java +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/service/MessageSenderService.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Service; import com.myoa.engineering.crawl.ppomppu.processor.domain.PpomppuArticle; import com.myoa.engineering.crawl.ppomppu.processor.dto.PpomppuArticleTransformer; import com.myoa.engineering.crawl.ppomppu.processor.infrastructure.client.MessageSenderAPIClient; +import com.myoa.engineering.crawl.ppomppu.support.dto.code.PpomppuBoardName; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; @@ -35,8 +36,9 @@ public class MessageSenderService { return messageSenderAPIClient.sendSimpleMessageToSlack(PpomppuArticleTransformer.transformToSimpleMessage(articles)); } - public Mono sendBlockMessageToSlack(List articles) { - return messageSenderAPIClient.sendBlockMessageToSlack(PpomppuArticleTransformer.transformToBlockMessage(articles)); + public Mono sendBlockMessageToSlack(PpomppuBoardName boardName, List articles) { + return messageSenderAPIClient.sendBlockMessageToSlack( + PpomppuArticleTransformer.transformToBlockMessage(boardName, articles)); } } diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/controller/MessageSenderAPIController.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/controller/MessageSenderAPIController.java index ef84721..06010e6 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/controller/MessageSenderAPIController.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/controller/MessageSenderAPIController.java @@ -44,13 +44,19 @@ public class MessageSenderAPIController { if (dto.getBlocks().isEmpty()) { return Mono.just(APIResponse.fail(dto, "empty blocks")); } - SlackMessageDTO slackMessageDTO = sender.ofBlockMessageBased(); - dto.getBlocks().forEach(slackMessageDTO::addSectionBlock); - slackMessageDTO.addBlock(SlackBaseMessageBlock.ofDivider()); - return sender.sendMessage(slackMessageDTO) - .doOnNext(e -> log.info("[sendBlockMessageToSlack] slackMessageDTO: {}", - ObjectMapperFactory.writeAsString(slackMessageDTO))) + + return sender.sendMessage(buildSlackMessageDTO(dto)) +// .doOnNext(e -> log.info("[sendBlockMessageToSlack] slackMessageDTO: {}", +// ObjectMapperFactory.writeAsString(buildSlackMessageDTO(dto)))) .then(Mono.just(APIResponse.success(dto))); } + + private SlackMessageDTO buildSlackMessageDTO(BlockMessageDTO dto) { + SlackMessageDTO slackMessageDTO = sender.ofBlockMessageBased(); + slackMessageDTO.addSectionBlock(dto.getTitle()); + dto.getBlocks().forEach(slackMessageDTO::addSectionBlock); + slackMessageDTO.addBlock(SlackBaseMessageBlock.ofDivider()); + return slackMessageDTO; + } } diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageDTO.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageDTO.java index d4bb207..5c8987d 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageDTO.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageDTO.java @@ -51,6 +51,9 @@ public class SlackMessageDTO implements MessageDTO { slackSectionMessageBlock.applyImageaccessory(block.getImageUrl(), block.getAltText()); addBlock(slackSectionMessageBlock); } + public void addSectionBlock(String rawBlockMessage) { + addBlock(SlackSectionMessageBlock.ofMarkDown(rawBlockMessage)); + } public void addBlock(SlackMessageBlock block) { blocks.add(block); diff --git a/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/BlockMessageDTO.java b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/BlockMessageDTO.java index 30a74d1..9531210 100644 --- a/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/BlockMessageDTO.java +++ b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/BlockMessageDTO.java @@ -4,6 +4,8 @@ import java.io.Serializable; import java.time.Instant; import java.util.List; +import com.myoa.engineering.crawl.ppomppu.support.dto.code.PpomppuBoardName; + import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -20,6 +22,7 @@ public class BlockMessageDTO implements Serializable { private static final long serialVersionUID = -6992039884035135523L; + private PpomppuBoardName boardName; private String title; private List blocks; private String url; @@ -27,8 +30,10 @@ public class BlockMessageDTO implements Serializable { private Instant requestedAt; @Builder - public BlockMessageDTO(String title, List blocks, String url, Instant publishedAt, + public BlockMessageDTO(PpomppuBoardName boardName, String title, + List blocks, String url, Instant publishedAt, Instant requestedAt) { + this.boardName = boardName; this.title = title; this.blocks = blocks; this.url = url; diff --git a/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/code/PpomppuBoardName.java b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/code/PpomppuBoardName.java index 4a5e6dd..2e7ba95 100644 --- a/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/code/PpomppuBoardName.java +++ b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/code/PpomppuBoardName.java @@ -12,30 +12,30 @@ import lombok.Getter; @Getter @AllArgsConstructor public enum PpomppuBoardName { - PPOMPPU_DOMESTIC_ALL("/zboard/zboard.php?id=ppomppu", "국내-전체", true), - PPOMPPU_DOMESTIC_ETC("/zboard/zboard.php?id=ppomppu&category=1", "국내-기타", false), - PPOMPPU_DOMESTIC_COMPUTER("/zboard/zboard.php?id=ppomppu&category=4", "국내-컴퓨터", false), - PPOMPPU_DOMESTIC_DIGITAL("/zboard/zboard.php?id=ppomppu&category=5", "국내-디지털", false), - PPOMPPU_DOMESTIC_FOOD("/zboard/zboard.php?id=ppomppu&category=6", "국내-식품/건강", false), - PPOMPPU_DOMESTIC_BOOK("/zboard/zboard.php?id=ppomppu&category=8", "국내-서적", false), - PPOMPPU_DOMESTIC_APPLIANCES("/zboard/zboard.php?id=ppomppu&category=9", "국내-가전/가구", false), - PPOMPPU_DOMESTIC_PARENTING("/zboard/zboard.php?id=ppomppu&category=10", "국내-육아", false), - PPOMPPU_DOMESTIC_GIFTCARD("/zboard/zboard.php?id=ppomppu&category=11", "국내-상품권", false), - PPOMPPU_DOMESTIC_CLOTHES("/zboard/zboard.php?id=ppomppu&category=12", "국내-의류/잡화", false), - PPOMPPU_DOMESTIC_COSMETIC("/zboard/zboard.php?id=ppomppu&category=13", "국내-화장품", false), - PPOMPPU_DOMESTIC_OUTDOOR("/zboard/zboard.php?id=ppomppu&category=15", "국내-등산/캠핑", false), - PPOMPPU_OVERSEA_ALL("/zboard/zboard.php?id=ppomppu4", "해외-전체", true), - PPOMPPU_OVERSEA_ETC("/zboard/zboard.php?id=ppomppu4&category=1", "해외-기타", false), - PPOMPPU_OVERSEA_APPLIANCES("/zboard/zboard.php?id=ppomppu4&category=7", "해외-가전", false), - PPOMPPU_OVERSEA_TVAV("/zboard/zboard.php?id=ppomppu4&category=8", "해외-TV/영상", false), - PPOMPPU_OVERSEA_COMPUTER("/zboard/zboard.php?id=ppomppu4&category=3", "해외-컴퓨터", false), - PPOMPPU_OVERSEA_DIGITAL("/zboard/zboard.php?id=ppomppu4&category=4", "해외-디지털", false), - PPOMPPU_OVERSEA_MOBILEACCESSORY("/zboard/zboard.php?id=ppomppu4&category=9", "해외-액세서리", false), - PPOMPPU_OVERSEA_CLOTHES("/zboard/zboard.php?id=ppomppu4&category=5", "해외-의류/잡화", false), - PPOMPPU_OVERSEA_WATCH("/zboard/zboard.php?id=ppomppu4&category=2", "해외-시계", false), - PPOMPPU_OVERSEA_SHOES("/zboard/zboard.php?id=ppomppu4&category=11", "해외-신발", false), - PPOMPPU_OVERSEA_FOOD("/zboard/zboard.php?id=ppomppu4&category=10", "해외-식품/건강", false), - PPOMPPU_OVERSEA_PARENTING("/zboard/zboard.php?id=ppomppu4&category=6", "해외-육아", false), + PPOMPPU_DOMESTIC_ALL("/zboard/zboard.php?id=ppomppu", "국내-전체", false), + PPOMPPU_DOMESTIC_ETC("/zboard/zboard.php?id=ppomppu&category=1", "국내-기타", true), + PPOMPPU_DOMESTIC_COMPUTER("/zboard/zboard.php?id=ppomppu&category=4", "국내-컴퓨터", true), + PPOMPPU_DOMESTIC_DIGITAL("/zboard/zboard.php?id=ppomppu&category=5", "국내-디지털", true), + PPOMPPU_DOMESTIC_FOOD("/zboard/zboard.php?id=ppomppu&category=6", "국내-식품/건강", true), + PPOMPPU_DOMESTIC_BOOK("/zboard/zboard.php?id=ppomppu&category=8", "국내-서적", true), + PPOMPPU_DOMESTIC_APPLIANCES("/zboard/zboard.php?id=ppomppu&category=9", "국내-가전/가구", true), + PPOMPPU_DOMESTIC_PARENTING("/zboard/zboard.php?id=ppomppu&category=10", "국내-육아", true), + PPOMPPU_DOMESTIC_GIFTCARD("/zboard/zboard.php?id=ppomppu&category=11", "국내-상품권", true), + PPOMPPU_DOMESTIC_CLOTHES("/zboard/zboard.php?id=ppomppu&category=12", "국내-의류/잡화", true), + PPOMPPU_DOMESTIC_COSMETIC("/zboard/zboard.php?id=ppomppu&category=13", "국내-화장품", true), + PPOMPPU_DOMESTIC_OUTDOOR("/zboard/zboard.php?id=ppomppu&category=15", "국내-등산/캠핑", true), + PPOMPPU_OVERSEA_ALL("/zboard/zboard.php?id=ppomppu4", "해외-전체", false), + PPOMPPU_OVERSEA_ETC("/zboard/zboard.php?id=ppomppu4&category=1", "해외-기타", true), + PPOMPPU_OVERSEA_APPLIANCES("/zboard/zboard.php?id=ppomppu4&category=7", "해외-가전", true), + PPOMPPU_OVERSEA_TVAV("/zboard/zboard.php?id=ppomppu4&category=8", "해외-TV/영상", true), + PPOMPPU_OVERSEA_COMPUTER("/zboard/zboard.php?id=ppomppu4&category=3", "해외-컴퓨터", true), + PPOMPPU_OVERSEA_DIGITAL("/zboard/zboard.php?id=ppomppu4&category=4", "해외-디지털", true), + PPOMPPU_OVERSEA_MOBILEACCESSORY("/zboard/zboard.php?id=ppomppu4&category=9", "해외-액세서리", true), + PPOMPPU_OVERSEA_CLOTHES("/zboard/zboard.php?id=ppomppu4&category=5", "해외-의류/잡화", true), + PPOMPPU_OVERSEA_WATCH("/zboard/zboard.php?id=ppomppu4&category=2", "해외-시계", true), + PPOMPPU_OVERSEA_SHOES("/zboard/zboard.php?id=ppomppu4&category=11", "해외-신발", true), + PPOMPPU_OVERSEA_FOOD("/zboard/zboard.php?id=ppomppu4&category=10", "해외-식품/건강", true), + PPOMPPU_OVERSEA_PARENTING("/zboard/zboard.php?id=ppomppu4&category=6", "해외-육아", true), ; private String resourcePath;