Skip to content

Instantly share code, notes, and snippets.

@tanshio
Created June 14, 2014 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanshio/045c1e1193f97cfa5e05 to your computer and use it in GitHub Desktop.
Save tanshio/045c1e1193f97cfa5e05 to your computer and use it in GitHub Desktop.
download adwords and sponserd search report by spookyjs
"use strict";
// user settings
var G_ID = "google_id",
G_PASS = "google_pass",
Y_ID = "y_id",
Y_PASS = "y_pass";
// wait setting
var waitTime = 5000;
// UA setting
var UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36";
// URL settings
var Y_URL = "https://login.bizmanager.yahoo.co.jp/login?url=https://promotionalads.business.yahoo.co.jp";
try {
var Spooky = require('spooky');
} catch (e) {
var Spooky = require('../lib/spooky');
}
if (process.argv.length < 3) {
console.log('missing argument.');
return;
}else{
var argValue = process.argv;
console.log(argValue);
}
var spooky = new Spooky({
child: {
transport: 'http'
},
casper: {
logLevel: 'debug',
verbose: true
}
},
function (err) {
if (err) {
e = new Error('Failed to initialize SpookyJS');
e.details = err;
throw e;
}
spooky.userAgent(UA);
spooky.start(Y_URL);
spooky.then(function () {
this.evaluate(function(id,password) {
document.getElementById('user_name').value = id;
document.getElementById('password').value = password;
document.login_form.submit();
}, "y_id","y_pass");
});
spooky.wait(waitTime, function() {
this.click('a[sl="header_SS"]');
});
spooky.wait(waitTime, function() {
this.click('a[sl="header_SSReports"]');
});
spooky.wait(waitTime, function() {
var response = this.evaluate(function () {
var url = document.querySelector('tr[sl="ReportsReport_row_0"] a[sl=ReportsReport_download_]').href;
var title = document.getElementById('yjJtReportBody').getElementsByTagName('tr')[0].getElementsByTagName('td')[0].textContent;
var xhr = new XMLHttpRequest(),
dataString = "",
dataList = [];
method = "GET";
var contentType = "application/x-www-form-urlencoded";
xhr.open(method, url, false);
xhr.overrideMimeType("text/plain; charset=shift_jis");
if (method === "POST") {
if (typeof data === "object") {
for (var k in data) {
dataList.push(encodeURIComponent(k) + "=" + encodeURIComponent(data[k].toString()));
}
dataString = dataList.join('&');
this.log("sendAJAX(): Using request data: '" + dataString + "'", "debug");
} else if (typeof data === "string") {
dataString = data;
}
xhr.setRequestHeader("Content-Type", contentType);
}
xhr.send(method === "POST" ? dataString : null);
return {
data: xhr.responseText,
title: title
};
});
var fs = require('fs');
fs.write(response.title+'.csv', response.data, 'w');
});
spooky.run(function(){
this.exit();
});
});
spooky.on('error', function (e, stack) {
console.error(e);
if (stack) {
console.log(stack);
}
});
/*
// Uncomment this block to see all of the things Casper has to say.
// There are a lot.
// He has opinions.
spooky.on('console', function (line) {
console.log(line);
});
*/
spooky.on('hello', function (greeting) {
console.log(greeting);
});
spooky.on('log', function (log) {
if (log.space === 'remote') {
console.log(log.message.replace(/ \- .*/, ''));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment