fetch-prompts.mjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 = "http://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_TW_URL = "PlexPt/awesome-chatgpt-prompts-zh/main/prompts-zh-TW.json";
  8. const TW_URL = MIRRORF_FILE_URL + RAW_TW_URL;
  9. const RAW_EN_URL = "f/awesome-chatgpt-prompts/main/prompts.csv";
  10. const EN_URL = MIRRORF_FILE_URL + RAW_EN_URL;
  11. const FILE = "./public/prompts.json";
  12. const ignoreWords = ["涩涩", "魅魔", "澀澀"];
  13. const timeoutPromise = (timeout) => {
  14. return new Promise((resolve, reject) => {
  15. setTimeout(() => {
  16. reject(new Error("Request timeout"));
  17. }, timeout);
  18. });
  19. };
  20. async function fetchCN() {
  21. console.log("[Fetch] fetching cn prompts...");
  22. try {
  23. const response = await Promise.race([fetch(CN_URL), timeoutPromise(5000)]);
  24. const raw = await response.json();
  25. return raw
  26. .map((v) => [v.act, v.prompt])
  27. .filter(
  28. (v) =>
  29. v[0] &&
  30. v[1] &&
  31. ignoreWords.every((w) => !v[0].includes(w) && !v[1].includes(w)),
  32. );
  33. } catch (error) {
  34. console.error("[Fetch] failed to fetch cn prompts", error);
  35. return [];
  36. }
  37. }
  38. async function fetchTW() {
  39. console.log("[Fetch] fetching tw prompts...");
  40. try {
  41. const response = await Promise.race([fetch(TW_URL), timeoutPromise(5000)]);
  42. const raw = await response.json();
  43. return raw
  44. .map((v) => [v.act, v.prompt])
  45. .filter(
  46. (v) =>
  47. v[0] &&
  48. v[1] &&
  49. ignoreWords.every((w) => !v[0].includes(w) && !v[1].includes(w)),
  50. );
  51. } catch (error) {
  52. console.error("[Fetch] failed to fetch tw prompts", error);
  53. return [];
  54. }
  55. }
  56. async function fetchEN() {
  57. console.log("[Fetch] fetching en prompts...");
  58. try {
  59. // const raw = await (await fetch(EN_URL)).text();
  60. const response = await Promise.race([fetch(EN_URL), timeoutPromise(5000)]);
  61. const raw = await response.text();
  62. return raw
  63. .split("\n")
  64. .slice(1)
  65. .map((v) =>
  66. v
  67. .split('","')
  68. .map((v) => v.replace(/^"|"$/g, "").replaceAll('""', '"'))
  69. .filter((v) => v[0] && v[1]),
  70. );
  71. } catch (error) {
  72. console.error("[Fetch] failed to fetch en prompts", error);
  73. return [];
  74. }
  75. }
  76. async function main() {
  77. Promise.all([fetchCN(), fetchTW(), fetchEN()])
  78. .then(([cn, tw, en]) => {
  79. fs.writeFile(FILE, JSON.stringify({ cn, tw, en }));
  80. })
  81. .catch((e) => {
  82. console.error("[Fetch] failed to fetch prompts");
  83. fs.writeFile(FILE, JSON.stringify({ cn: [], tw: [], en: [] }));
  84. })
  85. .finally(() => {
  86. console.log("[Fetch] saved to " + FILE);
  87. });
  88. }
  89. main();