ChatGPT-Next-Web/scripts/fetch-prompts.mjs

65 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-03-27 16:30:12 +00:00
import fetch from "node-fetch";
import fs from "fs/promises";
2023-04-09 12:35:42 +00:00
const RAW_FILE_URL = "https://raw.githubusercontent.com/";
const MIRRORF_FILE_URL = "https://raw.fgit.ml/";
const RAW_CN_URL = "PlexPt/awesome-chatgpt-prompts-zh/main/prompts-zh.json";
const CN_URL = MIRRORF_FILE_URL + RAW_CN_URL;
const RAW_EN_URL = "f/awesome-chatgpt-prompts/main/prompts.csv";
const EN_URL = MIRRORF_FILE_URL + RAW_EN_URL;
2023-03-27 16:30:12 +00:00
const FILE = "./public/prompts.json";
const timeoutPromise = (timeout) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Request timeout'));
}, timeout);
});
};
2023-03-27 16:30:12 +00:00
async function fetchCN() {
console.log("[Fetch] fetching cn prompts...");
try {
// const raw = await (await fetch(CN_URL)).json();
const response = await Promise.race([fetch(CN_URL), timeoutPromise(5000)]);
const raw = await response.json();
2023-03-27 16:30:12 +00:00
return raw.map((v) => [v.act, v.prompt]);
} catch (error) {
console.error("[Fetch] failed to fetch cn prompts", error);
return [];
}
}
async function fetchEN() {
console.log("[Fetch] fetching en prompts...");
try {
// const raw = await (await fetch(EN_URL)).text();
const response = await Promise.race([fetch(EN_URL), timeoutPromise(5000)]);
const raw = response.text();
2023-03-27 16:30:12 +00:00
return raw
.split("\n")
.slice(1)
.map((v) => v.split('","').map((v) => v.replace('"', "")));
} catch (error) {
2023-04-22 15:53:58 +00:00
console.error("[Fetch] failed to fetch en prompts", error);
2023-03-27 16:30:12 +00:00
return [];
}
}
async function main() {
Promise.all([fetchCN(), fetchEN()])
.then(([cn, en]) => {
fs.writeFile(FILE, JSON.stringify({ cn, en }));
})
.catch((e) => {
console.error("[Fetch] failed to fetch prompts");
fs.writeFile(FILE, JSON.stringify({ cn: [], en: [] }));
})
.finally(() => {
console.log("[Fetch] saved to " + FILE);
});
}
main();