Permalink
Browse files

Added command line google program. Fixed cheerio dep. Aliased link.li…

…nk to link.href.
  • Loading branch information...
1 parent 4139740 commit 8231b73eb443b674456a570148050488ab81b9bc @jprichardson jprichardson committed Jan 22, 2013
Showing with 103 additions and 13 deletions.
  1. +6 −0 CHANGELOG.md
  2. +1 −1 LICENSE
  3. +47 −6 README.md
  4. +36 −0 bin/google
  5. +6 −3 lib/google.js
  6. +7 −3 package.json
View
@@ -1,3 +1,9 @@
+0.2.0 / 2013-01-22
+------------------
+* Fixed Cheerio dependency bug. Stuck at version 0.10.4
+* Aliased `link.link` to `link.href`.
+* Made command line `google` program.
+
0.1.0 / 2012-10-03
------------------
* Added `resultsPerPage`.
View
@@ -1,7 +1,7 @@
(The MIT License)
-Copyright (c) 2012 JP Richardson
+Copyright (c) 2012-2013 JP Richardson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
View
@@ -10,11 +10,11 @@ This is not sponsored, supported, or affiliated with Google Inc.
Installation
------------
- npm install google
+ npm install [-g] google
-Example
+API Example
-------
This prints out the first 50 search results of the query `node.js best practices`.
@@ -29,7 +29,7 @@ google('node.js best practices', function(err, next, links){
if (err) console.error(err);
for (var i = 0; i < links.length; ++i) {
- console.log(links[i].title + ' - ' + links[i].link);
+ console.log(links[i].title + ' - ' + links[i].link); //link.href is an alias for link.link
console.log(links[i].description + "\n");
}
@@ -42,18 +42,59 @@ google('node.js best practices', function(err, next, links){
```
+
+CLI
+---
+
+You can use the `google` package as a command line program as well. Just specify the `-g` flag when installing. The command line version outputs CSV data in the following format:
+
+ "{{HREF}}","{{TITLE}}","{{DESCRIPTION}}"
+
+### Usage
+
+
+ Usage: google [options]
+
+ Options:
+
+ -h, --help output usage information
+ -V, --version output the version number
+ -q, --query <query> Search query.
+ -p, --pages [num] Number of pages to search. defaults to 5
+ -n, --results-per-page [num] Number of results per page. defaults to 100
+
+
+### Example
+
+ google -q "node.js best practices" -p 1 -n 10
+
+outputs...
+
+ "http://www.slideshare.net/the_undefined/nodejs-best-practices-10428790","Node.js - Best practices","Dec 2, 2011 ... Second talk given at the munich node.js user group meeting on Dec 1, 2011."
+ "http://www.quora.com/Node-js/What-are-the-best-practices-for-node-js","Node.js: What are the best practices for node.js? - Quora","Node Express Boilerplate (https://github.com/mape/node-exp...) combines a lot of the best practices for asset-packing, authentication, error handling, etc. If I were ..."
+ "http://news.ycombinator.com/item?id=3169240","Ask N: node.js best practices? | Hacker News","Oct 28, 2011 ... Database migrations and testing are a few of the areas where Rails has a clearly defined best practice, but things seem murkier on the node.js ..."
+ "http://www.wilcoxd.com/whitepapers/node_js/","Node.js best practices - Wilcox Development Solutions","So, when evaluating node.js for use in a potential project, I asked around for the node.js best practices. I didn't get as much discussion as I was hoping for."
+ "http://stackoverflow.com/questions/10869108/node-js-best-frameworks-and-best-practices","mvc - Node Js best Frameworks and best practices - Stack Overflow","Check out Express, it's popular. High performance, high class web development for Node.js. And express-mvc-bootstrap can help too. Application ..."
+ "http://stackoverflow.com/questions/11922773/node-js-boilerplate-best-practices","javascript - node.js boilerplate + best practices - Stack Overflow","IMHO, for a project skeleton, more important than which modules to include ( those are easy to install with npm, anyway) is how to structure your ..."
+ "http://stackoverflow.com/questions/11311672/building-a-website-using-node-js-best-practice","php - Building a website using node.js - best practice - Stack Overflow","To start with the bad news: As node.js is a pretty young technique, I think you'll find that the proces of creating a full fledged website and ..."
+ "http://www.tumblr.com/ZGeLvx3Kjwh6","Best Practice - nodeJS: an experience","nodeJS: an experience I just spent about 4 hours diving into node seriously for the first time. It is very cool, very slick, and very fast � but it has poor ..."
+ "http://serverfault.com/questions/274857/how-to-use-node-js-as-a-production-web-server","How to use node.js as a production web server? - Server Fault","May 28, 2011 ... I'm currently developing a project using node, and as I'm approaching ... The gurus of node.js best practice are Ryan Dahl and Isaac Schlueter."
+ "http://joyent.com/blog/how-to-create-your-own-node-js-module","How To: Create Your Own Node.js Module - Blog - Joyent","Feb 28, 2011... thorough primer on how to get started coding up a module for Node.js. ... IRC and the Node.js Google Group, and best practices including the ..."
+
+
+
+
Test
----
npm test
-or...
- mocha test
License
-------
Licensed under MIT. See `LICENSE` for more details.
-Copyright (c) 2012 JP Richardson
+Copyright (c) 2012-2013 JP Richardson
+
View
@@ -0,0 +1,36 @@
+#!/usr/bin/env node
+
+var program = require('commander')
+ , google = require('../lib/google')
+
+program.version(require('../package').version)
+ .option('-q, --query <query>', 'Search query.')
+ .option('-p, --pages [num]', 'Number of pages to search. defaults to 5', function(p) { return parseInt(p, 10) })
+ .option('-n, --results-per-page [num]', 'Number of results per page. defaults to 100', function(n) { return parseInt(n, 10) })
+ //.option('-h, --header', 'Include the CSV header. defaults to false')
+ .parse(process.argv)
+
+if (!program.query) {
+ console.error("Query parameter required. See --help for more details.")
+ process.exit(1)
+}
+
+program.pages = program.pages || 5
+program.resultsPerPage = program.resultsPerPage || 100
+program.header = program.header || false
+
+google.resultsPerPage = program.resultsPerPage
+
+var nextCounter = 0;
+google(program.query, function(err, next, links){
+ if (err) console.error(err)
+
+ links.forEach(function(link) {
+ console.log('"%s","%s","%s"', link.href, link.title, link.description)
+ })
+
+ if (nextCounter < program.pages - 1) {
+ nextCounter += 1;
+ if (next) next()
+ }
+})
View
@@ -13,7 +13,7 @@ var linkSel = 'h3.r a'
var URL = 'http://www.google.com/search?hl=en&q=%s&start=%s&sa=N&num=%s';
function google(query, callback) {
- igoogle(query, 0, callback);
+ igoogle(query, 0, callback);
}
google.resultsPerPage = 10;
@@ -31,10 +31,13 @@ var igoogle = function(query, start, callback) {
$(itemSel).each(function(i, elem) {
var linkElem = $(elem).children(linkSel).first()
, descElem = $(elem).children(descSel).first()
- , item = {title: $(linkElem).text(), link: null, description: null}
+ , item = {title: $(linkElem).text(), link: null, description: null, href: null}
, qsObj = querystring.parse($(linkElem).attr('href'));
- if (qsObj['/url?q']) item.link = qsObj['/url?q'];
+ if (qsObj['/url?q']) {
+ item.link = qsObj['/url?q']
+ item.href = item.link
+ }
$(descElem).remove('div');
item.description = $(descElem).text();
View
@@ -1,6 +1,6 @@
{
"name": "google",
- "version": "0.1.0",
+ "version": "0.2.0",
"description": "A module to search and scrape google. This is not sponsored, supported, or affiliated with Google Inc.",
"homepage": [
"https://github.com/jprichardson/node-google"
@@ -24,14 +24,18 @@
}
],
"dependencies": {
- "cheerio": "~0.10.1",
- "request": "2.9.x"
+ "cheerio": "0.10.4",
+ "request": "2.12.x",
+ "commander": "~1.1.1"
},
"devDepdencies": {
"mocha": "*"
},
"main": "./lib/google",
"scripts": {
"test": "mocha test"
+ },
+ "bin": {
+ "google": "./bin/google"
}
}

0 comments on commit 8231b73

Please sign in to comment.