[ANN] YouTube Library 0.8.0 Released

Posted by shane
on Monday, November 20

Documentation: http://youtube.shanesbrain.net/

RubyForge project page: http://rubyforge.org/projects/youtube/

My little YouTube Library has finally reached version 0.8.0, thanks to a sizable patch by Walter Korman, and some functional programming tips by Lucas Carlson. It now includes full unit tests, which can be run using rake, and all classes are under the YouTube namespace. Please note that this version breaks backwards compatibility. If you want to upgrade or install, do gem install youtube. You can still get the old version with gem install youtube --version 0.1.1.

Namespace

All classes are under the YouTube namespace. To create a new YouTube client, you now have to do:

youtube = YouTube::Client.new 'DEVELOPER ID'

Object Mapping

Results from YouTube are converted to corresponding Ruby objects. For example, video.view\_count will return an Integer, and video.rating\_avg will return a Float.

Embed HTML

Everyone wants to use this to embed videos on their Web3.1 site, no? Walter added the method embed\_html which takes in a width and height (or uses the default values) and outputs all the HTML you need to embed the Flash video on your site. Therefore, in a Rails view, you can do something like:

<%= video.embed\_html %>

Unit Tests

Run unit tests with % rake test. This allows for easy detection if YouTube ever changes their API on us.

Video Details

I got a lot of questions after the first release about how to get video details, so here in an example:

video = featured_videos.first       # returns a YouTube::Video object
details = video_details(video.id)   # returns a YouTube::VideoDetails object
@tags = details.tags                # returns a String for all tags for this video

In a similar vein to this library, Walter has released a Google Video API for Ruby that brilliantly uses Hpricot to parse video meta-data.

Comments

Leave a response

  1. EduardoDecember 07, 2006 @ 02:28 PM
    Hi, I'm trying to put youtube to work on dreamhost but I'm having a hard time... look this:
    [hi-c]$ script/console production
    Loading production environment.
    >> require 'youtube'
    ./script/../config/../config/../vendor/plugins/xml-simple-1.0.10/lib/xmlsimple.rb:275: warning: already initialized constant KNOWN_OPTIONS
    ./script/../config/../config/../vendor/plugins/xml-simple-1.0.10/lib/xmlsimple.rb:280: warning: already initialized constant DEF_KEY_ATTRIBUTES
    ./script/../config/../config/../vendor/plugins/xml-simple-1.0.10/lib/xmlsimple.rb:281: warning: already initialized constant DEF_ROOT_NAME
    ./script/../config/../config/../vendor/plugins/xml-simple-1.0.10/lib/xmlsimple.rb:282: warning: already initialized constant DEF_CONTENT_KEY
    ./script/../config/../config/../vendor/plugins/xml-simple-1.0.10/lib/xmlsimple.rb:283: warning: already initialized constant DEF_XML_DECLARATION
    ./script/../config/../config/../vendor/plugins/xml-simple-1.0.10/lib/xmlsimple.rb:284: warning: already initialized constant DEF_ANONYMOUS_TAG
    ./script/../config/../config/../vendor/plugins/xml-simple-1.0.10/lib/xmlsimple.rb:285: warning: already initialized constant DEF_FORCE_ARRAY
    ./script/../config/../config/../vendor/plugins/xml-simple-1.0.10/lib/xmlsimple.rb:286: warning: already initialized constant DEF_INDENTATION
    => true
    >> youtube = YouTube::Client.new 'iC0CAPoeq-4'
    => #<youtube::client:0x409fd8b0>
    >> youtube.videos_by_user('eduardordm')
    NoMethodError: undefined method `compact' for nil:NilClass
            from ./script/../config/../config/../vendor/plugins/youtube-0.8.0/lib/youtube.rb:85:in `videos_by_user'
            from (irb):3
    >> youtube.videos_by_user('br0wnpunk')
    NoMethodError: undefined method `compact' for nil:NilClass
            from ./script/../config/../config/../vendor/plugins/youtube-0.8.0/lib/youtube.rb:85:in `videos_by_user'
            from (irb):4
    >>
    
  2. shaneDecember 07, 2006 @ 03:20 PM
    XmlSimple should be returning an empty array instead of nil when no videos are found. This looks like a bug. Do the other methods work for you? This and others will be addressed in a bugfix release in a few days.
  3. ctaggDecember 14, 2006 @ 12:28 AM
    I think there's a tiny bug on the YouTube::Video class. Shouldn't the accessor for the length of the video be length_seconds?
  4. Daniel InsleyDecember 23, 2006 @ 07:04 AM
    (I noticed this post is the most recent, so I'm reposting this on the newer Youtube library entry as well) Eduardo, I came across the same problem when no results were returned from Youtube for an application i'm building. I fixed the problem locally so that it returns an empty array when no results returned from their side. I've also updated the tests to reflect these changes. (though, I had to throw Mocha in there in order to write the tests properly because nothing is currently being mocked in the tests) Shane, If you want to get ahold of me I can shoot him over some diff files of the changes I made. Not any big changes, but might save you some time. :)
  5. shaneDecember 23, 2006 @ 11:18 AM
    Thanks Daniel, I was working on a bugfix release so we may have duplicated some work. But go ahead and email me your patch at the address in the "About Me" page. If I use your patch I'll add you as a contributer. I'm thinking about setting up a trac for this lib to track bugs and whatnot.
  6. ctaggDecember 28, 2006 @ 03:26 AM
    Wondering if using solely the object tag (as described here: http://alistapart.com/articles/byebyeembed) is a better way to embed the video
  7. shaneFebruary 08, 2007 @ 06:43 PM
    Eduardo, ctagg, et al.. I just checked in fixes to the trunk that addresses all the issues in these comments. Sorry for the long hiatus. I will make a new release soon with these fixes along with the new functionality which YouTube recently added to their API.
Comment