1
1
#!/usr/bin/env node
2
2
3
+ 'use strict' ;
4
+
3
5
var http = require ( 'http' ) ;
4
6
var https = require ( 'https' ) ;
5
7
var fs = require ( 'fs' ) ;
@@ -41,63 +43,63 @@ function help() {
41
43
console . log ( 'gdocs.js --login <username>' ) ;
42
44
console . log ( 'gdocs.js --fetch [<docs collection>]' ) ;
43
45
process . exit ( - 1 ) ;
44
- } ;
46
+ }
45
47
46
48
47
49
function fetch ( collection , url ) {
48
50
console . log ( 'fetching a list of docs in collection ' + collection + '...' ) ;
49
51
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 ( )
65
55
}
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
+ ) ;
67
69
}
68
70
69
71
function download ( collection , name , url ) {
70
72
console . log ( 'Downloading:' , name , '...' ) ;
71
73
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' ) ;
81
83
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, '' ) ;
84
86
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, '' ) ;
87
89
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, "'" ) ;
91
93
92
94
93
- data = data + '\n' ;
95
+ data = data + '\n' ;
94
96
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' ) ;
97
99
98
- fs . writeFileSync ( 'docs/content/' + collection + '/' + name , reflow ( data , 100 ) ) ;
99
- }
100
- ) ;
100
+ fs . writeFileSync ( 'docs/content/' + collection + '/' + name , reflow ( data , 100 ) ) ;
101
+ }
102
+ ) ;
101
103
}
102
104
103
105
/**
@@ -111,34 +113,34 @@ function download(collection, name, url) {
111
113
*/
112
114
function login ( username , password ) {
113
115
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'
125
123
} ,
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 ] ;
139
134
}
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' ) ;
140
141
}
141
- ) ;
142
+ }
143
+ ) ;
142
144
}
143
145
144
146
function getAuthToken ( ) {
@@ -152,17 +154,18 @@ function getAuthToken() {
152
154
}
153
155
154
156
function request ( method , url , options , response ) {
155
- var url = url . match ( / h t t p ( s ? ) : \/ \/ ( .+ ?) ( \/ .* ) / ) ;
157
+ url = url . match ( / h t t p ( s ? ) : \/ \/ ( .+ ?) ( \/ .* ) / ) ;
156
158
var isHttps = url [ 1 ] ;
157
- var request = ( isHttps ? https : http ) . request ( {
159
+ var req = ( isHttps ? https : http ) . request ( {
158
160
host : url [ 2 ] ,
159
161
port : ( url [ 1 ] ? 443 : 80 ) ,
160
162
path : url [ 3 ] ,
161
163
method : method
162
164
} , function ( res ) {
165
+ var data ;
163
166
switch ( res . statusCode ) {
164
167
case 200 :
165
- var data = [ ] ;
168
+ data = [ ] ;
166
169
res . setEncoding ( 'utf8' ) ;
167
170
res . on ( 'end' , function ( ) { response ( data . join ( '' ) ) ; } ) ;
168
171
res . on ( 'close' , function ( ) { response ( data . join ( '' ) ) ; } ) ; // https
@@ -173,7 +176,7 @@ function request(method, url, options, response) {
173
176
console . log ( 'Eror: Login credentials expired! Please login.' ) ;
174
177
break ;
175
178
default :
176
- var data = [ ] ;
179
+ data = [ ] ;
177
180
console . log ( 'ERROR: ' , res . statusCode ) ;
178
181
console . log ( 'REQUEST URL: ' , url [ 0 ] ) ;
179
182
console . log ( 'REQUEST POST: ' , options . data ) ;
@@ -186,14 +189,14 @@ function request(method, url, options, response) {
186
189
}
187
190
} ) ;
188
191
for ( var header in options . headers ) {
189
- request . setHeader ( header , options . headers [ header ] ) ;
192
+ req . setHeader ( header , options . headers [ header ] ) ;
190
193
}
191
194
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 ( ) {
194
197
console . log ( 'end' ) ;
195
198
} ) ;
196
- request . end ( ) ;
199
+ req . end ( ) ;
197
200
}
198
201
199
202
function encodeData ( obj ) {
@@ -215,7 +218,9 @@ function askPassword(callback) {
215
218
stdin . on ( "data" , function ( c ) {
216
219
c = c + "" ;
217
220
switch ( c ) {
218
- case "\n" : case "\r" : case "\u0004" :
221
+ case "\n" :
222
+ case "\r" :
223
+ case "\u0004" :
219
224
stdio . setRawMode ( false ) ;
220
225
stdin . pause ( ) ;
221
226
callback ( password ) ;
@@ -227,7 +232,7 @@ function askPassword(callback) {
227
232
password += c ;
228
233
break ;
229
234
}
230
- } )
235
+ } ) ;
231
236
232
237
}
233
238
0 commit comments