From 627197185d75feb02a033590140ad619a603085b Mon Sep 17 00:00:00 2001 From: filesite Date: Wed, 10 Apr 2024 23:40:07 +0800 Subject: [PATCH] api for query task ready --- README.md | 20 ++++++++++++++--- common.mjs | 33 +++++++++++++++++++++++++++ router_api.mjs | 50 +++++++++++++++++++++++++++++++++++++++-- test/common.test.mjs | 18 +++++++++++++++ test/heroUnion.test.mjs | 47 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 163 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index eee0384..6d716e0 100644 --- a/README.md +++ b/README.md @@ -116,8 +116,8 @@ https://herounion.filesite.io/api/querytask/ * 请求方法:**GET** * 请求参数: ``` +uuid task_id -timestamp sign ``` @@ -165,6 +165,20 @@ contact: 可选,爬虫提供方联系方式,将在英雄联盟网站展示 * [lang语言代码参考ISO 639-1 Code](https://www.loc.gov/standards/iso639-2/php/code_list.php) +### Hero爬虫查询接口 + +* 接口网址: +``` +https://herounion.filesite.io/api/heros/ +``` +* 请求方法:**GET** +* 请求参数: +``` +page: 可选,从1开始的页码 +limit: 可选,每页数量 +``` + + ### 联盟状态查询接口 * 接口网址: @@ -213,9 +227,9 @@ var sign = md5( JSON.stringify(sortedParams) + token ); ## Hero Union 英雄联盟开发进度 -更新日期:2023-11-05 +更新日期:2024-4-10 -* v0.1 - beta 开发中... +* v0.1 - beta 开发中,完成进度 80% 左右 diff --git a/common.mjs b/common.mjs index db457dc..e97703f 100644 --- a/common.mjs +++ b/common.mjs @@ -110,8 +110,25 @@ class Common { return codes.findIndex((item) => item == status) > -1; } + //判断是否单位为毫秒的时间戳 + isTimestamp(timestamp) { + try { + timestamp = parseInt(timestamp); + }catch(err) { + console.error('Timestamp %s is not a number!', timestamp); + } + + return typeof(timestamp) == 'number' && /^1[0-9]{12}$/.test(timestamp); + } + //判断是否单位为秒的时间戳 isTimestampInSeconds(timestamp) { + try { + timestamp = parseInt(timestamp); + }catch(err) { + console.error('Timestamp %s is not a number!', timestamp); + } + return typeof(timestamp) == 'number' && /^1[0-9]{9}$/.test(timestamp); } @@ -145,6 +162,22 @@ class Common { return /^\w{6,32}$/i.test(uuid); } + //检查task_id是否符合要求:uuid_timestamp + isTaskIdOk(task_id) { + let arr = task_id.split('_'); + if (arr.length < 2) { + return false; + } + + let uuid = arr[0]; + let timestamp = arr[arr.length - 1]; + if (arr.length > 2) { + uuid = task_id.replace(`_${timestamp}`, ''); + } + + return this.isUuidOk(uuid) && this.isTimestamp(timestamp); + } + //检查英文名等参数是否符合标准:5 - 32位字母和下划线的组合 isNormalName(name, minLength, maxLength) { if (typeof(minLength) == 'undefined') {minLength = 6;} diff --git a/router_api.mjs b/router_api.mjs index db30b3a..54a6392 100644 --- a/router_api.mjs +++ b/router_api.mjs @@ -35,7 +35,7 @@ router.get('/', async (req, res) => { /** * 联盟成员向联盟提交数据抓取任务 * - * 参数列表 + * 参数: * uuid: 用户ID * url: 目标网址 * platform: 目标网址所属平台,可选值:[douyin, kuaishou, xigua, bilibili] @@ -112,10 +112,56 @@ router.post('/newtask/', async (req, res) => { /** * 联盟成员向联盟查询某个任务的数据 * + * 参数: + * uuid: 用户ID + * task_id: 任务ID + * sign: 参数签名,签名方法见README.md“接口参数签名方法” **/ router.get('/querytask/', async (req, res) => { + let uuid = req.query.uuid, + task_id = req.query.task_id, + sign = req.query.sign; - return res.send('api/querytask/'); + let data = {code: 0, message: ''}; + + //参数检查 + if (!uuid || !task_id || !sign) { + data.message = '必选参数uuid、task_id、sign不能为空'; + }else if (common.isUuidOk(uuid) == false) { + data.message = '参数uuid应为6-32位的英文字符串,请联系管理员获得'; + }else if (common.isTaskIdOk(task_id) == false) { + data.message = '任务编号task_id格式错误,请使用接口/api/newtask/返回数据里的id属性值'; + }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 = { + uuid: uuid, + task_id: task_id + }; + let mySign = common.sign(paramsCheck, userToken); + if (mySign.toLowerCase() != sign.toLowerCase()) { + data.message = `签名 ${sign} 不匹配,请确保token正确及签名方法跟文档一致`; + } + } + + if (!data.message) { + data.task = heroUnion.getTaskById(task_id); + + if (data.task) { + data.code = 1; + data.message = '获取任务数据完成'; + }else { + data.message = `找不到编号为${task_id}相关的任务数据`; + } + } + + return res.status(200).json(data); }); /** diff --git a/test/common.test.mjs b/test/common.test.mjs index c4c4934..a1293ef 100644 --- a/test/common.test.mjs +++ b/test/common.test.mjs @@ -104,3 +104,21 @@ test('Common function isNormalName test', async (t) => { let case5 = common.isNormalName(md5('test0123456'), 32, 32); assert.equal(case5, true); }); + + +test('Common function isTaskIdOk test', async (t) => { + let case1 = common.isTaskIdOk('test01'); + assert.equal(case1, false); + + let case2 = common.isTaskIdOk('test01_hello'); + assert.equal(case2, false); + + let case3 = common.isTaskIdOk('test0_123456'); + assert.equal(case3, false); + + let case4 = common.isTaskIdOk('test01_0123456789'); + assert.equal(case4, false); + + let case5 = common.isTaskIdOk('test01_1234567890123'); + assert.equal(case5, true); +}); diff --git a/test/heroUnion.test.mjs b/test/heroUnion.test.mjs index 607212e..c6f75d5 100644 --- a/test/heroUnion.test.mjs +++ b/test/heroUnion.test.mjs @@ -85,6 +85,53 @@ test('HeroUnion create task test', async (t) => { assert.equal(response.data.code, 1); }); +test('HeroUnion task query test', async (t) => { + let params = { + uuid: 'herounion_demo', + url: 'https://v.douyin.com/yyy', + platform: 'douyin', + contract: 'tajiantv', + data_mode: 'json', + country: 'cn', + lang: 'zh', + notify_url: 'https://tajian.tv/test/' + }; + let token = 'hello#world!'; + params.sign = common.sign(params, token); + + let api = 'http://127.0.0.1:8080/api/newtask/'; + + const response = await axios.post(api, params, axiosConfig); + console.log(response.data); + + assert.equal(response.status, 200); + assert.equal(response.data.code, 1); + assert.ok(response.data.task); + + //调用查询接口 + let task_id = response.data.task.id; + assert.ok(task_id); + + api = 'http://127.0.0.1:8080/api/querytask/'; + params = { + uuid: 'herounion_demo', + task_id: task_id + }; + params.sign = common.sign(params, token); + + let queryOption = axiosConfig; + queryOption.method = 'get'; + queryOption.url = api; + queryOption.params = params; + + const response2 = await axios(queryOption); + console.log('Query task params', params,); + console.log('Query task result', response2.data); + + assert.equal(response2.status, 200); + assert.equal(response2.data.code, 1); + assert.ok(response2.data.task); +}); test('HeroUnion stats test', async (t) => {