fetch-prompts.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import fetch from "node-fetch";
  2. import fs from "fs/promises";
  3. const RAW_FILE_URL = "https://raw.githubusercontent.com/";
  4. const MIRRORF_FILE_URL = "https://raw.fgit.ml/";
  5. const RAW_CN_URL = "PlexPt/awesome-chatgpt-prompts-zh/main/prompts-zh.json";
  6. const CN_URL = MIRRORF_FILE_URL + RAW_CN_URL;
  7. const RAW_EN_URL = "f/awesome-chatgpt-prompts/main/prompts.csv";
  8. const EN_URL = MIRRORF_FILE_URL + RAW_EN_URL;
  9. const FILE = "./public/prompts.json";
  10. async function fetchCN() {
  11. console.log("[Fetch] fetching cn prompts...");
  12. try {
  13. const raw = await (await fetch(CN_URL)).json();
  14. return raw.map((v) => [v.act, v.prompt]);
  15. } catch (error) {
  16. console.error("[Fetch] failed to fetch cn prompts", error);
  17. return [];
  18. }
  19. }
  20. async function fetchEN() {
  21. console.log("[Fetch] fetching en prompts...");
  22. try {
  23. const raw = await (await fetch(EN_URL)).text();
  24. return raw
  25. .split("\n")
  26. .slice(1)
  27. .map((v) => v.split('","').map((v) => v.replace('"', "")));
  28. } catch (error) {
  29. console.error("[Fetch] failed to fetch cn prompts", error);
  30. return [];
  31. }
  32. }
  33. async function main() {
  34. Promise.all([fetchCN(), fetchEN()])
  35. .then(([cn, en]) => {
  36. fs.writeFile(FILE, JSON.stringify({ cn, en }));
  37. })
  38. .catch((e) => {
  39. console.error("[Fetch] failed to fetch prompts");
  40. fs.writeFile(FILE, JSON.stringify({ cn: [], en: [] }));
  41. })
  42. .finally(() => {
  43. console.log("[Fetch] saved to " + FILE);
  44. });
  45. }
  46. main();