Last active
August 9, 2022 06:56
-
-
Save millionfor/c144eef4c9da1e1ea73135b95e429361 to your computer and use it in GitHub Desktop.
A script to download iconfont font library based on js
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
/** | |
* @fileName getIconfontData | |
* @dateTime 2021/11/18 15:30:25 <Thursday> | |
* @author QuanQuan <millionfor@apache.org> | |
* @gistId c144eef4c9da1e1ea73135b95e429361 | |
* @description Get Iconfont data | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
const request = require('request'); | |
const puppeteer = require('puppeteer'); | |
const ora = require('ora'); | |
const rimraf = require('rimraf'); | |
const StreamZip = require('node-stream-zip'); | |
require('shelljs/global'); | |
const iconfont = { | |
account: '', | |
password: '', | |
projectId: '' | |
}; | |
const spinner = ora('Loading iconfont script').start(); | |
async function getIconfontData() { | |
spinner.text = '[Iconfont] => Loading iconfont browser'; | |
const browser = await puppeteer.launch({ | |
headless: true, | |
slowMo: 250, // 250 | |
timeout: 0, | |
devtools: true, | |
args: ['--no-sandbox', '--disable-setuid-sandbox'] | |
}); | |
// 跳转iconfont | |
const page = await browser.newPage(); | |
await page.goto('https://www.iconfont.cn/login', { | |
waitUntil: 'networkidle2' | |
}); | |
spinner.text = '[Iconfont] => Loading logging in'; | |
await page.waitForSelector('#userid'); | |
const inputText = await page.$('#userid'); | |
await inputText.click(); | |
await inputText.type(iconfont.account); | |
await page.waitForSelector('#password'); // Wait for the password input box to load | |
const inputPwd = await page.$('#password'); // Get password input box | |
await inputPwd.click(); // Prevent detection and analyze specific situations | |
await inputPwd.type(iconfont.password); | |
await page.waitForSelector('button.mx-form-btn'); // Wait for the confirmation button to load | |
const submit = await page.$('button.mx-form-btn'); // Get confirmation button | |
await submit.click(); | |
spinner.succeed('[Iconfont] => Loading login successfully'); | |
spinner.stop(); | |
let cookies = await page.cookies(); | |
let str = ''; | |
cookies.map(v => { | |
str += `${v.name}=${v.value}; `; | |
}); | |
let downloadUrl = `https://www.iconfont.cn/api/project/download.zip?spm=a313x.7781069.1998910419.d7543c303&pid=${iconfont.projectId}&ctoken=6iKLYVTRBfatn6xjhmNtTwhs`; | |
let downloadDir = path.normalize(__dirname); | |
let downloadFile = 'download.zip'; | |
let dirAndFile = downloadDir + path.sep + downloadFile; | |
let assetsDir = path.resolve(__dirname, '../src/style'); | |
let iconfontDir = path.resolve(__dirname, '../src/style/fonts'); | |
await browser.close(); | |
request | |
.get(downloadUrl, { headers: { 'cookie': str, 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' } }, () => { | |
rimraf.sync(iconfontDir); | |
fs.renameSync(path.resolve(__dirname, '../', downloadFile), dirAndFile); | |
spinner.succeed('[Iconfont] => Download font file successfully'); | |
next(); | |
}) | |
.on('error', err => { | |
throw err; | |
}) | |
.pipe(fs.createWriteStream(downloadFile)); | |
async function unzip() { | |
const zip = new StreamZip.async({ file: dirAndFile }); | |
await zip.extract(null, assetsDir); | |
const readDir = fs.readdirSync(assetsDir).filter(item => item.indexOf('font_') > -1); | |
const fontsDir = path.resolve(assetsDir, 'fonts'); | |
const nIconfontDir = `${assetsDir}/iconfont.scss`; | |
fs.renameSync(path.resolve(assetsDir, readDir[0]), fontsDir); | |
cp('-f', `${iconfontDir}/iconfont.css`, nIconfontDir); | |
sed('-i', 'font-size: 16px;', ' ', nIconfontDir); | |
sed('-i', 'iconfont', './fonts/iconfont', nIconfontDir); | |
zip.on('extract', (entry, file) => { | |
console.log(`Extracted ${entry.name} to ${file}`); | |
}); | |
await zip.close(); | |
} | |
async function next() { | |
if (fs.existsSync(dirAndFile)) { | |
spinner.succeed('[Iconfont] => delete old files'); | |
rimraf.sync(iconfontDir); | |
spinner.succeed('[Iconfont] => decompress'); | |
await unzip(); | |
spinner.succeed('[Iconfont] => clear temporary files'); | |
rimraf.sync(dirAndFile); | |
spinner.succeed('[Iconfont] => Finish'); | |
spinner.stop(); | |
} | |
} | |
} | |
getIconfontData(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment