@@ -5,13 +5,19 @@ const { join } = require('path');
55const { PostsQuery, PostQuery } = require ( './query' ) ;
66
77class Crawler {
8- constructor ( username , { delay } ) {
8+ constructor ( username , { delay, cert } ) {
99 this . username = username ;
1010
1111 // options
1212 this . delay = delay ;
13+ this . cert = cert ;
1314
1415 this . __grahpqlURL = 'https://v2.velog.io/graphql' ;
16+ this . __api = axios . create ( {
17+ headers :{
18+ Cookie : cert ? `access_token=${ cert } ;` : null ,
19+ } ,
20+ } ) ;
1521 }
1622
1723 async parse ( ) {
@@ -34,7 +40,7 @@ class Crawler {
3440 let posts = [ ] ;
3541
3642 try {
37- await axios . get ( url ) ;
43+ await this . __api . get ( url ) ;
3844 } catch ( e ) {
3945 if ( e . response . status === 404 ) {
4046 console . error ( `โ ๏ธ ํด๋น ์ ์ ๋ฅผ ์ฐพ์ ์ ์์ด์ \n username = ${ this . username } ` ) ;
@@ -47,9 +53,9 @@ class Crawler {
4753 while ( true ) {
4854 try {
4955 if ( response && response . data . data . posts . length >= 20 ) {
50- response = await axios . post ( this . __grahpqlURL , PostsQuery ( this . username , posts [ posts . length - 1 ] . id ) ) ;
56+ response = await this . __api . post ( this . __grahpqlURL , PostsQuery ( this . username , posts [ posts . length - 1 ] . id ) ) ;
5157 } else {
52- response = await axios . post ( this . __grahpqlURL , PostsQuery ( this . username ) ) ;
58+ response = await this . __api . post ( this . __grahpqlURL , PostsQuery ( this . username ) ) ;
5359 }
5460 } catch ( e ) {
5561 console . error ( `โ ๏ธ ๋ฒจ๋ก๊ทธ์์ ๊ธ ๋ชฉ๋ก์ ๊ฐ์ ธ์ค๋๋ฐ ์คํจํ์ต๋๋ค. \n error = ${ e } ` ) ;
@@ -69,7 +75,7 @@ class Crawler {
6975 let response ;
7076
7177 try {
72- response = await axios . post ( this . __grahpqlURL , PostQuery ( this . username , url_slug ) ) ;
78+ response = await this . __api . post ( this . __grahpqlURL , PostQuery ( this . username , url_slug ) ) ;
7379 } catch ( e ) {
7480 console . error ( `โ ๏ธ ๋ฒจ๋ก๊ทธ์์ ๊ธ์ ๊ฐ์ ธ์ค๋๋ฐ ์คํจํ์ต๋๋ค. \n error = ${ e } url = ${ url_slug } ` ) ;
7581 process . exit ( 1 ) ;
@@ -107,7 +113,7 @@ class Crawler {
107113 const filename = url . replace ( / \/ \s * $ / , '' ) . split ( '/' ) . slice ( - 2 ) . join ( '-' ) . trim ( ) ;
108114 const path = join ( 'backup' , 'images' , decodeURI ( filename ) ) ;
109115
110- axios ( {
116+ this . __api ( {
111117 method : 'get' ,
112118 url : encodeURI ( decodeURI ( url ) ) ,
113119 responseType : 'stream' ,
0 commit comments