async Task <VideoList> getYoutube() { // var url = "http://www.youtube.com/playlist?list=UUy2j4l0auN8DJaQ6itwVWFA"; // var url = "http://www.youtube.com/watch?v="; // タイトル一覧を取得する var youtube = new YouTubeService(new Google.Apis.Services.BaseClientService.Initializer() { ApiKey = "", // Google API key ApplicationName = "VS100Watcher" }); var videos = new VideoList(); var req = youtube.Search.List("snippet"); req.ChannelId = "UCy2j4l0auN8DJaQ6itwVWFA"; // "VisualStudioJapan" のチャンネルID var res = new Google.Apis.YouTube.v3.Data.SearchListResponse(); do { req.PageToken = res.NextPageToken; res = await req.ExecuteAsync(); // Youtube のサムネールをリスト化する foreach (var it in res.Items) { var vi = new Video() { Result = it }; // var cl = new HttpClient(); // var data = await cl.GetByteArrayAsync(vi.ThumUrl); // var bmp = new BitmapImage(); // var mem = new MemoryStream(data); // await bmp.SetSourceAsync( mem.AsRandomAccessStream() ); // vi.ThumImage = bmp; videos.Add(vi); } } while (res.NextPageToken != null && res.NextPageToken != ""); // 番号でソートする var lst = new VideoList(); foreach (var it in videos.OrderBy(x => { if (x.Title.StartsWith("VS100")) { return(x.Title.Replace(" ", "").Replace("-", "")); } else { return(x.Title); } })) { lst.Add(it); Debug.WriteLine(it.Title); } return(lst); }
public async Task getRandomVideo(string id, string maxNb = "50") { p.doAction(Context.User, Context.Guild.Id, Program.Module.Youtube); YouTubeService yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = File.ReadAllText("Keys/YoutubeAPIKey.dat") }); var searchListRequest = yt.Search.List("snippet"); searchListRequest.ChannelId = id; try { searchListRequest.MaxResults = Convert.ToInt32(maxNb); if (searchListRequest.MaxResults < 1 || searchListRequest.MaxResults > 50) { await ReplyAsync("The number max of video must be between 1 and 50."); return; } } catch (FormatException) { await ReplyAsync("The number max of video must be a number."); return; } catch (OverflowException) { await ReplyAsync("The number max of video must be between 1 and 50."); return; } Google.Apis.YouTube.v3.Data.SearchListResponse searchListResult = null; try { searchListResult = searchListRequest.Execute(); } catch (Google.GoogleApiException gae) { if (gae.Error.Code == 404) { await ReplyAsync("I didn't find any channel with this id."); return; } } if (searchListResult.Items.Count == 0) { await ReplyAsync("This channel doesn't contain any video."); return; } await ReplyAsync("Here is a random video of " + searchListResult.Items[0].Snippet.ChannelTitle + ":"); await ReplyAsync("https://www.youtube.com/watch?v=" + searchListResult.Items[p.rand.Next(searchListResult.Items.Count)].Id.VideoId); }
static List <SearchResult> TransformResponse(Google.Apis.YouTube.v3.Data.SearchListResponse response) { return(response.Items.Select(i => new SearchResult { Local = false, TypeId = YouTubeTypeId, VideoId = i.Id.VideoId, Title = i.Snippet.Title, Description = i.Snippet.Description, ThumbnailBase64 = Convert.ToBase64String(Utility.Download(i.Snippet.Thumbnails.Default__.Url)) }).ToList()); }
/// <summary> /// Returns a List<see cref="ClipInfo">YouTubeInfo</see> which represent /// the YouTube videos that matched the keyWord input parameter /// </summary> public async Task <List <ClipInfo> > GetSearchClipInfos(string keyWord) { var SearchRequest = youtubeService.Search.List("snippet"); SearchRequest.Q = keyWord; // Replace with your search term. SearchRequest.MaxResults = 20; Google.Apis.YouTube.v3.Data.SearchListResponse SearchList = null; try { SearchList = await SearchRequest.ExecuteAsync(); } catch (Exception e) { Console.WriteLine(string.Format("Error in GetSearchClipInfos {0}", e.Message)); return(new List <ClipInfo>()); } List <ClipInfo> resultInfo = new List <ClipInfo>(); foreach (var SearchItem in SearchList.Items) { ClipInfo result = new ClipInfo(); switch (SearchItem.Id.Kind) { case "youtube#video": result.Title = SearchItem.Snippet.Title; result.ChannelName = SearchItem.Snippet.ChannelTitle; result.ID = SearchItem.Id.VideoId; result.Thumbnail = new Uri(SearchItem.Snippet.Thumbnails.Default__.Url); resultInfo.Add(result); break; case "youtube#channel": break; case "youtube#playlist": break; } } return(resultInfo); }
public void SetMultipleVideosInfo(SocketGuild guild, YoutubeVideo video, Google.Apis.YouTube.v3.Data.SearchListResponse searchList) { int count = 1; int i = 0; foreach (var searchResult in searchList.Items) { Utilities utilities = new Utilities(guild); video.videosList.Add($"{count}. {searchResult.Snippet.Title} \n"); video.link[i] = searchResult.Id.VideoId; video.title[i] = searchResult.Snippet.Title; video.desc[i] = searchResult.Snippet.Description; video.image[i] = searchResult.Snippet.Thumbnails.High.Url; if (video.desc[i] == null || video.desc[i] == "") { video.desc[i] = Utilities.GetAlert("PLAY_NULL_DESC"); } count++; i++; } }
private void SearchByKeyword(string keyword) { titles.Clear(); IDs.Clear(); durations.Clear(); if (keyword == "") { return; } var youtubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "AIzaSyABcJNv5pYNK0Qo_pxqMK0I13hSwipf6bk", ApplicationName = this.GetType().ToString() }); var searchListRequest = youtubeService.Search.List("snippet"); var videoListRequest = youtubeService.Videos.List("contentDetails"); searchListRequest.Q = keyword; searchListRequest.MaxResults = 50; searchListRequest.VideoCategoryId = "10"; searchListRequest.Type = "video"; Google.Apis.YouTube.v3.Data.SearchListResponse searchListResponse = null; try { searchListResponse = searchListRequest.Execute(); } catch { waitingPanel(false); return; } if (searchListResponse.Items.Count <= 0) { return; } foreach (var searchResult in searchListResponse.Items) { switch (searchResult.Id.Kind) { case "youtube#video": titles.Add(searchResult.Snippet.Title); IDs.Add(searchResult.Id.VideoId); videoListRequest.Id += searchResult.Id.VideoId + ","; break; } } videoListRequest.Id = videoListRequest.Id.Remove(videoListRequest.Id.Length - 1); var videoListResponse = videoListRequest.Execute(); foreach (var item in videoListResponse.Items) { TimeSpan time = convertTimeFormat(item.ContentDetails.Duration); if (time.TotalMinutes > 10) { titles.RemoveAt(IDs.IndexOf(item.Id)); IDs.Remove(item.Id); continue; } durations.Add(time.Minutes.ToString("00") + ":" + time.Seconds.ToString("00")); } }