fetch-prompts.mjs 1.3 KB

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