Skip to content

Commit 0edc053

Browse files
committed
Fix fetchFile API file save is lost
1 parent 5bd0f08 commit 0edc053

File tree

6 files changed

+108
-76
lines changed

6 files changed

+108
-76
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "x-crawl",
4-
"version": "0.1.4",
4+
"version": "0.1.5",
55
"author": "CoderHxl",
66
"description": "XCrawl is a Nodejs multifunctional crawler library. Crawl HTML, JSON, file resources, etc. through simple configuration.",
77
"license": "MIT",

publish/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "x-crawl",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"author": "CoderHxl",
55
"description": "XCrawl is a Nodejs multifunctional crawler library. Crawl HTML, JSON, file resources, etc. through simple configuration.",
66
"license": "MIT",

src/index.ts

+42-33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ import path from 'node:path'
33
import { JSDOM } from 'jsdom'
44

55
import { batchRequest, syncBatchRequest, request } from './request'
6-
import { isArray, isString, isUndefined, log, logError } from './utils'
6+
import {
7+
isArray,
8+
isString,
9+
isUndefined,
10+
log,
11+
logError,
12+
logNumber,
13+
logSuccess
14+
} from './utils'
715

816
import {
917
IXCrawlBaseConifg,
@@ -135,39 +143,40 @@ export default class XCrawl {
135143
intervalTime
136144
)
137145

138-
return new Promise((resolve) => {
139-
const container: IFetchCommon<IFileInfo> = []
140-
141-
requestRes.forEach((requestResItem, index) => {
142-
const { id, statusCode, headers, data } = requestResItem
143-
144-
const mimeType = headers['content-type'] ?? ''
145-
const suffix = mimeType.split('/').pop()
146-
const fileName = new Date().getTime().toString()
147-
const filePath = path.resolve(
148-
fileConfig.storeDir,
149-
`${fileName}.${suffix}`
150-
)
151-
152-
fs.createWriteStream(filePath, 'binary').write(data, (err) => {
153-
if (err) {
154-
log(logError(`File save error at id ${id}: ${err.message}`))
155-
} else {
156-
const fileInfo: IFileInfo = {
157-
fileName,
158-
mimeType,
159-
size: data.length,
160-
filePath
161-
}
162-
163-
container.push({ id, statusCode, headers, data: fileInfo })
164-
}
165-
166-
if (index === requestRes.length - 1) {
167-
resolve(container)
168-
}
146+
const container: IFetchCommon<IFileInfo> = []
147+
148+
requestRes.forEach((requestResItem) => {
149+
const { id, headers, data } = requestResItem
150+
151+
const mimeType = headers['content-type'] ?? ''
152+
const suffix = mimeType.split('/').pop()
153+
const fileName = new Date().getTime().toString()
154+
const filePath = path.resolve(
155+
fileConfig.storeDir,
156+
`${fileName}.${suffix}`
157+
)
158+
159+
try {
160+
fs.writeFileSync(filePath, data)
161+
162+
container.push({
163+
...requestResItem,
164+
data: { fileName, mimeType, size: data.length, filePath }
169165
})
170-
})
166+
} catch (error: any) {
167+
log(logError(`File save error at id ${id}: ${error.message}`))
168+
}
171169
})
170+
171+
const saveTotal = requestRes.length
172+
const success = container.length
173+
const error = requestRes.length - container.length
174+
log(
175+
`saveTotal: ${logNumber(saveTotal)}, success: ${logSuccess(
176+
success
177+
)}, error: ${logError(error)}`
178+
)
179+
180+
return container
172181
}
173182
}

src/request.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export async function batchRequest(
208208
error.forEach((message) => log(logError(message)))
209209

210210
log(
211-
`total: ${logNumber(requestConifgs.length)}, success: ${logSuccess(
211+
`requestsTotal: ${logNumber(requestConifgs.length)}, success: ${logSuccess(
212212
success.length
213213
)}, error: ${logError(error.length)}`
214214
)
@@ -255,7 +255,7 @@ export async function syncBatchRequest(
255255
log(logSuccess('All requests are over!'))
256256

257257
log(
258-
`total: ${logNumber(requestConifgs.length)}, success: ${logSuccess(
258+
`requestsTotal: ${logNumber(requestConifgs.length)}, success: ${logSuccess(
259259
successTotal
260260
)}, error: ${logError(errorTotal)}`
261261
)

test/start/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/start/index.ts

+61-38
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,77 @@ import XCrawl from '../../src'
33

44
const testXCrawl = new XCrawl({
55
timeout: 10000,
6-
intervalTime: {
7-
max: 3000,
8-
min: 2000
9-
},
6+
intervalTime: { max: 2000, min: 1000 },
107
mode: 'async'
118
})
129

13-
testXCrawl
14-
.fetchData({
15-
requestConifg: [
16-
{ url: 'http://localhost:3001/home' },
17-
{ url: 'http://localhost:9001/api/home/wonderfulplace' },
18-
{ url: 'http://localhost:9001/api/home/goodprice' },
19-
{ url: 'http://localhost:3001/home' },
20-
{ url: 'http://localhost:9001/ai/home/goodprice' }
21-
]
22-
})
23-
.then((res) => {
24-
// console.log(res)
10+
// testXCrawl
11+
// .fetchData({
12+
// requestConifg: [
13+
// { url: 'http://localhost:3001/home' },
14+
// { url: 'http://localhost:9001/api/home/wonderfulplace' },
15+
// { url: 'http://localhost:9001/api/home/goodprice' },
16+
// { url: 'http://localhost:3001/home' },
17+
// { url: 'http://localhost:9001/ai/home/goodprice' }
18+
// ]
19+
// })
20+
// .then((res) => {
21+
// // console.log(res)
22+
// })
23+
24+
testXCrawl.fetchHTML({ url: 'https://www.bilibili.com/' }).then((res) => {
25+
const { jsdom } = res.data
26+
27+
const document = jsdom.window.document
28+
const imgBoxEl = document.querySelectorAll('.bili-video-card__cover')
29+
30+
const imgUrls: string[] = []
31+
imgBoxEl.forEach((item, index) => {
32+
const img = item.lastChild as HTMLImageElement
33+
34+
if (index % 2) {
35+
imgUrls.push('https:' + img.src)
36+
} else {
37+
imgUrls.push(img.src)
38+
}
2539
})
2640

27-
// testXCrawl.fetchHTML({ url: 'https://www.bilibili.com/' }).then((res) => {
28-
// const { jsdom } = res.data
41+
console.log(imgUrls)
2942

30-
// const document = jsdom.window.document
31-
// const imgBoxEl = document.querySelectorAll('.bili-video-card__cover')
43+
const requestConifg = imgUrls.map((url) => ({ url }))
3244

33-
// const imgUrls: string[] = []
34-
// imgBoxEl.forEach((item, index) => {
35-
// const img = item.lastChild as HTMLImageElement
45+
testXCrawl
46+
.fetchFile({
47+
requestConifg,
48+
fileConfig: { storeDir: path.resolve(__dirname, './upload') }
49+
})
50+
.then((res) => {
51+
// console.log(res)
52+
})
53+
})
3654

37-
// if (index % 2) {
38-
// imgUrls.push('https:' + img.src)
39-
// } else {
40-
// imgUrls.push(img.src)
55+
// testXCrawl
56+
// .fetchData({
57+
// requestConifg: {
58+
// url: 'http://localhost:9001/api/area/阳江市',
59+
// method: 'POST',
60+
// data: {
61+
// type: 'plus',
62+
// offset: 0,
63+
// size: 20
64+
// }
4165
// }
4266
// })
67+
// .then((res) => {
68+
// const room = res[0].data.data.list[0]
69+
// const requestConifg = room.pictureUrls.map((item: any) => ({
70+
// url: item
71+
// }))
4372

44-
// console.log(imgUrls)
45-
46-
// const requestConifg = imgUrls.map((url) => ({ url }))
47-
48-
// testXCrawl
49-
// .fetchFile({
73+
// testXCrawl.fetchFile({
5074
// requestConifg,
51-
// fileConfig: { storeDir: path.resolve(__dirname, './upload') }
52-
// })
53-
// .then((res) => {
54-
// console.log(res)
75+
// fileConfig: {
76+
// storeDir: path.resolve(__dirname, './upload')
77+
// }
5578
// })
56-
// })
79+
// })

0 commit comments

Comments
 (0)