1+ package com .amcamp .domain .youtube .application ;
2+
3+ import com .amcamp .domain .youtube .dto .response .YouTubeVideoIdResponse ;
4+ import com .amcamp .global .util .MemberUtil ;
5+ import com .amcamp .infra .config .youtube .YoutubeProperties ;
6+ import com .google .api .client .http .javanet .NetHttpTransport ;
7+ import com .google .api .client .json .jackson2 .JacksonFactory ;
8+ import com .google .api .services .youtube .YouTube ;
9+ import com .google .api .services .youtube .YouTubeRequestInitializer ;
10+ import com .google .api .services .youtube .model .SearchListResponse ;
11+ import com .google .api .services .youtube .model .SearchResult ;
12+ import lombok .RequiredArgsConstructor ;
13+ import org .springframework .stereotype .Service ;
14+
15+ import java .io .IOException ;
16+ import java .util .Collections ;
17+ import java .util .List ;
18+
19+ @ Service
20+ @ RequiredArgsConstructor
21+ public class YouTubeService {
22+
23+ private final MemberUtil memberUtil ;
24+ private final YoutubeProperties youtubeProperties ;
25+
26+ public YouTubeVideoIdResponse getYouTubeLink (String query ) throws IOException {
27+ memberUtil .getCurrentMember ();
28+
29+ YouTube youtube = new YouTube .Builder (
30+ new NetHttpTransport (),
31+ new JacksonFactory (),
32+ request -> {
33+ })
34+ .setYouTubeRequestInitializer (new YouTubeRequestInitializer (youtubeProperties .apiKey ()))
35+ .build ();
36+
37+ YouTube .Search .List search = youtube .search ().list (Collections .singletonList ("id" ));
38+ search .setQ (query );
39+ search .setMaxResults (1L );
40+
41+ SearchListResponse searchResponse = search .execute ();
42+ List <SearchResult > results = searchResponse .getItems ();
43+
44+ if (!results .isEmpty ()) {
45+ String videoId = results .get (0 ).getId ().getVideoId ();
46+ return new YouTubeVideoIdResponse (videoId );
47+ }
48+
49+ return null ;
50+ }
51+ }
0 commit comments