-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnovel.js
106 lines (90 loc) · 2.7 KB
/
novel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const axios = require('axios')
const AdmZip = require('adm-zip')
const fs = require('fs')
const path = require('path')
const dotenv = require('dotenv');
dotenv.config('./env');
console.log(process.env.filename);
// 定义请求的 URL 和轮询间隔时间(毫秒)
// const url = 'https://example.com/api/endpoint';
const pollInterval = 5000
// 轮询函数
async function poll() {
try {
const imgparams = {
input:`${process.env.prompt}`,
model: 'nai-diffusion-3',
action: 'generate',
parameters: {
width: 832,
height: 1216,
scale: 5,
sampler: 'k_euler',
steps: 28,
n_samples: 1,
ucPreset: 0,
qualityToggle: true,
sm: false,
sm_dyn: false,
dynamic_thresholding: false,
controlnet_strength: 1,
legacy: false,
add_original_image: false,
uncond_scale: 1,
cfg_rescale: 0,
noise_schedule: 'native',
negative_prompt:`${process.env.negative_prompt}`,
},
}
// 发送 POST 请求
const response = await axios.post(
'https://api.novelai.net/ai/generate-image',
imgparams,
{
headers: {
Authorization:
`Bearer ${process.env.token}`,
},
responseType: 'arraybuffer'
}
)
// 定义保存文件的目标文件夹路径
const targetFolderPath = path.join(__dirname, `${process.env.filename}`);
// 创建目标文件夹
if (!fs.existsSync(targetFolderPath)) {
fs.mkdirSync(targetFolderPath);
}
// 检查响应状态码
if (response.status === 200) {
const zipData = response.data
// 创建一个临时文件名
const tempFileName = `${Math.random().toString(36).substring(7)}.zip`
// 将压缩文件保存到临时文件
fs.writeFileSync(tempFileName, zipData, 'binary')
// 解压缩文件
const zip = new AdmZip(tempFileName)
const zipEntries = zip.getEntries()
// 遍历解压缩后的文件并保存
zipEntries.forEach((entry) => {
const entryName = entry.entryName
const randomFileName = `${Math.random()
.toString(36)
.substring(7)}${path.extname(entryName)}`
const outputPath = path.join(targetFolderPath, randomFileName)
// 保存解压缩后的文件
fs.writeFileSync(outputPath, entry.getData(), 'binary')
console.log(`保存文件: ${outputPath}`)
})
// 删除临时文件
fs.unlinkSync(tempFileName)
} else {
console.log(`请求失败,状态码: ${response.status}`)
}
} catch (error) {
console.error('请求出错:', error)
}
// 继续轮询
setTimeout(poll, pollInterval)
}
// 启动轮询
poll()