Skip to content

Instantly share code, notes, and snippets.

@geetotes
Created May 20, 2013 21:35
Show Gist options
  • Save geetotes/5615765 to your computer and use it in GitHub Desktop.
Save geetotes/5615765 to your computer and use it in GitHub Desktop.
CasperJS googler with Tubes.io parms
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();
});
@geetotes
Copy link
Author

Call this script with:

http://tubes.io/yourusername/tube/googletube.json?api_key=yourkey&query='your query'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment