ActionScript 3.0 的 YouTube Data API:第一步

本文是由外部開發人員撰寫及提交。 YouTube API 與工具團隊對 Martin Legris 的支持,對他付出時間和專業。


Martin Legris
2008 年 3 月

簡介

本教學課程將示範如何使用實際 AS3 程式碼,以不到 10 行的方式,擷取 YouTube 上最近 dayweekmonthall_time 上最熱門的影片。您可以找到任何標準資訊提供

  • 觀看次數最多的影片
  • 最新上傳
  • 最多留言的影片
  • 最多連結
  • 最多影片回應
  • 最新精選影片
  • 評分最高
  • 最受喜愛影片
  • 手機影片

這個範例中使用的程式庫經過硬性輸入,也就是說,在您收到資料後,就可以在 IDE 中使用自動完成功能,例如 FlashDevelop、Flex IDE、Eclipse w/FDT、IntelliJ Idea 等等。

重要資源

開始之前,這裡列出您可以使用的資源:AS3 開發人員想要使用 YouTube API 提供的資料。

內用

發出第一個請求

以下程式碼相當合理。不過,我要深入說明這項功能後再深入說明。所有要求的起點都是名為 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 收到標準動態饋給的回應時,系統就會呼叫我們的函式。

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 屬性。ProfileEvent 是例外,而是具有 .profile 屬性,因為這次呼叫只會傳回一筆記錄。

動態饋給分為多種類型,所有方式都導入下列方法:

  • first() -- 擷取第一筆記錄並指向該記錄
  • next() -- 擷取下一筆可用記錄
  • last() -- 擷取最後的記錄並指向它
  • previous() -- 擷取資訊提供中的上一個紀錄
  • getAt() -- 在特定位置取得結果
  • count() -- 這個動態饋給可用的結果數量,與 totalResults 不同

當您到達結果結尾時,next() 會傳回空值。和之前一樣,當您收到第一個結果時,對 previous() 的下一次呼叫將會傳回 null

請注意,動態饋給內的資料 (例如類別) 會包裝在 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 有多簡單。我花了很多時間輸入所有資訊,所以要逐一查看動態饋給回應中可用的資料,是不是件容易的事。尚未支援新的寫入及上傳功能。如果想為程式庫提供內容,請造訪專案頁面

作者簡介


AUTHORNAME

Martin Legris 擅長 12 年的活躍軟體開發。如今,他主要關注使用者介面的研究,他最喜歡的工具是利用 Flash 工具執行 Flash。請造訪 blog.martinlegris.com 的網誌,並造訪 www.newcommerce.ca 網站 (網站可能已過時)。

創用 CC 授權
相關作品採用創用 CC 姓名標示 3.0 美國授權