本文由外部开发者编写和提交。 YouTube 工具和工具团队感谢 Martin Legris 所投入的时间和专业知识。
Martin Legris
2008 年 3 月
简介
在本教程中,我将通过 10 行的实际代码来向您介绍如何从 YouTube 上检索到最近 day
、week
、month
或 all_time
最受欢迎的视频。您可以获取以下任何标准 Feed:
- 观看最多
- 最新上传
- 评论最多
- 被链接最多
- 回复最多
- 最新精选
- 评分最高
- 收藏最多
- 手机视频
该示例中使用的库在整个过程中都是硬类型,这意味着当您收到数据后,便可在 IDE(例如 FlashDevelop、Flex IDE、Eclipse w/FDT、IntelliJ Idea 等)中使用自动补全功能。
重要资源
在我们开始之前,以下资源列出了 AS3 开发者,他们有兴趣使用 YouTube API 中的数据。
- 我开发的 AS3 库可供使用这些 Web 服务
- 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 中的标准。我们来声明一下它,并针对收到的视频进行一些跟踪。我们将跟踪到标准输出:
- 视频的标题、
- 用于嵌入视频的 {4/} 的网址,
- 观看次数
- 评论数量、
- 视频时长
- 和作者的姓名。
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 中运行示例所需的一切内容,不过,最好更新该库,以便我们及时修复错误或更新功能。您可以点击此处下载该库。
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 Data API。既然我已花时间认真输入了所有内容,因此您可以轻松探索每个 Feed 响应上有哪些数据。尚不支持新的写入和上传功能,但如果您想为该库做贡献,请访问项目页面。
作者个人简介
Martin Legris 拥有 12 年的活跃软件开发经验。目前,他主要专注于界面研究,他最喜欢的工具是 Flash 和 Action。请参阅他的博客(网址为 blog.martinlegris.com),并通过 www.newcommerce.ca 浏览网站(可能已经过时)。
此作品已获得知识共享署名 3.0 美国许可授权。