var cheerio = require('cheerio');
var Promise = require('bluebird');
var request = require('request-promise');

function extractProxer($) {
    return {
        url: $('source').attr('src'),
        type: 'mp4'
    };
}

function extractMP4Upload($) {
    var source = $('#player_code').html();
    var video = /'file' *: *'(.+)',/m.exec(source);
    if(video) {
        return {
            url: video[1],
            type: 'mp4'
        };
    } else {
        throw "Could not extract.";
    }
}

function extractYourUpload($) {
    var body = $('body').html();
    var video = /file *: *'(.+)',/m.exec(body);
    var referer = /link *: *'(.+embed.+)',/m.exec(body);
    if(video) {
        return request({
            url: video[1],
            headers: {
                'Referer': referer[1]
            },
            followRedirect: false
        }).catch(function(e) {
            return {
                url: e.response.headers.location,
                type: 'mp4'
            };
        });
    } else {
        throw "Could not extract.";
    }
}

function extractStreamCloud($, options) {
    var inputs = $('#login').find('form').serializeArray();
    var form = inputs.reduce(function(prev, curr) {
        prev[curr.name] = curr.value;
        return prev;
    }, {});

    return Promise.delay(12000).then(function() {
        return request({
            method: 'POST',
            url: options.url,
            form: form
        }).then(function(body) {
            var video = /file *: *"(.+)",/m.exec(body);
            if(video) {
                return {
                    url: video[1],
                    type: 'mp4'
                };
            } else {
                throw "Could not extract.";
            }
        });
    });
}

var parser = {
    extractors: {
        'proxer-stream': extractProxer,
        'mp4upload': extractMP4Upload,
        'yourupload': extractYourUpload,
        'streamcloud2': extractStreamCloud
    },
    findExtractor: function(stream) {
        if(this.extractors[stream.type]) {
            return parser.extractors[stream.type];
        } else {
            return function() {
                throw "Cannot find extractor for type " + stream.type;
            };
        }
    }
};

parser.parseVideo = function(options) {
    return Promise.resolve(options.page).then(cheerio.load)
        .then(function($) {
            return parser.findExtractor(options.stream)($, options);
        });
};

module.exports = parser;

@@ -103,16 +103,43 @@
 omise');
+%0Avar _ = require('lodash');
 %0A%0Afuncti
@@ -1791,16 +1791,560 @@
  %7D);%0A%7D%0A%0A
+function extractDailymotion($, options) %7B%0A    var bodyJson = /%5C(document%5C.getElementById%5C(%5C'player'%5C), (%5C%7B.*%5C%7D)%5C);/m.exec(options.page);%0A    if(!bodyJson) %7B%0A        throw %22Could not extract.%22%0A    %7D%0A%0A    var json = JSON.parse(bodyJson%5B1%5D);%0A    var qualities = json.metadata.qualities;%0A    var availableQualities = _.sortBy(_.filter(Object.keys(qualities), function(key) %7B return parseInt(key); %7D));%0A    var best = qualities%5BavailableQualities%5BavailableQualities.length - 1%5D%5D%5B0%5D;%0A    return %7B%0A        url: best.url,%0A        type: 'mp4'%0A    %7D;%0A%7D%0A%0A
 var pars
@@ -2530,16 +2530,59 @@
 eamCloud
+,%0A        'dailymotion': extractDailymotion
 %0A    %7D,%0A
@@ -2868,16 +2868,16 @@
   %7D%0A%7D;%0A%0A
-
 parser.p
@@ -2904,24 +2904,74 @@
 (options) %7B%0A
+    //TODO should not assume that we need cheerio%0A
     return P

Add extractor for dailymotion.