You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.0 KiB
188 lines
5.0 KiB
/** |
|
* 公用方法 |
|
*/ |
|
|
|
import fs from 'node:fs'; |
|
import { readdir, readFile } from 'node:fs/promises'; |
|
import { resolve } from 'node:path'; |
|
import md5 from 'md5'; |
|
|
|
class Common { |
|
|
|
//构造函数,设置默认配置 |
|
constructor() { |
|
this.configDir = resolve('conf/'); |
|
} |
|
|
|
getTimestamp() { |
|
return Math.floor(Date.now()); |
|
} |
|
|
|
getTimestampInSeconds() { |
|
return Math.floor(Date.now() / 1000); |
|
} |
|
|
|
getLocalTimeString(locales, timezone) { |
|
if (typeof(locales) == 'undefined' || !locales) { |
|
locales = 'zh-Hans-CN'; |
|
} |
|
|
|
if (typeof(timezone) == 'undefined' || !timezone) { |
|
timezone = 'Asia/Shanghai'; |
|
} |
|
|
|
let date = new Date(); |
|
let option = {"timeZone": timezone}; |
|
return date.toLocaleString(locales, option); |
|
} |
|
|
|
sortDict(obj) { //dict按key排序 |
|
return Object.keys(obj).sort().reduce(function (result, key) { |
|
result[key] = obj[key]; |
|
return result; |
|
}, {}); |
|
} |
|
|
|
joinDict(obj, glue, separator) { //dict拼接成字符串 |
|
if (typeof(glue) == 'undefined') { |
|
glue = '='; |
|
} |
|
|
|
if (typeof(separator) == 'undefined') { |
|
separator = '&'; |
|
} |
|
|
|
return Object.keys(obj).map(function(key) { |
|
let arr = [key]; |
|
let val = obj[key]; |
|
if (typeof(val) == 'string' || typeof(val) == 'number') { |
|
arr.push(val); |
|
}else if (typeof(val) == 'object') { |
|
arr.push(JSON.stringify(val)); |
|
} |
|
|
|
return arr.join(glue); |
|
}).join(separator); |
|
} |
|
|
|
sign(params, token) { //对参数做MD5签名 |
|
return md5( JSON.stringify(this.sortDict(params)) + token ); |
|
} |
|
|
|
//从conf/目录读取配置文件内容 |
|
async getConfigFromJsonFile(filename) { |
|
let data = null; |
|
|
|
let filePath = this.configDir + `/${filename}`; |
|
if (fs.existsSync(filePath)) { |
|
try { |
|
const contents = await readFile(filePath, { encoding: 'utf8' }); |
|
if (contents) { |
|
data = JSON.parse(contents); |
|
} |
|
} catch (err) { |
|
console.error(`[FAILED] get config content from %s failed, error: %s`, filePath, err.message); |
|
} |
|
}else { |
|
console.error("[ERROR] file %s not exist.", filePath); |
|
} |
|
|
|
return data; |
|
} |
|
|
|
//判断语言代码是否符合国际标准 |
|
isIosLangCode(lang) { |
|
return /^[a-z]{2}$/i.test(lang); |
|
} |
|
|
|
//判断国家代码是否符合国际标准 |
|
isIosCountryCode(country) { |
|
return /^[a-z]{2}$/i.test(country); |
|
} |
|
|
|
//判断爬虫状态代码是否正确 |
|
isBotStatus(status) { |
|
const codes = [ |
|
"idle", |
|
"busy" |
|
]; |
|
|
|
return codes.findIndex((item) => item == status) > -1; |
|
} |
|
|
|
//判断是否单位为秒的时间戳 |
|
isTimestampInSeconds(timestamp) { |
|
return typeof(timestamp) == 'number' && /^1[0-9]{9}$/.test(timestamp); |
|
} |
|
|
|
//检查爬虫名字是否符合标准:6 - 32位字母和下划线的组合 |
|
isBotNameOk(bot_name) { |
|
return /^\w{6,32}$/i.test(bot_name); |
|
} |
|
|
|
//检查爬虫支持的平台是否符合标准:用英文逗号间隔的最长3 - 100个字符的英文字符串 |
|
isPlatformsOk(platforms) { |
|
return /^[\w,]{3,100}$/i.test(platforms); |
|
} |
|
|
|
//检查爬虫支持的合约是否符合标准:用英文逗号间隔的最长3 - 100个字符的英文字符串 |
|
isContractsOk(contracts) { |
|
return /^[\w,]{3,100}$/i.test(contracts); |
|
} |
|
|
|
//检查爬虫提供方联系方式是否符合标准:6 - 50个非空白字符 |
|
isContactOk(contact) { |
|
return /^\S{6,50}$/i.test(contact); |
|
} |
|
|
|
getLogArguments() { |
|
let args = []; |
|
let localTime = this.getLocalTimeString('zh-Hans-CN', 'Asia/Shanghai'); |
|
|
|
if (arguments[0]) { |
|
let logFormat = `[%s] ${arguments[0]}`; |
|
args.push(logFormat); |
|
} |
|
|
|
if (arguments) { |
|
for (const index in arguments) { |
|
if (index > 1) { |
|
args.push(arguments[index]); |
|
}else if (index == 1) { |
|
args.push(localTime); |
|
args.push(arguments[index]); |
|
} |
|
} |
|
} |
|
|
|
return args; |
|
} |
|
|
|
log() { |
|
let args = this.getLogArguments.apply(this, arguments); |
|
console.log.apply(this, args); |
|
return args; |
|
} |
|
|
|
info() { |
|
let args = this.getLogArguments.apply(this, arguments); |
|
console.info.apply(this, args); |
|
return args; |
|
} |
|
|
|
warn() { |
|
let args = this.getLogArguments.apply(this, arguments); |
|
console.warn.apply(this, args); |
|
return args; |
|
} |
|
|
|
error() { |
|
let args = this.getLogArguments.apply(this, arguments); |
|
console.error.apply(this, args); |
|
return args; |
|
} |
|
|
|
} |
|
|
|
let commonFuns = new Common(); |
|
export default commonFuns; |