我需要什么
I'm working on a C# app that scans Facebook messages for youtube links and adds each new video to an existing playlist. If the playlist doesn't exist yet, it needs to be created.
我有什么
我有这段代码可以创建一个新的播放列表:
// Create a new, private playlist in the authorized user's channel.
var newPlaylist = new Playlist();
newPlaylist.Snippet = new PlaylistSnippet();
newPlaylist.Snippet.Title = "Test Playlist";
newPlaylist.Snippet.Description = "A playlist created with the YouTube API v3";
newPlaylist.Status = new PlaylistStatus();
newPlaylist.Status.PrivacyStatus = "public";
newPlaylist = await youtubeService.Playlists.Insert(newPlaylist, "snippet,status").ExecuteAsync();
但是因为它是插入的,所以它总是会在多次运行时创建同一个请求的播放列表的新实例。如果播放列表已经存在,它应该被更新。
这是添加新视频的代码,同时插入:
try
{
// Add a video to the newly created playlist.
var newPlaylistItem = new PlaylistItem();
newPlaylistItem.Snippet = new PlaylistItemSnippet();
newPlaylistItem.Snippet.PlaylistId = "PLMl3RyOwPdGlcrBTNYiu1XiNNgqYx6mx8";
//newPlaylistItem.Snippet.PlaylistId = newPlaylist.Id;
newPlaylistItem.Snippet.ResourceId = new ResourceId();
newPlaylistItem.Snippet.ResourceId.Kind = "youtube#video";
newPlaylistItem.Snippet.ResourceId.VideoId = videoId;
newPlaylistItem = await youtubeService.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync();
}
catch (Exception ex)
{
// Do some logging.
// Likely doesn't exist anymore, ignore.
}
问题
How do I check if a playlist already exists?
How do I check if a videa was already added?
最佳答案
您可以使用 YouTube Data API v3访问数据:
- Playlists: list - 将允许您列出用户的所有播放列表
- PlaylistItems: list - 将允许您列出播放列表中的项目
关于c# - 如何自动管理 YouTube 帐户的播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39540770/