banner



How To Get A Fixed Number Of Comments From Youtube Api

This article was written and submitted by an external developer. The YouTube APIs and Tools team thanks Martin Legris for his time and expertise.

Martin Legris
March 2008

  • Introduction
  • Important Resource
  • Digging In
    • Making your start request
    • Handling the results
    • A few notes about the library & feeds
  • Consummate Source Lawmaking
  • Conclusion

Introduction

In this tutorial I volition show you how, with under ten lines of actual AS3 code, you can retrieve the most popular videos on YouTube for the last day, calendar week, month or all_time. You can become any of the standard feeds:

  • About Viewed
  • Most Recent
  • Nearly Discussed
  • Most Linked
  • Nigh Responded
  • Recently Featured
  • Elevation Rated
  • Top Favorited
  • Videos for mobile phones

The library used in this example is hard-typed throughout, significant one time you lot have received the data, y'all tin can utilize the motorcar-completion feature in IDEs such every bit FlashDevelop, Flex IDE, Eclipse w/FDT, IntelliJ Thought, and more than.

Of import Resources

Before we start, here is the list of resources bachelor to you - the AS3 developer interested in using data from the YouTube API.

  • The AS3 Library I developed to consume these web services
  • Official YouTube Data API Documentation
  • FlashDevelop IDE for AS3
  • A listing of what events are fired past which request call done on YouTubeClient

Digging In

Making your showtime asking

The lawmaking which follows is fairly well commented. I volition, however, describe the functionality a chip before I dig in. The starting indicate for all requests is a form called YouTubeClient. This form is a singleton to which you get admission doing the following:

// first you import information technology import ca.newcommerce.youtube.webservice.YouTubeClient;  // then yous get a reference to information technology var client:YouTubeClient = YouTubeClient.getInstance();        

Now nosotros are ready to make a request:

client.getStandardFeed(YouTubeClient.STD_TOP_RATED,                                   YouTubeClient.TIME_MONTH,                                  1,                                   x);

I just requested the top rated videos over the last month, results 1 to x (annotation that there is a maximum of 50 results per call). Easy!

Treatment the results

Now, how exercise I admission these results? Since the REST type webservice paradigm is asynchronous, it is easier to apply events to handle the results instead of halting the code while the results are fetched. In this case, we need to declare an event listener for the StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED upshot. Our function will be chosen every time the YouTubeClient receives a response for a Standard Feed.

client.addEventListener(StandardVideoFeedEvent.STANDARD_VIDEO_DATA_RECEIVED,                          doVideosReceived);        

The function to be called will be doVideosReceived. It will accept one parameter, a variable of type StandardVideoFeedEvent. This follows the standard in AS3. Let us declare it, and make it trace a few things concerning the videos we received. Nosotros will trace to standard output:

  • the video's title,
  • the URL of the SWF to embed the video,
  • the view count,
  • the comment count,
  • the video elapsing,
  • and the author'due south proper name.
function doVideosReceived(evt:StandardVideoFeedEvent):void { 	// become 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.adjacent()) 	{ 		  // trace a newline followed past the video championship 		  trace("\nvideo title:"+video.title); 		   		  // trace the url to use to embed the flash role player with this video playing in it.. 		  trace("role 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 writer 		  trace("writer:"+video.authors.starting time().proper name); 	} }

A few notes about the library & feeds

Almost all events have a .feed property. ProfileEvent is the ane exception, information technology has a .profile holding instead, equally there is just one record returned by this call.

There are many types of feeds - they all implement the post-obit methods:

  • starting time() -- retrieve the first record and point to it
  • next() -- call up the next bachelor record
  • last() -- retrieve the final record and point to it
  • previous() -- call back the previous tape on the feed
  • getAt() -- get a result at a specific position
  • count() -- the number of results available for this feed, not the same every bit totalResults

When you get to the end of the results, adjacent() returns null. Same thing for previous, once you lot get to the first result, the next phone call to previous() volition return null.

Note that data inside of feeds (e.thousand. categories) are wrapped inside of Iterators, and so you can use the aforementioned functions to walk through available categories.

Consummate Source Code

The following is the full source code for this case. You can download a nil which contains everything you need to run the instance inside of Flash CS3, however, it is best to update the library in case we stock-still bugs or update features. You lot can download the library by clicking here.

package {     // get-go 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 course ytTest()     {         // some grade 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 telephone call.. become the Top Rated videos for the last calendar month             // results ane to 10; it returns a requestId             _requestId = _ws.getStandardFeed(YouTubeClient.STD_TOP_RATED,                                                 YouTubeClient.TIME_MONTH,                                                  1,                                                 10);         }          protected function doVideosReceived(evt:StandardVideoFeedEvent):void         {              // become a reference to the feed containing the videos             var feed:VideoFeed = evt.feed;              // variable to concur 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().proper name);              }          }      }  }        

Conclusion

Although this article barely touches the tip of the iceberg, it will give you a good idea of how elementary it is to query the YouTube Data API using this library. Since I've taken the time to hard-type everything, it is a breeze to explore what data is available on each feed response. Information technology doesn't support the new write and upload functionality yet, but if you desire to contribute to the library, visit the project folio.

Author Bio

AUTHORNAME

Martin Legris boasts 12 years of active software development. Today, he mainly concentrates on user interface enquiry and his favorite tool is Wink west/ ActionScript. Cheque out his blog at weblog.martinlegris.com and website (can be outdated) at www.newcommerce.ca.

Creative Commons License
This work is licensed nether a Creative Commons Attribution iii.0 The states License.

Source: https://developers.google.com/youtube/articles/youtube_actionscript3

Posted by: deloachcrehose.blogspot.com

0 Response to "How To Get A Fixed Number Of Comments From Youtube Api"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel