-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetImageList.ts
28 lines (26 loc) · 1.1 KB
/
GetImageList.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import * as iconv from 'iconv-lite'
import * as cheerio from 'cheerio'
import * as path from 'path'
import {request, logger} from './settings'
import {PostInfo, SingleImage} from './Interfaces'
export default async (postInfo: PostInfo): Promise<SingleImage[]> => {
const imageInfoList: SingleImage[]= []
logger.info(`requesting post [${postInfo.postUrl}] images url`)
let htmlStream = await request(postInfo.postUrl)
let $ = cheerio.load(iconv.decode(htmlStream, 'GBK'))
let inputs = $('input[type="image"]')
for (let i = 0; i < inputs.length; i++) {
const imageInfo = <SingleImage>{}
imageInfo.url = $(inputs[i]).attr('data-src') || $(inputs[i]).attr('src')
if (typeof imageInfo.url !== 'string') {
console.warn(inputs[i], imageInfo.url)
}
imageInfo.index = i + 1
imageInfo.id = path.parse(imageInfo.url).name + 'I' + (i + 1)
imageInfo.downloaded = false
imageInfo.retryTime = 0
imageInfoList.push(imageInfo)
}
logger.info(`get post's [${postInfo.postUrl}] images url successfully`)
return imageInfoList
}