如何自动管理 YouTube 帐户的播放列表

How to manage a YouTube account's playlist automatically

我需要什么

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.
}

问题

  1. How do I check if a playlist already exists?

  2. How do I check if a videa was already added?

您可以使用YouTube Data API v3访问数据:

  1. Playlists: list - 将允许您列出用户的所有播放列表
  2. PlaylistItems: list - 将允许您列出播放列表中的项目