@@ -5,7 +5,7 @@ const { join } = require('path');
55const { PostsQuery, PostQuery } = require ( './query' ) ;
66
77class Crawler {
8- constructor ( username , { delay, cert } ) {
8+ constructor ( username , { delay, cert, withDetail } ) {
99 this . username = username ;
1010
1111 if ( ! username ) {
@@ -16,6 +16,7 @@ class Crawler {
1616 // options
1717 this . delay = delay ;
1818 this . cert = cert ;
19+ this . withDetail = withDetail ;
1920
2021 this . __grahpqlURL = 'https://v2.velog.io/graphql' ;
2122 this . __api = axios . create ( {
@@ -104,22 +105,37 @@ class Crawler {
104105 }
105106
106107 const path = join ( 'backup' , 'content' , `${ title } .md` ) ;
107-
108- post . body = '---\n'
109- + `title: "${ post . title } "\n`
110- + `description: "${ post . short_description . replace ( / \n / g, ' ' ) } "\n`
111- + `date: ${ post . released_at } \n`
112- + `tags: ${ JSON . stringify ( post . tags ) } \n`
113- + '---\n' + post . body ;
108+ let frontmatter = '---\n'
109+ + `title: "${ post . title } "\n`
110+ + `description: "${ post . short_description . replace ( / \n / g, ' ' ) } "\n`
111+ + `date: ${ post . released_at } \n`
112+ + `tags: ${ JSON . stringify ( post . tags ) } \n` ;
114113
114+ if ( this . withDetail ) {
115+ if ( post . thumbnail ) {
116+ this . getThumbnailImage ( post . thumbnail ) ;
117+ frontmatter += `thumbnail: ${ post . thumbnail } \n` ;
118+ }
119+
120+ if ( post . series ) {
121+ frontmatter = frontmatter
122+ + `series:\n`
123+ + ` id: ${ post . series . id } \n`
124+ + ` name: ${ post . series . name } \n` ;
125+ }
126+ }
127+
128+ frontmatter += '---\n' ;
129+ post . body = frontmatter + post . body ;
130+
115131 try {
116132 await fs . promises . writeFile ( path , post . body , 'utf8' ) ;
117133 } catch ( e ) {
118134 console . error ( `⚠️ 파일을 쓰는데 문제가 발생했습니다. / error = ${ e } title = ${ post . title } ` ) ;
119135 }
120136 }
121137
122- async getImage ( body ) {
138+ getImage ( body ) {
123139 const regex = / ! \[ ( [ ^ \] ] * ) \] \( ( .* ?.p n g | .* ?.j p e g | .* ?.j p g | .* ?.w e b p | .* ?.s v g | .* ?.g i f | .* ?.t i f f ) \s * ( " (?: .* [ ^ " ] ) " ) ? \s * \) | ! \[ [ ^ \] ] * \] \( ( .* ?) \s * ( " (?: .* [ ^ " ] ) " ) ? \s * \) / g;
124140
125141 body = body . replace ( regex , ( _ , alt , url ) => {
@@ -142,6 +158,20 @@ class Crawler {
142158 return body ;
143159 }
144160
161+ getThumbnailImage ( url ) {
162+ const filename = url . replace ( / \/ \s * $ / , '' ) . split ( '/' ) . slice ( - 2 ) . join ( '-' ) . trim ( ) ;
163+ const path = join ( 'backup' , 'images' , decodeURI ( filename ) ) ;
164+
165+ this . __api ( {
166+ method : 'get' ,
167+ url : encodeURI ( decodeURI ( url ) ) ,
168+ responseType : 'stream' ,
169+ } )
170+ . then ( resp => resp . data . pipe ( fs . createWriteStream ( path ) ) )
171+ . catch ( e => console . error ( `⚠️ 이미지를 다운 받는데 오류가 발생했습니다 / url = ${ url } , e = ${ e } ` ) ) ;
172+
173+ return `/images/${ filename } ` ;
174+ }
145175} ;
146176
147177module . exports = Crawler ;
0 commit comments