如何在YouTube用户上传video时拨打电话?

我正在尝试创建一个简单的聊天机器人,它会在某个频道上传video时发送消息,理想情况下是video的名称和video的超链接。

YouTube的API非常奇怪,我对如何处理这个问题一无所知。

这是我到目前为止:

using System; using System.Linq; using Google.Apis.Services; using Google.Apis.YouTube.v3; YouTubeService service = new YouTubeService(new BaseClientService.Initializer() { ApiKey = apiKey, ApplicationName = "GoodApp" }); var getChannel = service.Channels.List("snippet"); getChannel.Id = channelId; var response = getChannel.Execute(); var channel = response[0]; //now what? 

使用YouTube PubSubHubbub服务,您可以在频道上传新video,更改video标题或更改video说明时获取推送通知。 从该页面引用有关设置的信息:

  1. 设置一个可以处理传入Atom提要通知的回调服务器。
  2. 使用Google中心订阅接收推送通知:
    • 模式设置为subscribe 。 (或者将模式设置为unsubscribe以取消订阅。)
    • 回调 URL设置为您在步骤1中设置的URL。
    • 主题url设置为https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID ,其中CHANNEL_ID是您要为其检索推送通知的YouTube频道ID 。
  3. 处理通知发送到您的回调服务器。 […]

不幸的是,回调服务器不仅仅是几行代码,而是取决于您运行的是哪种服务器。 如果您了解PHP,AppEngine或Go, PubSubHubbub上的订阅者存储库可能会有所帮助,否则搜索引擎似乎对我有好的结果。

我放弃了谷歌的.NET API,因为它对我的简单思维来说似乎太神秘了。

相反,我决定使用RestSharp直接从Web访问API,并使用JSON解析响应。

然后我设置一个System.Timers.Timer来检查它尚未检查的video每5分钟检查一次(频道每30分钟上传一次,但会有不同的时间)。

 RestClient restClient = new RestClient("https://www.googleapis.com/youtube/v3/"); DateTime lastTimeChecked = DateTime.Now; Timer timer = new Timer(60 * 5 * 1000); refreshTimer.Elapsed += CheckVideos; refreshTimer.Start(); public static void CheckVideos(object source = null, ElapsedEventArgs e = null) { var request = new RestRequest(String.Format( "search?part=snippet&channelId={0}&maxResults=4&order=date&type=video&key={1}", channelId, apiKey ), Method.GET); request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; var queryResult = restClient.Execute(request); YouTubeResponse response = JsonConvert.DeserializeObject(queryResult.Content); foreach (var video in response.items.Reverse()) { if (video.snippet.publishedAt > lastCheckTime) { //actual code to compare is much longer... SendMessage(String.Format("Video {0} was uploaded!", video.snippet.title)); } } LastCheckTime = DateTime.Now; } 

这远远不够完美,如果video上传的时间不合适,它通常会丢弃video。 显然,基于事件的解决方案是最佳的。