oEmbed-node, consuming oEmbed providers with node.js.

Published on Apr 07, 2013

During the winter I build a very simple website to curate ski videos from Vimeo and YouTube for my own personal use.

While working on it I needed to deal with the oEmbedd APIs of these two providers.

The result is this simple but extensible node.js module to easily consume oEmbed API‘s from multiple providers.

At the moment it only supports YouTube and Vimeo but you can easily extend it to work with others via custom providers.

UPDATE: Version 0.2.0 adds support for Revision3, Justin.tv and yFrog. It also fix a bug when processing YouTube urls that contain multiple parameters in the querystring.

Usage



  var oembed = require('oembed-node').init();
  oembed.get({url: "https://vimeo.com/62584176"}, getVideo);
  function getVideo(err, result) {

  }

</code>

The result will be a literal object with the properties returned by the provider. The library adds a video_url property to the object.
This property is not the url you entered but the “proper” url to embed a video. For example for YouTube you should use the http://youtu.be url to call the oEmbed API but the provider can deal with any url from youtube and “returns” the proper one.

Custom providers

The init method of the library takes a literal object that’s a map from host names to functions. Each of those functions is the handler that will return a proper oEmbed end point.

For example a custom provider for vimeo would be like this:



  var customProviders = {
    "vimeo.com" : {
      init: function (urlStr) {
        return {
          getUrls: function () {
            return {
              embed: "http://vimeo.com/api/oembed.json?url=" + urlStr,
              video: urlStr
            };
          }
        };
      }
    }
  };

And you can pass it to the init function of the module.



    var oembed = require('oembed-node').init(customProviders);

You can use the same handler for multiple host names, just associate it to other keys in the hash.