Permalink
Browse files

Added a statuscode builtin

  • Loading branch information...
1 parent 205e5fb commit c29e15ba37abfd2958ee09256e02b8b036c11ef8 @chriso chriso committed Feb 7, 2011
Showing with 53 additions and 1 deletion.
  1. +17 −1 builtin/README.md
  2. +36 −0 builtin/statuscode.coffee
View
18 builtin/README.md
@@ -48,6 +48,22 @@ Example 4 - return unique IPs
$ node.io resolve ips < domains.txt
+### statuscode
+
+Makes a HEAD request to each URL of input and returns the status code
+
+Example 1 - return the status code (url,status)
+
+ $ cat urls.txt | node.io -s statuscode
+
+Example 2 - find URLs that 404
+
+ $ cat urls.txt | node.io -s statuscode 404
+
+Example 3 - find URLs that redirect
+
+ $ cat urls.txt | node.io -s statuscode 3
+
### validate
This module is a simple wrapper for [node-validator](https://github.com/chriso/node-validator). Available filters are: [int, url, ip, alpha, alphanumeric, email]
@@ -89,4 +105,4 @@ Example 1 - remove duplicates from a list and output unique lines
Example 2 - to output duplicate lines
$ node.io duplicates find < list.txt
-
+
View
36 builtin/statuscode.coffee
@@ -0,0 +1,36 @@
+usage = '''
+Make a HEAD request to each URL of input and return the status code
+
+ 1. To return the status code (url,status)
+ $ cat urls.txt | node.io -s statuscode
+
+ 2. To find domains that 404
+ $ cat urls.txt | node.io -s statuscode 404
+
+ 3. To find domains that redirect
+ $ cat urls.txt | node.io -s statuscode 3
+'''
+
+nodeio = require 'node.io'
+
+class StatusCode extends nodeio.JobClass
+ init: ->
+ if @options.args.length and @options.args[0] is 'help'
+ @status usage
+ @exit
+
+ run: (url) ->
+ @head url, (err, data, headers, res) =>
+ status = if res? then res.statusCode else '-1'
+ if err
+ if err.length is 3 then status = err
+ if err is 'redirects' then status = 302
+ if @options.args.length and @options.args[0].length is 3
+ if @options.args[0] is ''+status then @emit url else @skip()
+ else if @options.args.length and @options.args[0].length is 1
+ if @options.args[0] is (''+status)[0] then @emit url else @skip()
+ else
+ @emit url + ',' + status
+
+@class = StatusCode
+@job = new StatusCode {timeout: 10, redirects: 0}

0 comments on commit c29e15b

Please sign in to comment.