Download server.js from txtorg/code: direct link, hf CLI and curl.
- Browser
- Download file 4.5 kB
-
https://huggingface.co/spaces/txtorg/code/resolve/main/server.js
- Command line
-
hf download hf://spaces/txtorg/code/server.js
-
curl -L -o server.js https://huggingface.co/spaces/txtorg/code/resolve/main/server.js
4.5 kB
| const express = require("express"); | |
| const fs = require("fs"); | |
| const app = express(); | |
| const PORT = 7860; | |
| const CODES_FILE = "codes.json"; | |
| // Function to generate a 6-character alphanumeric code | |
| function generateCode() { | |
| return Math.random().toString(36).substring(2, 8).toUpperCase(); | |
| } | |
| // Function to get current Unix timestamp | |
| function getUnixTimestamp() { | |
| return Math.floor(Date.now() / 1000); | |
| } | |
| // Ensure codes.json exists | |
| if (!fs.existsSync(CODES_FILE)) { | |
| fs.writeFileSync(CODES_FILE, JSON.stringify({}), "utf8"); | |
| } | |
| const AMOUNTS_FILE = "amounts.json"; | |
| const AMOUNTS_FILE_2 = "amounts2.json"; | |
| // Function to get current Unix timestamp | |
| function getUnixTimestamp() { | |
| return Math.floor(Date.now() / 1000); | |
| } | |
| // Ensure amounts.json exists | |
| if (!fs.existsSync(AMOUNTS_FILE)) { | |
| fs.writeFileSync(AMOUNTS_FILE, JSON.stringify({}), "utf8"); | |
| } | |
| app.get("/add", (req, res) => { | |
| const chatId = req.query.chatid; | |
| const amount = parseInt(req.query.amount, 10); | |
| if (!chatId || isNaN(amount) || amount <= 0) { | |
| return res.status(400).json({ error: "Valid chatid and amount are required" }); | |
| } | |
| const timestamp = getUnixTimestamp(); | |
| // Load existing amounts | |
| let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE, "utf8")); | |
| // Store the new amount | |
| if (!amounts[chatId]) { | |
| amounts[chatId] = []; | |
| } | |
| amounts[chatId].push({ amount, timestamp }); | |
| // Save to amounts.json | |
| fs.writeFileSync(AMOUNTS_FILE, JSON.stringify(amounts, null, 2), "utf8"); | |
| res.json({ chatId, amount, timestamp }); | |
| }); | |
| // API to get stored amounts for a chat ID | |
| app.get("/get", (req, res) => { | |
| const chatId = req.query.chatid; | |
| if (!chatId) { | |
| return res.status(400).json({ error: "chatid is required" }); | |
| } | |
| // Load existing amounts | |
| let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE, "utf8")); | |
| if (!amounts[chatId]) { | |
| return res.status(404).json({ error: "No amounts found for this chat ID" }); | |
| } | |
| res.json({ chatId, amounts: amounts[chatId] }); | |
| }); | |
| // API to generate a new code | |
| app.get("/api/generate", (req, res) => { | |
| const chatId = req.query.chatid; | |
| if (!chatId) { | |
| return res.status(400).json({ error: "chatid is required" }); | |
| } | |
| const code = generateCode(); | |
| const timestamp = getUnixTimestamp(); | |
| // Load existing codes | |
| let codes = JSON.parse(fs.readFileSync(CODES_FILE, "utf8")); | |
| // Store the new code | |
| if (!codes[chatId]) { | |
| codes[chatId] = []; | |
| } | |
| codes[chatId].push({ code, timestamp }); | |
| // Save to codes.json | |
| fs.writeFileSync(CODES_FILE, JSON.stringify(codes, null, 2), "utf8"); | |
| res.json({ chatId, code, timestamp }); | |
| }); | |
| // API to get recent codes for a chat ID | |
| app.get("/api/get", (req, res) => { | |
| const chatId = req.query.q; | |
| if (!chatId) { | |
| return res.status(400).json({ error: "q (chatid) is required" }); | |
| } | |
| // Load existing codes | |
| let codes = JSON.parse(fs.readFileSync(CODES_FILE, "utf8")); | |
| if (!codes[chatId]) { | |
| return res.status(404).json({ error: "No codes found for this chat ID" }); | |
| } | |
| res.json({ chatId, codes: codes[chatId] }); | |
| }); | |
| app.get("/add2", (req, res) => { | |
| const chatId = req.query.chatid; | |
| const amount = parseInt(req.query.amount, 10); | |
| if (!chatId || isNaN(amount) || amount <= 0) { | |
| return res.status(400).json({ error: "Valid chatid and amount are required" }); | |
| } | |
| const timestamp = getUnixTimestamp(); | |
| // Load existing amounts2 | |
| let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE_2, "utf8")); | |
| // Store the new amount | |
| if (!amounts[chatId]) { | |
| amounts[chatId] = []; | |
| } | |
| amounts[chatId].push({ amount, timestamp }); | |
| // Save to amounts2.json | |
| fs.writeFileSync(AMOUNTS_FILE_2, JSON.stringify(amounts, null, 2), "utf8"); | |
| res.json({ chatId, amount, timestamp }); | |
| }); | |
| // API to get stored amounts for a chat ID (Version 2) | |
| app.get("/get2", (req, res) => { | |
| const chatId = req.query.chatid; | |
| if (!chatId) { | |
| return res.status(400).json({ error: "chatid is required" }); | |
| } | |
| // Load existing amounts2 | |
| let amounts = JSON.parse(fs.readFileSync(AMOUNTS_FILE_2, "utf8")); | |
| if (!amounts[chatId]) { | |
| return res.status(404).json({ error: "No amounts found for this chat ID" }); | |
| } | |
| res.json({ chatId, amounts: amounts[chatId] }); | |
| }); | |
| // Start server | |
| app.listen(PORT, () => { | |
| console.log(`Server running on port ${PORT}`); | |
| }); | |