77 lines
2.7 KiB
Java
77 lines
2.7 KiB
Java
package com.myoa.engineering.crawl.ppomppu.processor.dto;
|
|
|
|
import com.myoa.engineering.crawl.ppomppu.processor.domain.PpomppuArticle;
|
|
import java.time.Instant;
|
|
import java.time.ZoneId;
|
|
import java.time.format.DateTimeFormatter;
|
|
import org.jsoup.nodes.Element;
|
|
import org.jsoup.select.Elements;
|
|
|
|
/**
|
|
* PpomppuArticleTransformer
|
|
*
|
|
* @author Shin Woo-jin (woozu.shin@kakaoent.com)
|
|
* @since 2021-09-08
|
|
*/
|
|
public final class PpomppuArticleTransformer {
|
|
|
|
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yy.MM.dd HH:mm:ss")
|
|
.withZone(ZoneId.of("Asia/Seoul"));
|
|
|
|
private PpomppuArticleTransformer() {}
|
|
|
|
public static PpomppuArticle toArticle(Elements articleElement) {
|
|
final long articleId = PpomppuArticleTransformer.toArticleId(articleElement.get(0));
|
|
final String title = PpomppuArticleTransformer.toTitle(articleElement.get(2));
|
|
final String articleUrl = PpomppuArticleTransformer.toArticleUrl(articleElement.get(2));
|
|
final int recommended = PpomppuArticleTransformer.toRecommended(articleElement.get(6));
|
|
final int hit = PpomppuArticleTransformer.toHit(articleElement.get(7));
|
|
final Instant registeredAt = PpomppuArticleTransformer.toRegisteredAt(articleElement.get(5));
|
|
|
|
return PpomppuArticle.builder()
|
|
.articleId(articleId)
|
|
.title(title)
|
|
.articleUrl(articleUrl)
|
|
.recommended(recommended)
|
|
.hit(hit)
|
|
.registeredAt(registeredAt)
|
|
.build();
|
|
}
|
|
|
|
public static Long toArticleId(Element td) {
|
|
return Long.parseLong(td.text().trim());
|
|
}
|
|
|
|
public static String toTitle(Element td) {
|
|
return td.text();
|
|
}
|
|
|
|
public static String toArticleUrl(Element td) {
|
|
return td.getElementsByTag("a").attr("href");
|
|
}
|
|
|
|
public static Integer toRecommended(Element td) {
|
|
final String voteString = td.text();
|
|
final int recommended;
|
|
|
|
if (voteString.isEmpty()) {
|
|
recommended = 0;
|
|
} else {
|
|
final int voteUp = Integer.parseInt(td.text().split(" - ")[0]);
|
|
final int voteDown = Integer.parseInt(td.text().split(" - ")[1]);
|
|
recommended = voteUp - voteDown;
|
|
}
|
|
return recommended;
|
|
}
|
|
|
|
public static Integer toHit(Element td) {
|
|
return Integer.parseInt(td.text());
|
|
}
|
|
|
|
public static Instant toRegisteredAt(Element td) {
|
|
final String registeredAtString = td.attr("title");
|
|
return DATE_TIME_FORMATTER.parse(registeredAtString, Instant::from);
|
|
}
|
|
|
|
}
|