@@ -21,12 +21,13 @@ import {
21
21
IFetchFileConfig ,
22
22
IFetchPollingConfig ,
23
23
IFetchBaseConifg ,
24
- IFetchCommon ,
25
24
IFileInfo ,
26
25
IFetchHTML ,
27
26
IRequestResItem ,
28
27
IRequestConfig ,
29
- IIntervalTime
28
+ IIntervalTime ,
29
+ IFetchCommon ,
30
+ IFetchCommonArr
30
31
} from './types'
31
32
32
33
export default class XCrawl {
@@ -73,23 +74,24 @@ export default class XCrawl {
73
74
74
75
private async useBatchRequestByMode (
75
76
requestConifg : IRequestConfig | IRequestConfig [ ] ,
76
- intervalTime : IIntervalTime | undefined
77
+ intervalTime : IIntervalTime | undefined ,
78
+ callback : ( requestResItem : IRequestResItem ) => void
77
79
) {
78
80
const requestConfigQueue = isArray ( requestConifg )
79
81
? requestConifg
80
82
: [ requestConifg ]
81
83
82
- let requestRes : IRequestResItem [ ] = [ ]
83
84
if ( this . baseConfig . mode !== 'sync' ) {
84
- requestRes = await batchRequest ( requestConfigQueue , intervalTime )
85
+ await batchRequest ( requestConfigQueue , intervalTime , callback )
85
86
} else {
86
- requestRes = await syncBatchRequest ( requestConfigQueue , intervalTime )
87
+ await syncBatchRequest ( requestConfigQueue , intervalTime , callback )
87
88
}
88
-
89
- return requestRes
90
89
}
91
90
92
- async fetchHTML ( config : IFetchHTMLConfig ) : Promise < IFetchHTML > {
91
+ async fetchHTML (
92
+ config : IFetchHTMLConfig ,
93
+ callback ?: ( res : IFetchHTML ) => void
94
+ ) : Promise < IFetchHTML > {
93
95
const { requestConifg } = this . mergeConfig ( {
94
96
requestConifg : isString ( config ) ? { url : config } : config
95
97
} )
@@ -105,44 +107,50 @@ export default class XCrawl {
105
107
}
106
108
}
107
109
110
+ if ( callback ) {
111
+ callback ( res )
112
+ }
113
+
108
114
return res
109
115
}
110
116
111
- async fetchData < T = any > ( config : IFetchDataConfig ) : Promise < IFetchCommon < T > > {
117
+ async fetchData < T = any > (
118
+ config : IFetchDataConfig ,
119
+ callback ?: ( res : IFetchCommon < T > ) => void
120
+ ) : Promise < IFetchCommonArr < T > > {
112
121
const { requestConifg, intervalTime } = this . mergeConfig ( config )
113
122
114
- const requestRes = await this . useBatchRequestByMode (
115
- requestConifg ,
116
- intervalTime
117
- )
118
-
119
- const container : IFetchCommon < T > = [ ]
120
-
121
- requestRes . forEach ( ( item ) => {
122
- const contentType = item . headers [ 'content-type' ] ?? ''
123
- const rawData = item . data
123
+ const container : IFetchCommonArr < T > = [ ]
124
+ function handleResItem ( requestResItem : IRequestResItem ) {
125
+ const contentType = requestResItem . headers [ 'content-type' ] ?? ''
126
+ const rawData = requestResItem . data
124
127
125
128
const data = contentType . includes ( 'text' )
126
129
? rawData . toString ( )
127
130
: JSON . parse ( rawData . toString ( ) )
128
131
129
- container . push ( { ...item , data } )
130
- } )
132
+ const itemRes = { ...requestResItem , data }
133
+
134
+ if ( callback ) {
135
+ callback ( itemRes )
136
+ }
137
+
138
+ container . push ( itemRes )
139
+ }
140
+
141
+ await this . useBatchRequestByMode ( requestConifg , intervalTime , handleResItem )
131
142
132
143
return container
133
144
}
134
145
135
- async fetchFile ( config : IFetchFileConfig ) : Promise < IFetchCommon < IFileInfo > > {
146
+ async fetchFile (
147
+ config : IFetchFileConfig ,
148
+ callback ?: ( res : IFetchCommon < IFileInfo > ) => void
149
+ ) : Promise < IFetchCommonArr < IFileInfo > > {
136
150
const { requestConifg, intervalTime, fileConfig } = this . mergeConfig ( config )
137
151
138
- const requestRes = await this . useBatchRequestByMode (
139
- requestConifg ,
140
- intervalTime
141
- )
142
-
143
- const container : IFetchCommon < IFileInfo > = [ ]
144
-
145
- requestRes . forEach ( ( requestResItem ) => {
152
+ const container : IFetchCommonArr < IFileInfo > = [ ]
153
+ function handleResItem ( requestResItem : IRequestResItem ) {
146
154
const { id, headers, data } = requestResItem
147
155
148
156
const mimeType = headers [ 'content-type' ] ?? ''
@@ -156,16 +164,24 @@ export default class XCrawl {
156
164
try {
157
165
fs . writeFileSync ( filePath , data )
158
166
159
- container . push ( {
167
+ const res = {
160
168
...requestResItem ,
161
169
data : { fileName, mimeType, size : data . length , filePath }
162
- } )
170
+ }
171
+
172
+ if ( callback ) {
173
+ callback ( res )
174
+ }
175
+
176
+ container . push ( res )
163
177
} catch ( error : any ) {
164
178
log ( logError ( `File save error at id ${ id } : ${ error . message } ` ) )
165
179
}
166
- } )
180
+ }
181
+
182
+ await this . useBatchRequestByMode ( requestConifg , intervalTime , handleResItem )
167
183
168
- const saveTotal = requestRes . length
184
+ const saveTotal = isArray ( requestConifg ) ? requestConifg . length : 1
169
185
const success = container . length
170
186
const error = saveTotal - success
171
187
log (
@@ -188,12 +204,12 @@ export default class XCrawl {
188
204
const total = year + month + day + hour + minute
189
205
190
206
let count = 0
191
- function cb ( ) {
207
+ function startCallback ( ) {
192
208
console . log ( logWarn ( `Start the ${ logWarn . bold ( ++ count ) } polling` ) )
193
209
callback ( count )
194
210
}
195
211
196
- cb ( )
197
- setInterval ( cb , total )
212
+ startCallback ( )
213
+ setInterval ( startCallback , total )
198
214
}
199
215
}
0 commit comments