Skip to content

Commit

Permalink
修复获取Prompts脚本报错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
nick3 committed Sep 27, 2023
1 parent 9bb2ba4 commit 8402ae3
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions scripts/fetch-prompts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@ import fetch from "node-fetch";
import fs from "fs/promises";

const RAW_FILE_URL = "https://raw.githubusercontent.com/";
const MIRRORF_FILE_URL = "https://raw.fgit.ml/";
// const MIRRORF_FILE_URL = "http://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 CN_URL = RAW_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;
const EN_URL = RAW_FILE_URL + RAW_EN_URL;
const FILE = "./public/prompts.json";

const ignoreWords = ["涩涩", "魅魔"];

const timeoutPromise = (timeout) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Request timeout'));
reject(new Error("Request timeout"));
}, timeout);
});
};

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();
return raw.map((v) => [v.act, v.prompt]);
return raw
.map((v) => [v.act, v.prompt])
.filter(
(v) =>
v[0] &&
v[1] &&
ignoreWords.every((w) => !v[0].includes(w) && !v[1].includes(w)),
);
} catch (error) {
console.error("[Fetch] failed to fetch cn prompts", error);
return [];
Expand All @@ -40,7 +48,12 @@ async function fetchEN() {
return raw
.split("\n")
.slice(1)
.map((v) => v.split('","').map((v) => v.replace('"', "")));
.map((v) =>
v
.split('","')
.map((v) => v.replace(/^"|"$/g, "").replaceAll('""', '"'))
.filter((v) => v[0] && v[1]),
);
} catch (error) {
console.error("[Fetch] failed to fetch en prompts", error);
return [];
Expand All @@ -61,4 +74,4 @@ async function main() {
});
}

main();
main();

0 comments on commit 8402ae3

Please sign in to comment.