Created
May 20, 2013 21:35
-
-
Save geetotes/5615765 to your computer and use it in GitHub Desktop.
CasperJS googler with Tubes.io parms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var links = []; | |
var casper = require("casper").create(); | |
require('utils'); | |
//this filter looks for external links | |
function isLinky(linky) { | |
var pattern = /^((http|https|ftp):\/\/)/; | |
if(pattern.test(linky)) | |
return linky; | |
} | |
function getLinks() { | |
var links = document.querySelectorAll("h3.r a"); | |
return Array.prototype.map.call(links, function(e) { | |
try { | |
// google handles redirects hrefs to some script of theirs | |
return(/url\?q=(.*)&sa=U/).exec(e.getAttribute("href"))[1]; | |
} catch (err) { | |
return e.getAttribute("href"); | |
} | |
}); | |
} | |
casper.start("https://www.google.com/", function() { | |
// grab the argument passed in | |
cli = JSON.parse(casper.cli.args[0]); | |
query = cli.query; | |
this.fill('form[action="/search"]', { q: query }, true); | |
}); | |
casper.then(function() { | |
// aggregate results for the search | |
links = this.evaluate(getLinks); | |
}); | |
casper.run(function() { | |
//use the filter on the array to get rid of google image search and internal google results from the links | |
console.log(JSON.stringify(links.filter(isLinky))); | |
this.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Call this script with:
http://tubes.io/yourusername/tube/googletube.json?api_key=yourkey&query='your query'