Skip to content

Commit

Permalink
fix: metatags NPE 수정 (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip authored Jan 26, 2024
1 parent 6ec5567 commit abdbff9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.api.TaveShot.domain.search.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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
Expand All @@ -29,30 +25,12 @@ public class GoogleItemDto {
@JsonProperty(value = "snippet")
private String snippet;

@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){
public void modifyBlog(String link) {
// 정규 표현식
String regex = "\\.(.*?)\\.";

Expand All @@ -67,11 +45,4 @@ public void modifyBlog(String link){
}
}

public void modifyCreatedDate(){
String created = pagemap.metatags.get(0).createdTime;
if(created != null){
this.createdDate = created.substring(0, 10);
}
else this.createdDate = created;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.api.TaveShot.domain.search.dto.GoogleResponseDto;
import com.api.TaveShot.global.exception.ApiException;
import com.api.TaveShot.global.exception.ErrorType;
import lombok.AllArgsConstructor;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -16,11 +16,6 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

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


@Transactional
Expand All @@ -29,17 +24,16 @@
@Service
public class SearchService {

private final ProblemElementRepository problemElementRepository;
@Value("${google.secret.key}")
private String KEY;

@Value("${google.secret.cx}")
private String CX;

private final ProblemElementRepository problemElementRepository;

public GoogleListResponseDto findBlog(String query, int index) {

ProblemElement problemElement = problemElementRepository.findByProblemId(Integer.parseInt(query)).orElseThrow( () -> new ApiException(ErrorType._PROBLEM_NOT_FOUND));
ProblemElement problemElement = problemElementRepository.findByProblemId(Integer.parseInt(query))
.orElseThrow(() -> new ApiException(ErrorType._PROBLEM_NOT_FOUND));

WebClient webClient = WebClient.builder()
.baseUrl("https://www.googleapis.com/customsearch/v1")
Expand All @@ -56,11 +50,9 @@ public GoogleListResponseDto findBlog(String query, int index) {
.retrieve()
.bodyToFlux(GoogleResponseDto.class);


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

0 comments on commit abdbff9

Please sign in to comment.