11#!/usr/bin/env node
22
3+ 'use strict' ;
4+
35var http = require ( 'http' ) ;
46var https = require ( 'https' ) ;
57var fs = require ( 'fs' ) ;
@@ -41,63 +43,63 @@ function help() {
4143 console . log ( 'gdocs.js --login <username>' ) ;
4244 console . log ( 'gdocs.js --fetch [<docs collection>]' ) ;
4345 process . exit ( - 1 ) ;
44- } ;
46+ }
4547
4648
4749function fetch ( collection , url ) {
4850 console . log ( 'fetching a list of docs in collection ' + collection + '...' ) ;
4951 request ( 'GET' , url , {
50- headers : {
51- 'Gdata-Version' : '3.0' ,
52- 'Authorization' : 'GoogleLogin auth=' + getAuthToken ( )
53- }
54- } ,
55- function ( chunk ) {
56- var entries = chunk . split ( '<entry' ) ;
57- entries . shift ( ) ;
58- entries . forEach ( function ( entry ) {
59- var title = entry . match ( / < t i t l e > ( .* ?) < \/ t i t l e > / ) [ 1 ] ;
60- if ( title . match ( / \. n g d o c $ / ) ) {
61- var exportUrl = entry . match ( / < c o n t e n t t y p e = ' t e x t \/ h t m l ' s r c = ' ( .* ?) ' \/ > / ) [ 1 ] ;
62- download ( collection , title , exportUrl ) ;
63- } ;
64- } ) ;
52+ headers : {
53+ 'Gdata-Version' : '3.0' ,
54+ 'Authorization' : 'GoogleLogin auth=' + getAuthToken ( )
6555 }
66- ) ;
56+ } ,
57+ function ( chunk ) {
58+ var entries = chunk . split ( '<entry' ) ;
59+ entries . shift ( ) ;
60+ entries . forEach ( function ( entry ) {
61+ var title = entry . match ( / < t i t l e > ( .* ?) < \/ t i t l e > / ) [ 1 ] ;
62+ if ( title . match ( / \. n g d o c $ / ) ) {
63+ var exportUrl = entry . match ( / < c o n t e n t t y p e = ' t e x t \/ h t m l ' s r c = ' ( .* ?) ' \/ > / ) [ 1 ] ;
64+ download ( collection , title , exportUrl ) ;
65+ }
66+ } ) ;
67+ }
68+ ) ;
6769}
6870
6971function download ( collection , name , url ) {
7072 console . log ( 'Downloading:' , name , '...' ) ;
7173 request ( 'GET' , url + '&exportFormat=txt' ,
72- {
73- headers : {
74- 'Gdata-Version' : '3.0' ,
75- 'Authorization' : 'GoogleLogin auth=' + getAuthToken ( )
76- }
77- } ,
78- function ( data ) {
79- data = data . replace ( '\ufeff' , '' ) ;
80- data = data . replace ( / \r \n / mg, '\n' ) ;
74+ {
75+ headers : {
76+ 'Gdata-Version' : '3.0' ,
77+ 'Authorization' : 'GoogleLogin auth=' + getAuthToken ( )
78+ }
79+ } ,
80+ function ( data ) {
81+ data = data . replace ( '\ufeff' , '' ) ;
82+ data = data . replace ( / \r \n / mg, '\n' ) ;
8183
82- // strip out all text annotations
83- data = data . replace ( / \[ [ a - z A - Z ] { 1 , 2 } \] / mg, '' ) ;
84+ // strip out all text annotations
85+ data = data . replace ( / \[ [ a - z A - Z ] { 1 , 2 } \] / mg, '' ) ;
8486
85- // strip out all docos comments
86- data = data . replace ( / ^ [ ^ \s _ ] + : \n \S + [ \S \s ] * $ / m, '' ) ;
87+ // strip out all docos comments
88+ data = data . replace ( / ^ [ ^ \s _ ] + : \n \S + [ \S \s ] * $ / m, '' ) ;
8789
88- // fix smart-quotes
89- data = data . replace ( / [ “ ” ] / g, '"' ) ;
90- data = data . replace ( / [ ‘ ’ ] / g, "'" ) ;
90+ // fix smart-quotes
91+ data = data . replace ( / [ “ ” ] / g, '"' ) ;
92+ data = data . replace ( / [ ‘ ’ ] / g, "'" ) ;
9193
9294
93- data = data + '\n' ;
95+ data = data + '\n' ;
9496
95- //this should be a bug in Google Doc API, hence need to remove this once the bug is fixed
96- data = data . replace ( / \n \n / g, '\n' ) ;
97+ //this should be a bug in Google Doc API, hence need to remove this once the bug is fixed
98+ data = data . replace ( / \n \n / g, '\n' ) ;
9799
98- fs . writeFileSync ( 'docs/content/' + collection + '/' + name , reflow ( data , 100 ) ) ;
99- }
100- ) ;
100+ fs . writeFileSync ( 'docs/content/' + collection + '/' + name , reflow ( data , 100 ) ) ;
101+ }
102+ ) ;
101103}
102104
103105/**
@@ -111,34 +113,34 @@ function download(collection, name, url) {
111113 */
112114function login ( username , password ) {
113115 request ( 'POST' , 'https://www.google.com/accounts/ClientLogin' ,
114- {
115- data : {
116- Email : username ,
117- Passwd : password ,
118- accountType : 'GOOGLE' ,
119- service : 'writely' ,
120- 'Gdata-version' : '3.0'
121- } ,
122- headers : {
123- 'Content-type' : 'application/x-www-form-urlencoded'
124- }
116+ {
117+ data : {
118+ Email : username ,
119+ Passwd : password ,
120+ accountType : 'GOOGLE' ,
121+ service : 'writely' ,
122+ 'Gdata-version' : '3.0'
125123 } ,
126- function ( chunk ) {
127- var token ;
128- chunk . split ( '\n' ) . forEach ( function ( line ) {
129- var parts = line . split ( '=' ) ;
130- if ( parts [ 0 ] == 'Auth' ) {
131- token = parts [ 1 ] ;
132- }
133- } ) ;
134- if ( token ) {
135- fs . writeFileSync ( 'tmp/gdocs.auth' , token ) ;
136- console . log ( "logged in, token saved in 'tmp/gdocs.auth'" ) ;
137- } else {
138- console . log ( 'failed to log in' ) ;
124+ headers : {
125+ 'Content-type' : 'application/x-www-form-urlencoded'
126+ }
127+ } ,
128+ function ( chunk ) {
129+ var token ;
130+ chunk . split ( '\n' ) . forEach ( function ( line ) {
131+ var parts = line . split ( '=' ) ;
132+ if ( parts [ 0 ] == 'Auth' ) {
133+ token = parts [ 1 ] ;
139134 }
135+ } ) ;
136+ if ( token ) {
137+ fs . writeFileSync ( 'tmp/gdocs.auth' , token ) ;
138+ console . log ( "logged in, token saved in 'tmp/gdocs.auth'" ) ;
139+ } else {
140+ console . log ( 'failed to log in' ) ;
140141 }
141- ) ;
142+ }
143+ ) ;
142144}
143145
144146function getAuthToken ( ) {
@@ -152,17 +154,18 @@ function getAuthToken() {
152154}
153155
154156function request ( method , url , options , response ) {
155- var url = url . match ( / h t t p ( s ? ) : \/ \/ ( .+ ?) ( \/ .* ) / ) ;
157+ url = url . match ( / h t t p ( s ? ) : \/ \/ ( .+ ?) ( \/ .* ) / ) ;
156158 var isHttps = url [ 1 ] ;
157- var request = ( isHttps ? https : http ) . request ( {
159+ var req = ( isHttps ? https : http ) . request ( {
158160 host : url [ 2 ] ,
159161 port : ( url [ 1 ] ? 443 : 80 ) ,
160162 path : url [ 3 ] ,
161163 method : method
162164 } , function ( res ) {
165+ var data ;
163166 switch ( res . statusCode ) {
164167 case 200 :
165- var data = [ ] ;
168+ data = [ ] ;
166169 res . setEncoding ( 'utf8' ) ;
167170 res . on ( 'end' , function ( ) { response ( data . join ( '' ) ) ; } ) ;
168171 res . on ( 'close' , function ( ) { response ( data . join ( '' ) ) ; } ) ; // https
@@ -173,7 +176,7 @@ function request(method, url, options, response) {
173176 console . log ( 'Eror: Login credentials expired! Please login.' ) ;
174177 break ;
175178 default :
176- var data = [ ] ;
179+ data = [ ] ;
177180 console . log ( 'ERROR: ' , res . statusCode ) ;
178181 console . log ( 'REQUEST URL: ' , url [ 0 ] ) ;
179182 console . log ( 'REQUEST POST: ' , options . data ) ;
@@ -186,14 +189,14 @@ function request(method, url, options, response) {
186189 }
187190 } ) ;
188191 for ( var header in options . headers ) {
189- request . setHeader ( header , options . headers [ header ] ) ;
192+ req . setHeader ( header , options . headers [ header ] ) ;
190193 }
191194 if ( options . data )
192- request . write ( encodeData ( options . data ) ) ;
193- request . on ( 'end' , function ( ) {
195+ req . write ( encodeData ( options . data ) ) ;
196+ req . on ( 'end' , function ( ) {
194197 console . log ( 'end' ) ;
195198 } ) ;
196- request . end ( ) ;
199+ req . end ( ) ;
197200}
198201
199202function encodeData ( obj ) {
@@ -215,7 +218,9 @@ function askPassword(callback) {
215218 stdin . on ( "data" , function ( c ) {
216219 c = c + "" ;
217220 switch ( c ) {
218- case "\n" : case "\r" : case "\u0004" :
221+ case "\n" :
222+ case "\r" :
223+ case "\u0004" :
219224 stdio . setRawMode ( false ) ;
220225 stdin . pause ( ) ;
221226 callback ( password ) ;
@@ -227,7 +232,7 @@ function askPassword(callback) {
227232 password += c ;
228233 break ;
229234 }
230- } )
235+ } ) ;
231236
232237}
233238
0 commit comments