@@ -5,7 +5,7 @@ const { join } = require('path');
5
5
const { PostsQuery, PostQuery } = require ( './query' ) ;
6
6
7
7
class Crawler {
8
- constructor ( username , { delay, cert } ) {
8
+ constructor ( username , { delay, cert, withDetail } ) {
9
9
this . username = username ;
10
10
11
11
if ( ! username ) {
@@ -16,6 +16,7 @@ class Crawler {
16
16
// options
17
17
this . delay = delay ;
18
18
this . cert = cert ;
19
+ this . withDetail = withDetail ;
19
20
20
21
this . __grahpqlURL = 'https://v2.velog.io/graphql' ;
21
22
this . __api = axios . create ( {
@@ -104,22 +105,37 @@ class Crawler {
104
105
}
105
106
106
107
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` ;
114
113
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
+
115
131
try {
116
132
await fs . promises . writeFile ( path , post . body , 'utf8' ) ;
117
133
} catch ( e ) {
118
134
console . error ( `⚠️ 파일을 쓰는데 문제가 발생했습니다. / error = ${ e } title = ${ post . title } ` ) ;
119
135
}
120
136
}
121
137
122
- async getImage ( body ) {
138
+ getImage ( body ) {
123
139
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;
124
140
125
141
body = body . replace ( regex , ( _ , alt , url ) => {
@@ -142,6 +158,20 @@ class Crawler {
142
158
return body ;
143
159
}
144
160
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
+ }
145
175
} ;
146
176
147
177
module . exports = Crawler ;
0 commit comments