Spaces:
Sleeping

File size: 932 Bytes
90b3952
 
 
 
 
 
 
 
 
 
 
 
 
7087ddd
 
90b3952
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { promises as fs } from "fs";
import { fileURLToPath } from "url";
import path from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

/**
 * Reads a JSON dataset from the local file system and returns a random text.
 * @returns {Promise<string>} A random text string from the dataset.
 */
export async function getRandomText() {
  try {
    const filePath = path.join(__dirname, "../../tmp", "text.json");

    const fileContent = await fs.readFile(filePath, "utf-8");
    const data = JSON.parse(fileContent);

    if (!Array.isArray(data) || data.length === 0) {
      throw new Error("Dataset is empty or not an array.");
    }

    const randomIndex = Math.floor(Math.random() * data.length);
    return data[randomIndex];
  } catch (error) {
    console.error("Error in getRandomText:", error);
    throw new Error("Error fetching text from local dataset.");
  }
}