티스토리 뷰
열심히 원본문서 + 구글검색을 통해 겨우 채널의 동영상 목록을 불러왔다
pom.xml
<properties>
<!-- google youtube -->
<project.youtube.version>v3-rev8-1.12.0-beta</project.youtube.version>
<project.http.version>1.12.0-beta</project.http.version>
<project.oauth.version>1.12.0-beta</project.oauth.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<!--google youtube -->
<repository>
<id>google-api-services</id>
<url>http://google-api-client-libraries.appspot.com/mavenrepo</url>
</repository>
</repositories>
<dependencies>
<!-- google api (youtube) -->
<dependencies>
<!-- YouTube Data V3 support -->
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-youtube</artifactId>
<version>${project.youtube.version}</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>${project.http.version}</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>${project.oauth.version}</version>
</dependency>
</dependencies>
<reporting>
<plugins>
<!-- google youtube -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.google.api.services.samples.youtube.cmdline.youtube_cmdline_channelbulletin_sample.ChannelBulletin</mainClass>
</configuration>
</plugin>
</reporting>
이리저리 고생해서 내가 넣은 pom태그들이다.. 정확하지는 않고 버전도 틀릴 수 있다.
Controller
package egovframework.sns.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import egovframework.sns.service.SnsService;
import sm.core.domain.sns.YoutubeDomain;
@Controller
@RequestMapping("/sns")
public class SnsController {
@Autowired
SnsService snsService;
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final long NUMBER_OF_VIDEOS_RETURNED = 1;
private static YouTube youtube;
@RequestMapping("/youtube")
public String SnsMain(Model model){
System.out.println("snsMain Controller");
List<YoutubeDomain> youtubeList = snsService.get();
model.addAttribute("youtubeList",youtubeList);
System.out.println("Sdsdsd :" + youtubeList);
return "default-main/sns/youtube";
}
}
Service
package egovframework.sns.service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.PlaylistItem;
import com.google.api.services.youtube.model.Thumbnail;
import sm.core.domain.sns.YoutubeDomain;
@Service
public class SnsService {
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final long NUMBER_OF_VIDEOS_RETURNED = 1;
private static YouTube youtube;
private static void prettyPrint(Iterator<PlaylistItem> iterator, YoutubeDomain youTubeDto) {
System.out.println("\n=============================================================");
System.out.println("=============================================================\n");
if (!iterator.hasNext()) {
System.out.println(" There aren't any results for your query.");
}
while (iterator.hasNext()) {
PlaylistItem singleVideo = iterator.next();
// Double checks the kind is video.
if (singleVideo.getKind().equals("youtube#video")) {
Thumbnail thumbnail = (Thumbnail) singleVideo.getSnippet().getThumbnails().get("default");
System.out.println(" Video Id" + singleVideo.getId());
System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
//System.out
// .println(" contentDetails Duration: " + singleVideo.getContentDetails());
System.out.println(" Thumbnail: " + thumbnail.getUrl());
System.out.println("\n-------------------------------------------------------------\n");
//youTubeDto.setThumbnailPath(thumbnail.getUrl());
//youTubeDto.setTitle(singleVideo.getSnippet().getTitle());
//youTubeDto.setVideoId(singleVideo.getId());
}
}
}
public List<YoutubeDomain> get() {
System.out.println("Dd");
List<YoutubeDomain> youtubeList = new ArrayList<YoutubeDomain>();
try {
youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("youtube-video-duration-get").build();
//내가 원하는 정보 지정할 수 있어요. 공식 API문서를 참고해주세요.
//YouTube.Videos.List videos = youtube.videos().list("id,snippet,contentDetails,part", null);
//videos.setKey("AIzaSyD-GsKkk0mSoiJNIQvLu2Is2vFHBwDjCaw"); //api키
//videos.setId("L9gSiyTLMe8"); // 영상 아이디
//videos.setMaxResults(NUMBER_OF_VIDEOS_RETURNED); //조회 최대 갯수.
//videos.setPart("id");
//YouTube.Channels.List channels = youtube.channels().list("id,snippet");
//channels.setKey("AIzaSyD-GsKkk0mSoiJNIQvLu2Is2vFHBwDjCaw");
//channels.setId("UCUj6rrhMTR9pipbAWBAMvUQ");
//최근 업로드 영상을 받아오는 형식
YouTube.PlaylistItems.List playListItems = youtube.playlistItems().list("id,snippet");
playListItems.setKey("AIzaSyD-GsKkk0mSoiJNIQvLu2Is2vFHBwDjCaw"); //api 키
playListItems.setPlaylistId("UU4CIotqM68CV_GpdfGLoQ8A"); //플레이리스트 명
playListItems.setMaxResults((long) 10);
//YouTube.Playlists.List playList = youtube.playlists().list("id,snippet");
//YouTube.Search.List searchList = youtube.search().list("id,snippet");
//searchList.setKey("AIzaSyD-GsKkk0mSoiJNIQvLu2Is2vFHBwDjCaw");
List<PlaylistItem> videoList = playListItems.execute().getItems();
//List<> playListItemsList = playListItems.execute().getItems();
System.out.println("youtube channel = " + videoList);
System.out.println("iterator = " + videoList.iterator());
/* if (videoList != null) {
prettyPrint(videoList.iterator(), youTubeDto);
}*/
for(PlaylistItem video : videoList){
String videoId = video.getSnippet().getResourceId().getVideoId();
String videoTitle = video.getSnippet().getTitle();
Map<String, Thumbnail> videoThumnail = video.getSnippet().getThumbnails();
String videoThumnailUrl = videoThumnail.get("default").getUrl();
youtubeList.add(new YoutubeDomain(videoId, videoTitle, videoThumnailUrl));
}
} catch (GoogleJsonResponseException e) {
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
} catch (IOException e) {
System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
return youtubeList;
}
}
'dev > Spring Framework' 카테고리의 다른 글
vo 복사 하기 (0) | 2021.08.24 |
---|---|
Spring @RestController 어노테이션 사용하기 (0) | 2021.07.08 |
Spring home 컨트롤러 설정 (0) | 2021.07.04 |
Spirng mvc Annotation으로 서비스 객체 생성하기 (0) | 2021.06.27 |
Spring 설정파일 분리하기 (0) | 2021.06.22 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Intercepter
- hypertexttransferprotocol
- React
- 스프링부트
- 컨트
- reject
- 백엔드 개발자 역량
- 인터셉터
- HTTP
- BindingResult
- 향해플러스
- 향해플러스백엔드
- 항해99
- 로그인
- 백엔드 개발자 공부
- thymleaf
- Java
- 향해99
- 예외처리
- SpringBoot
- jpa api
- exception
- JPA
- 리터럴
- 항해플러스
- filter
- react실행
- ArgumentResolver
- 스프링공부
- rejectValue
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함