本文由一位外部开发者撰写和提交。 YouTube API 和工具团队感谢 Martin Legris 抽出宝贵时间并提供专业知识。
Martin Legris
2008 年 3 月
简介
在本教程中,我将向您展示如何使用不到 10 行实际 AS3 代码,检索过去 day、week、month 或 all_time 天内 YouTube 上最热门的视频。您可以获取以下任一标准 Feed:
- 观看最多
- 最新
- 评论最多
- 被链接最多
- 回复最多
- 最新精选
- 评分最高
- 收藏最多
- 手机视频
本例中使用的库始终是强类型的,这意味着,收到数据后,您可以在 FlashDevelop、Flex IDE、Eclipse w/FDT、IntelliJ Idea 等 IDE 中使用自动补全功能。
重要资源
在开始之前,我们特意列出了您可以使用 YouTube API 数据的 AS3 开发者,并参考以下资源。
- 我为使用这些网络服务而开发的 AS3 库
- YouTube Data API 官方文档
- 适用于 AS3 的 FlashDevelop IDE
- 在 YouTubeClient 上执行的哪些请求调用会触发哪些事件的列表
深入探究
发出首个请求
以下代码给出的注释也相当不错。不过,在深入探讨之前,我会先介绍一下该功能。所有请求的起点都是一个名为 YouTubeClient 的类。此类是一个单例,您可以通过执行以下操作来访问它:
// first you import it import ca.newcommerce.youtube.webservice.YouTubeClient; // then you get a reference to it var client:YouTubeClient = YouTubeClient.getInstance();
现在,我们可以发出请求了:
client.getStandardFeed(YouTubeClient.STD_TOP_RATED,
YouTubeClient.TIME_MONTH,
1,
10);我刚刚请求获取上个月评分最高的视频,结果为 1 到 10(请注意,每次调用最多只能返回 50 条结果)。很简单!
处理结果
那么,如何访问这些结果?由于 REST 类型的网络服务范例是异步的,因此使用事件来处理结果比在获取结果时暂停代码更容易。在本例中,我们需要为 StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED 事件声明事件监听器。每当 YouTubeClient 收到标准 Feed 的响应时,系统都会调用我们的函数。
client.addEventListener(StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED,
doVideosReceived);
要调用的函数将是 doVideosReceived。它将接受一个参数,即一个类型为 StandardVideoFeedEvent 的变量。这符合 AS3 中的标准。请声明这一情况,并追查我们收到的视频方面的一些信息。我们将跟踪到标准输出:
- 视频的标题
- 要嵌入视频的 SWF 的网址,
- 观看次数
- 评论数
- 视频时长,
- 以及作者的姓名。
function doVideosReceived(evt:StandardVideoFeedEvent):void
{
// get a reference to the feed containing the videos
var feed:VideoFeed = evt.feed;
// variable to hold each video retreived on the feed
var video:VideoData;
// iterate through the availabe results
while(video = feed.next())
{
// trace a newline followed by the video title
trace("\nvideo title:"+video.title);
// trace the url to use to embed the flash player with this video playing in it..
trace("player url:"+video.swfUrl);
// the view count
trace("viewCount:"+video.viewCount);
// the comment count
trace("commentCount:"+video.commentCount);
// the duration
trace("duration:"+video.duration);
// the author
trace("author:"+video.authors.first().name);
}
}关于内容库和 Feed 的一些注意事项
几乎所有事件都具有 .feed 属性。ProfileEvent 是唯一的例外,它具有 .profile 属性,因为此调用只会返回一条记录。
Feed 有很多类型,它们都实现以下方法:
first()- 检索第一条记录并指向它next()- 检索下一个可用记录last()- 检索上一个记录并指向它previous()- 检索 Feed 上的上一条记录getAt()- 获取特定位置的结果count()- 此 Feed 可用的结果数,与totalResults不同
当您到达结果的末尾时,next() 会返回 null。前面的示例也是如此,找到第一个结果后,对 previous() 的下一次调用将返回 null。
请注意,Feed 内的数据(例如类别)封装在 Iterators 内,因此您可以使用相同的函数来浏览可用类别。
完整源代码
以下是此示例的完整源代码。您可以下载 ZIP 文件,其中包含在 Flash CS3 中运行示例所需的一切内容,但最好更新库,以防我们修复了 bug 或更新了功能。您可以点击此处下载该库。
package
{
// first import dependencies (You can be more specific than this if you want)
import ca.newcommerce.youtube.data.*;
import ca.newcommerce.youtube.events.*;
import ca.newcommerce.youtube.feeds.*;
import ca.newcommerce.youtube.iterators.*;
import ca.newcommerce.youtube.webservice.YouTubeClient;
public class ytTest()
{
// some class variables
protected var _ws:YouTubeClient;
protected var _requestId:Number;
public function ytTest()
{
// now inside of an init function
_ws = YouTubeClient.getInstance();
// register to list to the events you are interested in
_ws.addEventListener(StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED, doVideosReceived);
// do your call.. get the Top Rated videos for the last month
// results 1 to 10; it returns a requestId
_requestId = _ws.getStandardFeed(YouTubeClient.STD_TOP_RATED,
YouTubeClient.TIME_MONTH,
1,
10);
}
protected function doVideosReceived(evt:StandardVideoFeedEvent):void
{
// get a reference to the feed containing the videos
var feed:VideoFeed = evt.feed;
// variable to hold each video retrieved on the feed
var video:VideoData;
while(video = feed.next())
{
// trace a newline followed by the video title
trace("\nvideo title:"+video.title);
// trace the swf URL (used for embedding)
trace("player url:"+video.swfUrl);
// the view count
trace("viewCount:"+video.viewCount);
// the comment count
trace("commentCount:"+video.commentCount);
// the duration
trace("duration:"+video.duration);
// the author
trace("author:"+video.authors.first().name);
}
}
}
}
总结
尽管本文只是触及冰山一角,但可以帮助您充分了解使用此库查询 YouTube 数据 API 是多么简单。我已花时间费力输入所有内容,因此可以轻松了解每个供稿响应都有哪些数据。目前尚不支持新的写入和上传功能,但如果您想为该库贡献代码,请访问项目页面。
作者简历
Martin Legris 拥有 12 年的软件开发经验。目前,他主要专注于界面研究,最喜欢的工具是 Flash w/ ActionScript。您可以访问 blog.martinlegris.com 查看他的博客,也可以访问 www.newcommerce.ca 查看他的网站(可能已过时)。
此作品已获得 Creative Commons 署名 3.0 美国版许可授权。