无法使用PhantomJS / CasperJS自动化包含大文件的Dailymotion上传页面

时间:2015-07-03 04:21:03

标签: javascript phantomjs casperjs

我制作了2个PhantomJS / CasperJS脚本,因为我正在尝试为多个平台开发一个开源视频上传器。

问题在于,对于Dailymotion,脚本适用于小文件,但不适用于大文件。

以下是测试PhantomJS脚本:

var page = new WebPage(), testindex = 0, loadInProgress = false;
page.settings.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0';
page.viewportSize = {
  width: 2560,
  height: 1440
};
page.customHeaders = {
  "Accept": "text/html, application/xhtml+xml, application/xml, q=0.9, */*, q=0.8",
  "Accept-Language": "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3",
  "Referer": "http://www.dailymotion.com/fr",
  "Access-Control-Allow-Origin": "*"
};
page.onConsoleMessage = function(msg) {
  console.log(msg);
};

page.onLoadStarted = function() {
  loadInProgress = true;
};

page.onLoadFinished = function() {
  loadInProgress = false;
};

page.onError = function(msg, trace) {
    var msgStack = ['ERROR: ' + msg];
    if (trace && trace.length) {
        msgStack.push('TRACE:');
        trace.forEach(function(t) {
            msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
        });
    }
    // uncomment to log into the console 
    // console.error(msgStack.join('\n'));
};

var steps = [
  function() {
    //Load Login Page
    page.open("http://www.dailymotion.com/widget/upload/?skin=fog");
  },
  function() {
      page.evaluate(function() {
      var form = document.getElementById("login_form");
      form.elements["username"].value="Nemsis1990";
      form.elements["password"].value="20051965";
      form.submit();
    });
  },
function() {
    //Load Login Page
    page.open("http://www.dailymotion.com/upload");
  },
  function() {
    console.log("Uploading file..");
    page.uploadFile('input[type=file]', '/users/jabassar/Desktop/sample2.mp4');
  },
  function () {
    page.render("test.png");
  }
];


interval = setInterval(function() {
  if (!loadInProgress && typeof steps[testindex] == "function") {
    //console.log("Step " + (testindex + 1) + " completed.");
    steps[testindex]();
    testindex++;
  }
  if (typeof steps[testindex] != "function") {
    //console.log("Task finished");
    phantom.exit();
  }
}, 10000);

function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 30000000, //< Default Max Timout is 3s
        start = new Date().getTime(),
        condition = false,
        interval = setInterval(function() {
            if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
                // If not time-out yet and condition not yet fulfilled
                condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
            } else {
                if(!condition) {
                    // If condition still not fulfilled (timeout but condition is 'false')
                    console.log("'waitFor()' timeout");
                    phantom.exit(1);
                } else {
                    // Condition fulfilled (timeout and/or condition is 'true')
                    console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                    typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                    clearInterval(interval); //< Stop this interval
                }
            }
        }, 1000); //< repeat check every 250ms
};

以下是测试CasperJS脚本:

var casper = require('casper').create();

casper.start('http://www.dailymotion.com/widget/upload/?skin=fog', function() {
    casper.fill('form[id="login_form"]', {
        'username': 'Nemsis1990',
        'password': '20051965'
    ,}, true);
    this.wait(2000);
});

casper.thenOpen('http://www.dailymotion.com/upload', function() {
    this.echo(this.getTitle());
});

casper.then(function() {
  this.page.uploadFile('input[type=file]', '/users/jabassar/Desktop/sample2.mp4');
});

casper.then(function() {
  this.wait(30000);
  this.capture('test2.png');
});

casper.run();

所以它适用于这个微小的(2mb)文件:

http://sample-videos.com/video/mp4/720/big_buck_bunny_720p_2mb.mp4

但它不能处理这个大文件:

http://sample-videos.com/video/mp4/720/big_buck_bunny_720p_30mb.mp4

Dailymotion说(javascript日志):

CHUNK FAIL [object Object] [object Object]
ERROR!!!!
CHUNK FAIL [object Object] [object Object]
ERROR!!!!
CHUNK FAIL [object Object] [object Object]
ERROR!!!!
CHUNK FAIL [object Object] [object Object]
ERROR!!!!

(2.0.0 phantomjs uploadFile被窃听,因此我使用1.9.8)

你能帮我吗?

PS:PhantomJS必须使用--web-security = false运行,否则会抛出警告。

0 个答案:

没有答案