YouTube API How-To

Posted on : 08-11-2006 | By : Tony Stubblebine | In : Uncategorized

Tags: , , , , , , , ,

8

What is it

The YouTube API lets you search for videos and display them on your site. For example, I use the API to pull videos tagged dance and display them on Rate My Dance Moves. The YouTube API is currently the 10th most popular API on ProgrammableWeb with 46 mashups.

To get started you need to sign up for a developer account. That will give you the developer id required to use the API.

Using the API

The major API methods are for retrieving lists of videos, which you can do by tag, user, user favorites, and featured. The information returned from the list methods is usually enough to display a video on your site. All you have to do is plug the video id into the web snippit below.


<object width="425" height="350">
<param name="movie" value="http://www.youtube.com/v/ID&autoplay=1">
</param>
<embed src="http://www.youtube.com/v/ID&autoplay=1"
type="application/x-shockwave-flash" width="425" height="350"></embed>
</object>

Most of the information you need is in the video list methods. However, if you want a list of comments or channels for the video you’ll need to call the get_details method.

One complaint I had with list_by_tag is that the results come back ordered by relevance rather than recency. That means if you want to build a self-updating site you need to either regularly crawl the entire result set (results are paged) or sit on one of the RSS Feeds.

API Wrappers

There are good libraries for using the YouTube API with Perl, Ruby, and .NET. If you’re using another language, say Java, PHP or Python, you’ll have to write some code yourself. I’ve included links to example PHP and Python code and listed an example Ruby program that you can use as a template for whatever language you’re writing in.

There’s two competing Perl modules, but Hironori Yoshida’s WebService::YouTube and WebService::Youtube::Feeds seem best based on the recent development activity and strength of documentation. His is the only wrapper which includes support for the feeds, nice since the feeds have functionality thats not in the API (namely list by recency).

Shane Vitarana released a YouTube Ruby Gem after I’d built RateMyDanceMoves. Too bad, since it seems like the most polished of all the wrappers.

Eamonn Flynn has a .NET wrapper for the YouTube API.

There doesn’t seem to be a packaged PHP library but these two tutorials from waxjelly should be enough to get you started.
Simple PHP Script Using the YouTube API with Pagination
A More Complex PHP Script Using the YouTube API with Pagination.

ThinkHole labs has some example YouTube API code for Python users. Apparently using the API can be as simple as passing a dictionary to YouTube’s XML-RPC interface.

Code Example

I’d written my own code before the Ruby library came out. I want to show it here so you can see how simple writing your own code would be.


#!/usr/bin/env ruby
require 'open-uri'

tag = "dancing"
per_page = "100"
def_id = "YOUR_DEV_ID_HERE"
url = "http://www.youtube.com/api2_rest?" \
+ "method=youtube.videos.list_by_tag" \
+ "&tag=#{tag}&per_page=#{per_page}&dev_id=#{dev_id}&page=1"

open(url) do |f|
xml = f.read
end

doc = REXML::Document.new(xml)

elements = doc.root.get_elements("//video")
elements.each do |v|
puts v.get_elements("id").first.get_text.to_s
puts v.get_elements("title").first.get_text.to_s
puts v.get_elements("tags").first.get_text.to_s
puts v.get_elements("thumbnail_url").first.get_text.to_s
end

More Info

The Amazing YouTube Tools Collection has a long collection of YouTube tools, mashups, and plugins.

Online Ruby Training

Posted on : 17-07-2005 | By : Tony Stubblebine | In : Uncategorized

Tags: , , ,

2

Scott Gray, the founder of UserActive, was talking to me about how important hands-on learning is to any sort of training. We talked a little bit about me writing a Ruby or Ruby on Rails class for UserActive and I wanted to get a sense for how the classes work.

I started with Learning PHP. The class is self-paced, you read lab material and type code into an integrated programming sandbox. Then at the end of the lab you take a short test or do a short programming assignment. A grader looks over your work and gives you feedback.

The hands-on piece works! The programming sandbox is really useful and gives you a chance to immediately hack on whatever the lab is teaching you.

The Learning PHP class was geared towards people new to programming or new to the web, for instance there’s a “What is a variable” section. That got me thinking, how would I gear a Ruby on Rails tutorial, towards beginners or towards experts.

Rails has a real following among high level programmers because it abstracts a lot of menial details while also offering enough flexibility to override the defaults. It looks like a Java killer so it’s ending up in a lot of flame wars about how enterprise ready it is.

People haven’t explored how easy it is for designers or part-time programmers to use. Is it a PHP killer? Well, you don’t need to know any Ruby to get a Rails application up. That’s a good sign. You usually don’t need to know any SQL. That’s another good sign.

I wonder if the Model-View-Controller model is too abstract for most non-programmers? In my experience, people will do fine. Our web producers work with a much more abstracted system.

In any case, I think the online lab + sandbox model that UserActive offers would be a great introduction to people who probably aren’t going to go through the trouble of installing Rails themselves.

In the mean time, here’s the full UserActive catalog.

PHP Book Haul

Posted on : 21-06-2005 | By : Tony Stubblebine | In : Uncategorized

Tags: ,

0

Current work project is a social network a la Friendster or Linkedin to be launched at OSCON. It’s written in PHP. I need to do the single sign-on integration but I’ve never written in PHP before. Thankfully there’s O’Reilly’s free book perk.

PHP Cookbook
Programming PHP
Web Database Applications with PHP and MySQL

The cookbook’s been the most valuable from this haul.