Skip to content

Commit

Permalink
refactor: 블로그 타입 추가 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjd4204 committed Jan 5, 2024
1 parent b987bb5 commit d2ee189
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.api.TaveShot.domain.search.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Slf4j
public class GoogleItemDto {

@JsonProperty(value = "title")
Expand All @@ -23,6 +29,49 @@ public class GoogleItemDto {
@JsonProperty(value = "snippet")
private String snippet;

@JsonProperty(value = "metatags")
private List<GoogleMetatagsResponseDto> metatags;
@JsonProperty(value = "pagemap")
private Pagemap pagemap;

private String blog;

private String createdDate;


public static class Pagemap {

@JsonProperty(value = "metatags")
private List<Metatags> metatags;

}

public static class Metatags {

@JsonProperty(value = "article:published_time")
private String createdTime;


}

public void modifyBlog(String link){
// 정규 표현식
String regex = "\\.(.*?)\\.";

// 패턴 컴파일
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(link);

// 매칭된 부분 찾기
while (matcher.find()) {
// . 사이에 있는 단어 출력
this.blog = matcher.group(1);
}
}

public void modifyCreatedDate(){
String created = pagemap.metatags.get(0).createdTime;
if(created != null){
this.createdDate = created.substring(0, 10);
}
else this.createdDate = created;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.api.TaveShot.domain.search.service;

import com.api.TaveShot.domain.search.dto.GoogleItemDto;
import com.api.TaveShot.domain.search.dto.GoogleListResponseDto;
import com.api.TaveShot.domain.search.dto.GoogleResponseDto;
import lombok.AllArgsConstructor;
Expand All @@ -14,6 +15,7 @@
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.stream.Collectors;


@Transactional
Expand All @@ -29,7 +31,7 @@ public class SearchService {
private String CX;


public GoogleListResponseDto findBlog(String query){
public GoogleListResponseDto findBlog(String query) {
WebClient webClient = WebClient.builder()
.baseUrl("https://www.googleapis.com/customsearch/v1")
.build();
Expand All @@ -44,7 +46,14 @@ public GoogleListResponseDto findBlog(String query){
.retrieve()
.bodyToFlux(GoogleResponseDto.class);

log.info("{}", dto.collectList().block());

dto = dto.map(googleResponseDto -> {
for (GoogleItemDto googleItemDto : googleResponseDto.getItems()){
googleItemDto.modifyBlog(googleItemDto.getLink());
googleItemDto.modifyCreatedDate();
}
return googleResponseDto;
});

List<GoogleResponseDto> googleResponseDtos = dto.collectList().block();

Expand Down

0 comments on commit d2ee189

Please sign in to comment.