Exemple #1
0
        public async Task CreatePlaylist(Core.Playlist playlist, bool publicPlaylist = true)
        {
            var youtubeService = await GetService();

            var newPlaylist = new Google.Apis.YouTube.v3.Data.Playlist()
            {
                Snippet = new PlaylistSnippet()
                {
                    Title       = playlist.Title,
                    Description = playlist.Description
                },
                Status = new PlaylistStatus
                {
                    PrivacyStatus = publicPlaylist == true ? "public" : "private"
                }
            };

            newPlaylist = await youtubeService.Playlists.Insert(newPlaylist, "snippet,status").ExecuteAsync();

            foreach (var item in playlist.MediaItems)
            {
                // Add a video to the newly created playlist.
                var newVideo = new PlaylistItem()
                {
                    Snippet = new PlaylistItemSnippet()
                    {
                        PlaylistId = newPlaylist.Id,
                        ResourceId = new ResourceId()
                    }
                };
                newVideo.Snippet.ResourceId.Kind    = "youtube#video";
                newVideo.Snippet.ResourceId.VideoId = GetVideoId(item);
                newVideo = await youtubeService.PlaylistItems.Insert(newVideo, "snippet").ExecuteAsync();
            }
        }
 /// <inheritdoc/>
 public async Task AddPlaylist(string title, string description)
 {
     try
     {
         var newPlaylist = new Google.Apis.YouTube.v3.Data.Playlist();
         newPlaylist.Snippet             = new PlaylistSnippet();
         newPlaylist.Snippet.Title       = title;
         newPlaylist.Snippet.Description = description;
         newPlaylist.Status = new PlaylistStatus();
         newPlaylist.Status.PrivacyStatus = "private";
         await m_YouTubeService !.Playlists.Insert(newPlaylist, "snippet,status").ExecuteAsync();
     }
     catch
     {
         // エラーが発生した場合は何もしない
     }
 }
Exemple #3
0
        public Model.Playlist GetPlaylistInfo(string playlistId)
        {
            Model.Playlist informationAboutPlaylist = null;

            try
            {
                var yt = new YouTubeService(new BaseClientService.Initializer()
                {
                    ApiKey = _APIKEY
                });
                var channelRequest = yt.Playlists.List("snippet");
                channelRequest.Id = playlistId;
                var channelResponse = channelRequest.Execute();

                Google.Apis.YouTube.v3.Data.Playlist playlist = channelResponse.Items[0];


                var playlistItemsListRequest = yt.PlaylistItems.List("snippet");
                playlistItemsListRequest.PlaylistId = playlistId;
                playlistItemsListRequest.MaxResults = 50;

                // Retrieve the list of videos uploaded to the authenticated user's channel.
                var playlistItemsListResponse = playlistItemsListRequest.Execute();

                informationAboutPlaylist = new Model.Playlist()
                {
                    Thumbnail   = GetMaxResolution(playlist.Snippet.Thumbnails),
                    Description = playlist.Snippet.Description,
                    Name        = playlist.Snippet.Title
                };
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }

            return(informationAboutPlaylist);
        }
Exemple #4
0
        public ObservableCollection <Track> GetSongInfo(string playlistId)
        {
            ObservableCollection <Track> informationAboutSong = null;

            try
            {
                var yt = new YouTubeService(new BaseClientService.Initializer()
                {
                    ApiKey = _APIKEY
                });
                var channelRequest = yt.Playlists.List("snippet");
                channelRequest.Id = playlistId;
                var channelResponse = channelRequest.Execute();

                Google.Apis.YouTube.v3.Data.Playlist playlist = channelResponse.Items[0];

                var playlistItemsListRequest = yt.PlaylistItems.List("snippet, contentDetails");
                playlistItemsListRequest.PlaylistId = playlistId;
                playlistItemsListRequest.MaxResults = 50;

                // Retrieve the list of videos uploaded to the authenticated user's channel.
                var playlistItemsListResponse = playlistItemsListRequest.Execute();

                informationAboutSong = new ObservableCollection <Track>();
                foreach (var playlistItem in playlistItemsListResponse.Items)
                {
                    //For get song duration
                    WebClient myDownloader = new WebClient();
                    myDownloader.Encoding = System.Text.Encoding.UTF8;

                    //We indicate our video (song)
                    string jsonResponse = myDownloader.DownloadString(
                        "https://www.googleapis.com/youtube/v3/videos?id=" + playlistItem.Snippet.ResourceId.VideoId + "&key="
                        + _APIKEY + "&part=contentDetails");
                    dynamic dynamicObject = Json.Decode(jsonResponse);
                    string  tmp           = dynamicObject.items[0].contentDetails.duration;
                    var     songDuration  = Convert.ToInt32(System.Xml.XmlConvert.ToTimeSpan(tmp).TotalSeconds);

                    //For get the channel title of the channel that the video belongs to.
                    var videoItemListRequest = yt.Videos.List("snippet");
                    videoItemListRequest.Id         = playlistItem.ContentDetails.VideoId;
                    videoItemListRequest.MaxResults = 50;

                    var videoItemListResponse = videoItemListRequest.Execute();

                    Track track = new Track()
                    {
                        ThumbnailUri = GetMaxResolution(playlistItem.Snippet.Thumbnails),

                        SongName = playlistItem.Snippet.Title.Contains(" - ") ? playlistItem.Snippet.Title.Substring(playlistItem.Snippet.Title.IndexOf(" - ") + 3)
                                                                              : playlistItem.Snippet.Title,
                        ArtistName = playlistItem.Snippet.Title.Contains(" - ") && videoItemListResponse.Items[0].Snippet.ChannelTitle
                                     != playlistItem.Snippet.Title.Substring(0, playlistItem.Snippet.Title.IndexOf(" - "))
                                                                                    ? playlistItem.Snippet.Title.Substring(0, playlistItem.Snippet.Title.IndexOf(" - "))
                                                                                    : videoItemListResponse.Items[0].Snippet.ChannelTitle,
                        Duration = $"{(songDuration / 60)}:{(songDuration % 60)}"
                    };

                    informationAboutSong.Add(track);
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }

            return(informationAboutSong);
        }