mirror of
https://github.com/qitoqito/psyduck.git
synced 2026-01-14 14:40:43 +08:00
parent
973e643d30
commit
dbf05c47c1
127
qlCreate.js
127
qlCreate.js
@ -1,30 +1,21 @@
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {
|
import {fileURLToPath} from 'url';
|
||||||
fileURLToPath
|
|
||||||
} from 'url';
|
|
||||||
import ini from 'ini'
|
import ini from 'ini'
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
class Ql {
|
class Ql {
|
||||||
constructor() {
|
constructor() {
|
||||||
console.log(`Readme: 请先初始化config.ini,打开qitoqito_psyduck/config文件夹,将demo.ini更名config.ini\n 设置QINGLONG_ClientId和QINGLONG_ClientSecret(前面;符号要去掉才能正常解析)\n 如需使用脚本分身,请先自行创建分类ini\n 以京东为例,在指定的iniPath目录(默认qitoqito_psyduck)自行创建jd.ini\n\n [jd_checkCookie]\n map=jd_task_checkCookie\n\n 将上述节点代码复制到jd.ini,jd_checkCookie就能映射到jd_task_checkCookie脚本\n`)
|
const dirpath = fileURLToPath(import.meta.url);
|
||||||
const dirpath = fileURLToPath(
|
|
||||||
import.meta.url);
|
|
||||||
const abspath = path.dirname(dirpath)
|
const abspath = path.dirname(dirpath)
|
||||||
let iniText = fs.readFileSync(`${abspath}/config/config.ini`, 'UTF-8')
|
let iniText = fs.readFileSync(`${abspath}/config/config.ini`, 'UTF-8')
|
||||||
let obj = ini.parse(iniText)
|
let obj = ini.parse(iniText)
|
||||||
this.env = obj.env
|
let env = obj.env
|
||||||
if (this.env.QINGLONG_ClientId) {
|
this.config = {
|
||||||
this.config = {
|
baseURL: env.QINGLONG_BaseUrl || 'http://127.0.0.1:5700',
|
||||||
baseURL: this.env.QINGLONG_BaseUrl || 'http://127.0.0.1:5700',
|
clientId: env.QINGLONG_ClientId,
|
||||||
clientId: this.env.QINGLONG_ClientId,
|
clientSecret: env.QINGLONG_ClientSecret
|
||||||
clientSecret: this.env.QINGLONG_ClientSecret
|
};
|
||||||
};
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.config = null
|
|
||||||
}
|
|
||||||
this.token = null
|
this.token = null
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,11 +39,15 @@ class Ql {
|
|||||||
async setEnvs(data) {
|
async setEnvs(data) {
|
||||||
const token = this.token
|
const token = this.token
|
||||||
try {
|
try {
|
||||||
const response = await axios.put(`${this.config.baseURL}/open/envs`, data, {
|
const response = await axios.put(
|
||||||
headers: {
|
`${this.config.baseURL}/open/envs`,
|
||||||
'Authorization': `Bearer ${token}`
|
data,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[Error] 更新环境变量失败:', error);
|
console.error('[Error] 更新环境变量失败:', error);
|
||||||
@ -163,11 +158,15 @@ class Ql {
|
|||||||
async addCron(command) {
|
async addCron(command) {
|
||||||
const token = this.token
|
const token = this.token
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(`${this.config.baseURL}/open/crons`, command, {
|
const response = await axios.post(
|
||||||
headers: {
|
`${this.config.baseURL}/open/crons`,
|
||||||
'Authorization': `Bearer ${token}`
|
command,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[Error] 创建定时任务失败:', error);
|
console.error('[Error] 创建定时任务失败:', error);
|
||||||
@ -180,15 +179,9 @@ class Ql {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async create() {
|
async create() {
|
||||||
if (!this.config) {
|
|
||||||
consoel.log("请先初始化config.ini,并设置青龙ClientID和ClientSecret")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
await this.getToken()
|
await this.getToken()
|
||||||
let dirpath = fileURLToPath(
|
let dirpath = fileURLToPath(import.meta.url);
|
||||||
import.meta.url);
|
|
||||||
let abspath = path.dirname(dirpath)
|
let abspath = path.dirname(dirpath)
|
||||||
let iniPath = this.env.iniPath || `${abspath}/config`
|
|
||||||
let crons = await this.getCrons()
|
let crons = await this.getCrons()
|
||||||
let data = {}
|
let data = {}
|
||||||
for (let i of crons.data) {
|
for (let i of crons.data) {
|
||||||
@ -208,74 +201,10 @@ class Ql {
|
|||||||
dir.forEach(async function(item, index) {
|
dir.forEach(async function(item, index) {
|
||||||
let stat = fs.lstatSync(`${abspath}/parse/` + item)
|
let stat = fs.lstatSync(`${abspath}/parse/` + item)
|
||||||
if (stat.isDirectory() === true) {
|
if (stat.isDirectory() === true) {
|
||||||
if (fs.existsSync(`${iniPath}/${item}.ini`)) {
|
|
||||||
let iniText = fs.readFileSync(`${iniPath}/${item}.ini`, 'UTF-8')
|
|
||||||
let obj = ini.parse(iniText)
|
|
||||||
for (let filename in obj) {
|
|
||||||
if (obj[filename].map) {
|
|
||||||
let map = obj[filename].map
|
|
||||||
try {
|
|
||||||
let imp = await
|
|
||||||
import (`${abspath}/parse/${item}/${map}.js`)
|
|
||||||
let psyDuck = new imp.Main()
|
|
||||||
let crontab = psyDuck.crontab()
|
|
||||||
let code = `
|
|
||||||
import path from 'path';
|
|
||||||
import {
|
|
||||||
fileURLToPath
|
|
||||||
} from 'url';
|
|
||||||
!(async () => {
|
|
||||||
let dirpath = fileURLToPath(import.meta.url).replace('.swap','');
|
|
||||||
let abspath = path.dirname(dirpath)
|
|
||||||
let filename = dirpath.match(/(\\w+)\\.js/)[1]
|
|
||||||
let type = filename.split('_')[0]
|
|
||||||
if (['js', 'jx', 'jr', 'jw'].includes(type)) {
|
|
||||||
type = 'jd'
|
|
||||||
}
|
|
||||||
let length = process.argv.length
|
|
||||||
let params = {
|
|
||||||
filename,
|
|
||||||
mapping: "${map}",
|
|
||||||
}
|
|
||||||
if (length > 2) {
|
|
||||||
for (let i = 2; i < length; i++) {
|
|
||||||
let key = process.argv[i].match(/^-\\w+$/)
|
|
||||||
if (key) {
|
|
||||||
params[key[0].substr(1)] = process.argv[i + 1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let psyDuck = await import (\`\${abspath}/parse/\${type}/${map}.js\`)
|
|
||||||
let main = new psyDuck.Main()
|
|
||||||
await main.init(params)
|
|
||||||
})().catch((e) => {
|
|
||||||
console.log(e.message)
|
|
||||||
})`
|
|
||||||
fs.writeFile(`${abspath}/${filename}.js`, code, function(err, data) {
|
|
||||||
if (err) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
console.log(`🐯❄️ 写入成功: ${filename}.js`)
|
|
||||||
})
|
|
||||||
if (!data[`${filename}.js`]) {
|
|
||||||
dicts[`${filename}.js`] = {
|
|
||||||
name: `PsyDuck_${psyDuck.profile.title}`,
|
|
||||||
schedule: crontab,
|
|
||||||
command: `task qitoqito_psyduck/${filename}.js`,
|
|
||||||
labels: [`PsyDuck`]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (let script of fs.readdirSync(`${abspath}/parse/${item}`)) {
|
for (let script of fs.readdirSync(`${abspath}/parse/${item}`)) {
|
||||||
try {
|
try {
|
||||||
if (script.match(/\w+\_\w+\_\w/)) {
|
if (script.match(/\w+\_\w+\_\w/)) {
|
||||||
let imp = await
|
let imp = await import(`${abspath}/parse/${item}/${script}`)
|
||||||
import (`${abspath}/parse/${item}/${script}`)
|
|
||||||
let psyDuck = new imp.Main()
|
let psyDuck = new imp.Main()
|
||||||
let crontab = psyDuck.crontab()
|
let crontab = psyDuck.crontab()
|
||||||
let code = `
|
let code = `
|
||||||
@ -336,10 +265,10 @@ import {
|
|||||||
try {
|
try {
|
||||||
let add = await this.addCron(dicts[i])
|
let add = await this.addCron(dicts[i])
|
||||||
if (add.data.name) {
|
if (add.data.name) {
|
||||||
console.log('🐯❄️ 任务添加成功: ', i)
|
console.log('任务添加成功: ', i)
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('🐽🐽️ 任务添加失败: ', i)
|
console.log('任务添加失败: ', i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user