Union of hero bots.
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.

216 lines
7.7 KiB

12 months ago
/**
* Express router of api/
*/
import express from 'express';
import common from './common.mjs';
import HeroUnion from './heroUnion.mjs';
//初始化爬虫联盟
const heroUnion = new HeroUnion();
heroUnion.init();
12 months ago
const router = express.Router();
router.get('/', async (req, res) => {
const apiList = {
"/api/": "查看所有API",
"/api/onboard/": "爬虫状态上报到联盟",
"/api/stats/": "查看联盟状态",
"/api/newtask/": "向联盟提交新的爬虫任务",
};
const data = {
name: heroUnion.config.name,
version: heroUnion.config.version,
apis: apiList
};
return res.status(200).json(data);
12 months ago
});
/**
* 参数列表
* uuid: 用户ID
* url: 目标网址
* platform: 目标网址所属平台可选值[douyin, kuaishou, xigua, bilibili]
* contract: 需要抓取的数据合约凡是支持此合约的爬虫将根据合约内容抓取数据具体参考爬虫所支持的合约
* data_mode: 返回数据格式可选值[json, html]
* country: 国家代码
* lang: 语言代码
* notify_url: 通知回调网址
* sign: 参数签名签名方法见README.md接口参数签名方法
**/
12 months ago
router.post('/newtask/', async (req, res) => {
let uuid = req.body.uuid,
url = req.body.url,
platform = req.body.platform,
contract = req.body.contract,
data_mode = req.body.data_mode,
country = req.body.country,
lang = req.body.lang,
notify_url = req.body.notify_url,
sign = req.body.sign;
12 months ago
let data = {code: 0, message: ''};
//参数格式检查
if (!uuid || !url || !platform || !contract || !sign) {
data.message = '必选参数uuid、url、platform、contract、sign不能为空';
}else if (common.isUuidOk(uuid) == false) {
data.message = '参数uuid应为6-32位的英文字符串,请联系管理员获得';
}else if (common.isUrlOk(url) == false) {
data.message = '参数url必须是一个网址';
}else if (common.isNormalName(platform, 5) == false) {
data.message = '平台名platform应为5-32位的英文字符串';
}else if (common.isNormalName(contract, 5) == false) {
data.message = '合约contract应为5-32位的英文字符串';
}else if (data_mode && data_mode != 'json' && data_mode != 'html') {
data.message = '数据格式data_mode可选值:json, html';
}else if (country && common.isIosCountryCode(country) == false) {
data.message = '国家代码country请传小写的两位字母,参考两位ISO CODES:https://countrycode.org/';
}else if (lang && common.isIosLangCode(lang) == false) {
data.message = '语言代码lang请传小写的两位字母,参考ISO 639-1 Code:https://www.loc.gov/standards/iso639-2/php/code_list.php';
}else if (notify_url && common.isUrlOk(notify_url) == false) {
data.message = '参数notify_url必须是一个网址';
}else if (common.isNormalName(sign, 32, 32) == false) {
data.message = '签名sign应为32位的英文字符串';
}
//签名检查
let userToken = await heroUnion.getUserToken(uuid);
if (!userToken) {
data.message = `用户 ${uuid} 不存在,请检查参数uuid并确认大小写完整正确`;
}else {
let paramsCheck = {};
for (const key in req.body) {
if (key != 'sign') {
paramsCheck[key] = req.body[key];
}
}
let mySign = common.sign(paramsCheck, userToken);
if (mySign.toLowerCase() != sign.toLowerCase()) {
data.message = `签名 ${sign} 不匹配,请确保token正确及签名方法跟文档一致`;
}
}
if (!data.message) {
data.task = heroUnion.createTask(uuid, url, platform, contract, data_mode, notify_url, country, lang);
data.code = 1;
data.message = '新爬虫任务提交完成';
}
return res.status(200).json(data);
12 months ago
});
router.get('/gettask/', async (req, res) => {
return res.send('api/gettask/');
});
router.post('/savetask/', async (req, res) => {
return res.send('api/savetask/');
});
router.get('/querytask/', async (req, res) => {
return res.send('api/querytask/');
});
8 months ago
/**
* 参数列表
* name
* description
* status: [idle, busy]
* platforms: '', //支持的平台,可由爬虫定义
* contracts: '', //支持的数据抓取合约,具体内容由爬虫定义
8 months ago
* timestamp
* country
* lang
* contact //爬虫提供方的联系方式
8 months ago
*/
12 months ago
router.post('/onboard/', async (req, res) => {
8 months ago
let bot_name = req.body.name,
bot_desc = req.body.description,
status = req.body.status,
platforms = req.body.platforms, //多个则用英文逗号间隔
contracts = req.body.contracts, //多个则用英文逗号间隔
8 months ago
timestamp = req.body.timestamp,
country = req.body.country,
lang = req.body.lang,
contact = req.body.contact;
12 months ago
8 months ago
let data = {
"code": 0,
"message": ""
};
//参数格式检查
if (!bot_name || !bot_desc || !status || !timestamp || !platforms || !contracts) {
data.message = '必填参数name、description、status、platforms、contracts、timestamp不能为空';
8 months ago
}else if (common.isBotNameOk(bot_name) == false) {
data.message = '爬虫名字必须是6 - 32位英文字母、下划线的组合';
}else if (typeof(bot_desc) != 'string' || bot_desc.length > 100) {
data.message = '爬虫简介必须是100个字符以内的字符串';
}else if (common.isBotStatus(status) == false) {
data.message = '爬虫状态status传参错误,其可选值:idle、busy';
}else if (common.isTimestampInSeconds(timestamp) == false) {
data.message = '时间戳timestamp请传秒数';
}else if (common.isPlatformsOk(platforms) == false) {
data.message = '支持的平台platforms应为英文逗号间隔的3 - 100个英文字符串';
}else if (common.isContractsOk(contracts) == false) {
data.message = '支持的合约contracts应为英文逗号间隔的3 - 100个英文字符串';
8 months ago
}else if (country && common.isIosCountryCode(country) == false) {
data.message = '国家代码country请传小写的两位字母,参考两位ISO CODES:https://countrycode.org/';
}else if (lang && common.isIosLangCode(lang) == false) {
data.message = '语言代码lang请传小写的两位字母,参考ISO 639-1 Code:https://www.loc.gov/standards/iso639-2/php/code_list.php';
}else if (contact && common.isContactOk(contact) == false) {
data.message = '联系方式contact应为6 - 50个字符';
8 months ago
}
if (!data.message) {
let bot = {
name: bot_name.toLowerCase(),
description: bot_desc,
status: status,
timestamp: timestamp,
platforms: platforms.split(','),
contracts: contracts.split(','),
contact: contact,
8 months ago
//如果没传则填充默认值
country: country ? country.toLowerCase() : 'cn',
lang: lang ? lang.toLowerCase() : 'zh'
};
heroUnion.heroOnboard(bot);
data.code = 1;
data.message = '欢迎上船,因为有你,联盟将更健壮!';
}
return res.status(200).json(data);
12 months ago
});
router.get('/heros/', async (req, res) => {
let page = req.query.page,
limit = req.query.limit;
if (!page || typeof(page) != 'number') {
page = 1;
}
if (!limit || typeof(limit) != 'number') {
limit = 20;
}
return res.status(200).json(heroUnion.getHeros(page, limit));
});
12 months ago
router.get('/stats/', async (req, res) => {
return res.status(200).json(heroUnion.getStats());
12 months ago
});
export default router;