Last active
May 21, 2019 11:36
-
-
Save xiadd/a03cf9a551aeb75299a1690f9aaee69c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 请预先安装axios,signale,async | |
* 并发请自行修改 | |
*/ | |
const axios = require('axios') | |
const signale = require('signale') | |
const url = require('url') | |
const fs = require('fs') | |
const mapLimit = require('async/mapLimit') | |
const LIST_URL = 'https://api.tuwan.com/apps/Welfare/getMenuList?format=json&page=1' | |
const DETAIL_URL = 'https://api.tuwan.com/apps/Welfare/detail?format=json&id=1292&' | |
/** | |
* 获取图片列表 | |
* @param {number} page 当前页 | |
*/ | |
async function getMenuList(page = 1) { | |
try { | |
const response = await axios.request({ | |
url: LIST_URL, | |
method: 'GET', | |
params: { page } | |
}) | |
const data = response.data | |
if (data.error !== 0) { | |
return signale.error('发生了一个错误') | |
} | |
const listData = data.data | |
return listData | |
} catch (error) { | |
throw new Error(error) | |
} | |
} | |
/** | |
* 获取图片详情 | |
* @param {any} id 图片id | |
*/ | |
async function getDetail(id) { | |
try { | |
const response = await axios.request({ | |
url: DETAIL_URL, | |
method: 'GET', | |
params: { id } | |
}) | |
const data = response.data | |
if (data.error !== 0) { | |
return signale.error('发生了一个错误') | |
} | |
return data | |
} catch (error) { | |
signale.error(error) | |
} | |
} | |
/** | |
* 保存远程文件zip文件到本地 | |
* @param {string} remoteUrl 远程文件地址 | |
* @param {string} name 文件名 | |
*/ | |
async function saveFile(remoteUrl, dir) { | |
try { | |
const response = await axios({ | |
method: 'GET', | |
url: remoteUrl, | |
responseType: 'stream' | |
}) | |
response.data.pipe(fs.createWriteStream(dir)) | |
.on('close', () => { }) | |
.on('error', error => signale.error(error)) | |
} catch (err) { | |
signale.error(err) | |
} | |
} | |
function getBigPic(thumbUrl) { | |
const imgPahtname = url.parse(thumbUrl).pathname.split('/') | |
const b64 = imgPahtname[4] | |
const imgArray = Buffer.from(b64, 'base64').toString().split(',') | |
imgArray[1] = 1000 | |
imgArray[2] = 0 | |
const newImgArray = imgArray.join(',') | |
const newB64 = Buffer.from(newImgArray).toString('base64') | |
imgPahtname[4] = newB64 | |
const newPathname = imgPahtname.join('/') | |
return `http://img4.tuwandata.com${newPathname}` | |
} | |
async function start() { | |
try { | |
fs.statSync(`./assets`) | |
} catch (error) { | |
fs.mkdirSync(`./assets`) | |
} | |
for (let page = 1; page <= 32; page++) { | |
const listData = await getMenuList(page) | |
mapLimit(listData, 1, async function (img) { | |
try { | |
const detail = await getDetail(img.id) | |
const thumb = detail.thumb | |
const title = detail.title | |
try { | |
fs.statSync(`./assets/${title}-${detail.id}`) | |
} catch (error) { | |
fs.mkdirSync(`./assets/${title}-${detail.id}`) | |
} | |
thumb.forEach(async (img, index) => { | |
const bigPicUrl = getBigPic(img) | |
await saveFile(bigPicUrl, `./assets/${title}-${detail.id}/${index}.jpg`) | |
signale.success(`${new Date()} 保存${title}-${detail.id}成功`) | |
}) | |
} catch (error) { | |
signale.error(error) | |
} | |
}) | |
} | |
} | |
// 执行代码 | |
start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment