-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from kakao-tech-campus-2nd-step3/develop
[�Develop] 3주차 과제 PR
- Loading branch information
Showing
84 changed files
with
2,595 additions
and
644 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Dockerfile | ||
|
||
# jdk17 Image Start | ||
#FROM --platform=linux/amd64 ubuntu:latest | ||
FROM openjdk:17 | ||
|
||
# 인자 설정 - Jar_File | ||
ARG JAR_FILE=build/libs/*.jar | ||
|
||
# jar 파일 복제 | ||
COPY ${JAR_FILE} app.jar | ||
|
||
# 인자 설정 부분과 jar 파일 복제 부분 합쳐서 진행해도 무방 | ||
#COPY build/libs/*.jar app.jar | ||
|
||
# 실행 명령어 | ||
ENTRYPOINT ["java", "-jar", "app.jar"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package team7.inplace.cicd; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class TestController { | ||
@GetMapping("/cicd") | ||
public ResponseEntity test() { | ||
return ResponseEntity.ok("ci/cd 테스트2"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/team7/inplace/crawling/application/YoutubeCrawlingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package team7.inplace.crawling.application; | ||
|
||
import java.util.Objects; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import team7.inplace.crawling.client.KakaoMapClient; | ||
import team7.inplace.crawling.client.YoutubeClient; | ||
import team7.inplace.crawling.persistence.YoutubeChannelRepository; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class YoutubeCrawlingService { | ||
private final YoutubeChannelRepository youtubeChannelRepository; | ||
private final YoutubeClient youtubeClient; | ||
private final KakaoMapClient kakaoMapClient; | ||
|
||
/* | ||
1. 유튜브 채널 정보를 모두 가져온다. | ||
2. 마지막 비디오와, 유튜브 UUID를 이용하여 비디오 정보를 가져온다. | ||
3. 마지막 비디오 UUID를 업데이트 한다. | ||
4. 카카오 API를 호출해 장소 정보를 가져온다 | ||
*/ | ||
public void crawlAllVideos() { | ||
var youtubeChannels = youtubeChannelRepository.findAll(); | ||
for (var channel : youtubeChannels) { | ||
var rawVideoInfos = youtubeClient.getVideos(channel.getPlayListUUID(), channel.getLastVideoUUID()); | ||
channel.updateLastVideoUUID(rawVideoInfos.get(0).videoId()); | ||
|
||
var videos = rawVideoInfos.stream() | ||
.map(rawVideoInfo -> kakaoMapClient.search(rawVideoInfo, channel.getChannelType().getCode())) | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
} | ||
} | ||
} | ||
|
58 changes: 58 additions & 0 deletions
58
src/main/java/team7/inplace/crawling/client/KakaoMapClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package team7.inplace.crawling.client; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.util.Objects; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
import team7.inplace.crawling.client.dto.RawPlace; | ||
import team7.inplace.crawling.client.dto.RawVideoInfo; | ||
import team7.inplace.global.kakao.config.KakaoApiProperties; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class KakaoMapClient { | ||
private static final String KAKAO_MAP_LOCATE_SEARCH_URL = "https://dapi.kakao.com/v2/local/search/keyword.json"; | ||
private static final String KAKAO_MAP_LOCATE_SEARCH_PARAMS = "?query=%s&sort=accuracy&page=1&size=15"; | ||
private static final String KAKAO_MAP_PLACE_SEARCH_URL = "https://place.map.kakao.com/main/v/"; | ||
private final KakaoApiProperties kakaoApiProperties; | ||
private final RestTemplate restTemplate; | ||
|
||
public RawPlace.Info search(RawVideoInfo videoInfo, String category) { | ||
var address = videoInfo.address(); | ||
var locationInfo = getLocateInfo(address, category); | ||
var placeId = locationInfo.has("documents") ? | ||
locationInfo.get("documents").get(0).get("id").asText() : null; | ||
if (Objects.isNull(placeId)) { | ||
return null; | ||
} | ||
|
||
var placeInfo = getPlaceInfo(placeId); | ||
return RawPlace.Info.from(locationInfo, placeInfo); | ||
} | ||
|
||
private JsonNode getLocateInfo(String address, String category) { | ||
var url = KAKAO_MAP_LOCATE_SEARCH_URL + KAKAO_MAP_LOCATE_SEARCH_PARAMS.formatted(address); | ||
url = url + (Objects.isNull(category) ? "" : "&category_group_code=" + category); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Authorization", kakaoApiProperties.getAuthorization()); | ||
HttpEntity<String> entity = new HttpEntity<>(headers); | ||
|
||
ResponseEntity<JsonNode> response = restTemplate.exchange(url, HttpMethod.GET, entity, JsonNode.class); | ||
return response.getBody(); | ||
} | ||
|
||
private JsonNode getPlaceInfo(String placeId) { | ||
var url = KAKAO_MAP_PLACE_SEARCH_URL + placeId; | ||
|
||
ResponseEntity<JsonNode> response = restTemplate.getForEntity(url, JsonNode.class); | ||
return response.getBody(); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/team7/inplace/crawling/client/YoutubeClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package team7.inplace.crawling.client; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
import team7.inplace.crawling.client.dto.RawVideoInfo; | ||
|
||
@Slf4j | ||
@Component | ||
public class YoutubeClient { | ||
private static final String PLAY_LIST_ITEMS_BASE_URL = "https://www.googleapis.com/youtube/v3/playlistItems"; | ||
private static final String PLAY_LIST_PARAMS = "?part=snippet&playlistId=%s&key=%s&maxResults=50"; | ||
private static final String ADDRESS_REGEX = "[가-힣0-9]+(?:도|시|구|군|읍|면|동|리|로|길)[^#,\\n()]+(?:동|읍|면|리|로|길|호|층|번지)[^#,\\n()]+"; | ||
private final RestTemplate restTemplate; | ||
private final String apiKey; | ||
|
||
public YoutubeClient(@Value("${youtube.api.key}") String apiKey, RestTemplate restTemplate) { | ||
log.info("Youtube API Key: {}", apiKey); | ||
this.restTemplate = restTemplate; | ||
this.apiKey = apiKey; | ||
} | ||
|
||
public List<RawVideoInfo> getVideos(String playListId, String finalVideoUUID) { | ||
List<RawVideoInfo> videoInfos = new ArrayList<>(); | ||
String nextPageToken = null; | ||
while (true) { | ||
String url = PLAY_LIST_ITEMS_BASE_URL + String.format(PLAY_LIST_PARAMS, playListId, apiKey); | ||
|
||
JsonNode response = null; | ||
if (Objects.nonNull(nextPageToken)) { | ||
url += "&pageToken=" + nextPageToken; | ||
} | ||
try { | ||
response = restTemplate.getForObject(url, JsonNode.class); | ||
} catch (Exception e) { | ||
log.error("Youtube API 호출이 실패했습니다. Youtuber Id {}", playListId); | ||
log.info(e.getMessage()); | ||
break; | ||
} | ||
if (Objects.isNull(response)) { | ||
log.error("Youtube API Response가 NULL입니다 {}.", playListId); | ||
break; | ||
} | ||
|
||
var containsLastVideo = extractRawVideoInfo(videoInfos, response.path("items"), finalVideoUUID); | ||
if (containsLastVideo) { | ||
break; | ||
} | ||
nextPageToken = response.path("nextPageToken").asText(); | ||
if (isLastPage(nextPageToken)) { | ||
break; | ||
} | ||
} | ||
return videoInfos; | ||
} | ||
|
||
private boolean isLastPage(String nextPageToken) { | ||
return Objects.isNull(nextPageToken) || nextPageToken.isEmpty(); | ||
} | ||
|
||
private boolean extractRawVideoInfo(List<RawVideoInfo> videoInfos, JsonNode items, String finalVideoUUID) { | ||
for (JsonNode item : items) { | ||
var snippet = item.path("snippet"); | ||
var videoId = snippet.path("resourceId").path("videoId").asText(); | ||
var videoTitle = snippet.path("title").asText(); | ||
var videoDescription = snippet.path("description").asText(); | ||
if (videoId.equals(finalVideoUUID)) { | ||
return true; | ||
} | ||
|
||
var address = extractAddress(videoDescription); | ||
if (Objects.nonNull(address)) { | ||
videoInfos.add(new RawVideoInfo(videoId, videoTitle, address)); | ||
continue; | ||
} | ||
log.info("주소를 찾을 수 없습니다. {}", videoDescription); | ||
} | ||
return false; | ||
} | ||
|
||
private String extractAddress(String description) { | ||
Pattern pattern = Pattern.compile(ADDRESS_REGEX); | ||
Matcher matcher = pattern.matcher(description); | ||
if (matcher.find()) { | ||
return matcher.group(); | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.