Rails: Getting Youtube Video Information in XML

May 22, 2009

Here’s another little Rails code snippet for getting video details, this time from Youtube. The code simply retrieves an XML details object for a given video. Enjoy. . .

YOUTUBE_URL_REGEX = /(www.)?youtube\.com\/watch\?v=([^&]*)/i
url = http://www.youtube.com/watch?v=az-8Z1skhLs&feature=channel_page

begin
if YOUTUBE_URL_REGEX.match(url)
vid_id = YOUTUBE_URL_REGEX.match(resource_locator)[2]
youtube_url = "http://gdata.youtube.com/feeds/api/videos/#{vid_id}"

response = Net::HTTP.get_response(URI.parse(youtube_url))
results = Hash.from_xml(response.body)

@description = results["entry"]["group"]["description"]
@thumbnail_url = results["entry"]["group"]["thumbnail"][0]["url"]
end
rescue Exception => exc
# DNS failed...
end

See also: Getting Vimeo Video Information in JSON

Tags: , ,

Leave a Reply

Powered by WP Hashcash