【发布时间】:2012-06-13 15:35:13
【问题描述】:
我正在使用 youtube 的 .NET API 来检索视频源,代码如下:
String[] ids;
YouTubeRequestSettings settings = new YouTubeRequestSettings("My App Name", "My App Key");
YouTubeRequest request = new YouTubeRequest(settings);
Uri uri =
new Uri("http://gdata.youtube.com/feeds/api/users/nptelhrd/uploads?max-results=50");//Change "GoogleDevelopers" to "default"
Google.GData.Client.Feed<Video> videoFeed = request.Get<Video>(uri);
ids = new String[videoFeed.TotalResults];
int count = 0;
for (int i = 0; i < 50; i++)
ids[i] = videoFeed.Entries.ElementAt(i).VideoId;
for (int i = 50; i < ids.Length-50; i+=50)
{
Uri uri2 =
new Uri("http://gdata.youtube.com/feeds/api/users/nptelhrd/uploads?max-results=50&start-index=" + i.ToString());//Change "GoogleDevelopers" to "default"
Google.GData.Client.Feed<Video> videoFeed2 = request.Get<Video>(uri);
for (int j = 0; j < 50; j++)
{
ids[i+j] = videoFeed2.Entries.ElementAt(j).VideoId;
count++;
}
}
上述代码返回用户“nptelhrd”上传的每个视频的所有信息(标题、描述、观看次数、评分...等)。此特定用户上传了 6950 个视频。
以上代码在 512Kbps 连接上执行需要 15 分钟,因为它检索每个视频的所有信息,速度非常慢,浪费大量服务器资源。不能修改上面的代码,让它只检索videoId吗?如何只检索所有视频的 videoId?
【问题讨论】:
-
如果 API 不支持您的要求,那么根本无法使用 API 实现您的目标。一种解决方案(尽管可能不受欢迎)可能是使用 Web 服务器下载这些提要并将视频 ID 回显到页面。这将允许您大大减少客户端将下载的不必要数据量,将大部分负载转移到 Web 服务器(大概配备了足够强大的连接,不必担心此类数据减少) .
-
是的,我认为这是唯一的选择,谢谢!
标签: c# optimization youtube youtube-api youtube.net-api