From 6eea9a0678e7fe9e0d575ce40649f2b5a2cc6ffc Mon Sep 17 00:00:00 2001 From: master Date: Mon, 18 Dec 2023 18:19:46 +0800 Subject: [PATCH] function createTask, getWaitingTask done --- heroUnion.mjs | 131 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 127 insertions(+), 4 deletions(-) diff --git a/heroUnion.mjs b/heroUnion.mjs index ab6f758..2cc14c0 100644 --- a/heroUnion.mjs +++ b/heroUnion.mjs @@ -27,19 +27,142 @@ class HeroUnion { //构造函数,设置默认配置 constructor() { this.stats = {}; + this.tasks = []; + this.taskStatus = { + 'total': 0, + 'waiting': 0, + 'running': 0, + 'done': 0, + 'failed': 0 + }; + this.statusCode = { + 'waiting': '待处理', + 'running': '处理中', + 'done': '完成', + 'failed': '失败' + }; + + this.supportedPlatforms = { + 'douyin': true, + 'kuaishou': true, + 'xigua': true, + 'bilibili': true + }; + } + + //公用方法 + getTimestamp() { + return Math.floor(Date.now()); + } + getTimestampInSeconds() { + return Math.floor(Date.now() / 1000); } //--任务相关功能-- - //提交新任务 - createTask() { + //根据任务提交者ID和时间戳生成任务ID编号 + generateTaskId(uuid) { + let timestamp = this.getTimestamp(); + return `${uuid}_${timestamp}`; + } + + isSupportedPlatform(platform) { + return typeof(this.supportedPlatforms[platform]) != 'undefined' && this.supportedPlatforms[platform]; + } + //提交新任务 + /** + * { + * id: '', + * status: '', + * uuid: '', + * country: '', + * lang: '', + * url: '', + * platform: '', + * data_mode: '', + * notify_url: '', + * } + **/ + createTask(uuid, url, platform, data_mode, notify_url, country, lang) { + let task = { + id: this.generateTaskId(uuid), + status: 'waiting', + + //必选 + uuid: uuid, + url: url, + platform: platform, + + //可选 + data_mode: 'default', + country: 'china', + lang: 'zh-CN', + notify_url: '' + }; + + if (typeof(data_mode) != 'undefined' && data_mode) { + task.data_mode = data_mode; + } + if (typeof(notify_url) != 'undefined' && notify_url) { + task.notify_url = notify_url; + } + if (typeof(country) != 'undefined' && country) { + task.country = country; + } + if (typeof(lang) != 'undefined' && lang) { + task.lang = lang; + } + + this.tasks.push(task); + this.taskStatus.total ++; + this.taskStatus.waiting ++; + + return task; } - //获取 1 个待处理的任务 - getWaitingTask() { + //参数均可选,获取 1 个待处理的任务 + getWaitingTask(platform, country, lang, data_mode) { + let searchResult = null; + + for (const task of this.tasks) { + if (task.status == 'waiting') { + if (typeof(platform) != 'undefined' && platform) { + if (platform == task.platform) { + searchResult = task; + }else { + continue; + } + } + + if (typeof(country) != 'undefined' && country) { + if (country == task.country) { + searchResult = task; + }else { + continue; + } + } + + if (typeof(lang) != 'undefined' && lang) { + if (lang == task.lang) { + searchResult = task; + }else { + continue; + } + } + + if (typeof(data_mode) != 'undefined' && data_mode) { + if (data_mode == task.data_mode) { + searchResult = task; + }else { + continue; + } + } + } + } + return searchResult; } //保存任务处理结果