var async = require('async'),
  request = require('request'),
  u = require('underscore');

var regex = /(?:youtu\.be\/|youtube\.com\/(?:watch\?(?:.*&)?v=|(?:embed|v)\/))([^\?#&"'>]+)/;
module.exports.regex = regex;


var getApi = function(id, callback){
  request({
    url: 'http://gdata.youtube.com/feeds/api/videos/' + id + '?v=2&alt=json',
    json: true
  }, function(err, res, body){
    callback(err, body);
  });
};

var getOembed = function(id, callback){
  request({
    url: 'http://www.youtube.com/oembed?url=' + encodeURIComponent('https://www.youtube.com/watch?v=' + id),
    json: true
  }, function(err, res, body){
    callback(err, body);
  });
};


var fromId = function(id, callback){
  if (id){
    async.parallel({
      api: function(cb){
        getApi(id, cb);
      },
      oembed: function(cb){
        getOembed(id, cb);
      }
    }, function(err, resp){
      var json = u.extend({}, resp.oembed, {
        description: resp.api.entry.media$group.media$description.$t
      });
      callback(err, json);
    });
  }
  return null;
};

module.exports.fromUrl = function(url, callback){
  var match = url.match(regex);
  if (match){
    return fromId(match[1], callback);
  }
  return null;
};

@@ -207,24 +207,136 @@
  = regex;%0A%0A%0A
+var getMediaUrl = function(id)%7B%0A  // no need to use https%0A  return 'http://www.youtube.com/watch?v=' + id;%0A%7D;%0A%0A%0A
 var getApi =
@@ -531,16 +531,159 @@
 %7D);%0A%7D;%0A%0A
+var getMarkup = function(id, callback)%7B%0A  request(%7B%0A    url: getMediaUrl(id)%0A  %7D, function(err, res, body)%7B%0A    callback(err, body);%0A  %7D);%0A%7D;%0A%0A
 var getO
@@ -797,48 +797,24 @@
 ent(
-'https://www.youtube.com/watch?v=' + 
+getMediaUrl(
 id)
+)
 ,%0A  
@@ -1020,16 +1020,80 @@
      %7D,%0A
+      markup: function(cb)%7B%0A        getMarkup(id, cb);%0A      %7D,%0A
       oe
@@ -1175,16 +1175,510 @@
  resp)%7B%0A
+      // note that we are assuming a certain order of attributes here... TODO handle them switched%0A      var pattern = /%3C%5B%5E%3E%5D+%22og:(video:(?:width%7Cheight))%22%5B%5E%3E%5D+content=%22(%5B%5E%22%5D*)%22%5B%5E%3E%5D*%3E/g,%0A        match, width, height;%0A%0A      while ( match = pattern.exec( resp.markup ) ) %7B%0A        switch ( match%5B1%5D ) %7B%0A          case 'video:width':%0A            width = parseInt( match%5B2%5D, 10 );%0A            break;%0A          case 'video:height':%0A            height = parseInt( match%5B2%5D, 10 );%0A        %7D%0A      %7D%0A%0A
       va
@@ -1782,16 +1782,62 @@
 ption.$t
+,%0A        width: width,%0A        height: height
 %0A      %7D

include full width/height