@@ -54,7 +54,7 @@ function fetch(collection, url){
5454 if ( title . match ( / \. n g d o c $ / ) ) {
5555 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 ] ;
5656 download ( collection , title , exportUrl ) ;
57- }
57+ } ;
5858 } ) ;
5959 }
6060 ) ;
@@ -136,7 +136,13 @@ function login(username, password){
136136}
137137
138138function getAuthToken ( ) {
139- return fs . readFileSync ( 'tmp/gdocs.auth' ) ;
139+ var pwdFile = 'tmp/gdocs.auth' ;
140+ try {
141+ fs . statSync ( pwdFile ) ;
142+ return fs . readFileSync ( pwdFile ) ;
143+ } catch ( e ) {
144+ console . log ( 'Please log in first...' ) ;
145+ }
140146}
141147
142148function request ( method , url , options , response ) {
@@ -147,14 +153,29 @@ function request(method, url, options, response) {
147153 path : url [ 3 ] ,
148154 method : method
149155 } , function ( res ) {
150- var data = [ ] ;
151- res . setEncoding ( 'utf8' ) ;
152- res . on ( 'end' , function ( ) {
153- response ( data . join ( '' ) ) ;
154- } ) ;
155- res . on ( 'data' , function ( chunk ) {
156- data . push ( chunk ) ;
157- } ) ;
156+ switch ( res . statusCode ) {
157+ case 200 : {
158+ var data = [ ] ;
159+ res . setEncoding ( 'utf8' ) ;
160+ res . on ( 'end' , function ( ) {
161+ response ( data . join ( '' ) ) ;
162+ } ) ;
163+ res . on ( 'data' , function ( chunk ) {
164+ data . push ( chunk ) ;
165+ } ) ;
166+ res . on ( 'error' , function ( e ) {
167+ console . log ( e ) ;
168+ } ) ;
169+ break ;
170+ }
171+ case 401 : {
172+ console . log ( 'Eror: Login credentials expired! Please login.' ) ;
173+ break ;
174+ }
175+ default : {
176+ console . log ( res ) ;
177+ }
178+ }
158179 } ) ;
159180 for ( var header in options . headers ) {
160181 request . setHeader ( header , options . headers [ header ] ) ;
0 commit comments