feat: add cron and manual trigger

This commit is contained in:
2026-07-24 11:29:24 +05:30
parent 7207fcfab3
commit cc7ec85a5d
4 changed files with 169 additions and 128 deletions

124
fx.ts Normal file
View File

@@ -0,0 +1,124 @@
import { $ } from "bun"
const API =
"https://api.frankfurter.app/latest?from=USD&to=INR"
export async function getUSD() {
const res = await fetch(API)
if (!res.ok) {
throw new Error(`Exchange API failed: ${res.status}`)
}
const data = await res.json()
const rate = (data as { rates: { INR: number } }).rates.INR
if (typeof rate !== "number") {
throw new Error("Invalid exchange rate")
}
const helions = (rate / 100).toFixed(2)
return `**📈 Exchange Rate**
$1 = ₹${rate.toFixed(2)} = ⚙${helions}
`
}
export async function getUsage() {
//
// CPU LOAD
//
const loadavg = await $`cat /proc/loadavg`.text()
const [load1] = loadavg.trim().split(" ")
//
// MEMORY
//
const memRaw = await $`free -b`.text()
const memLine = memRaw
.split("\n")
.find((line) => line.startsWith("Mem:"))
if (!memLine) {
throw new Error("Failed to parse memory info")
}
const [
,
totalMem,
usedMem,
] = memLine.trim().split(/\s+/)
//
// DISK
//
const diskRaw = await $`df -B1 /`.text()
const diskLine = diskRaw
.split("\n")[1]
if (!diskLine) {
throw new Error("Failed to parse disk info")
}
const [
,
totalDisk,
usedDisk,
] = diskLine.trim().split(/\s+/)
const temp = await $`vcgencmd measure_temp`.text()
const tempData = temp.split("=")?.[1]?.replace("'", "°")
//
// HELPERS
//
function formatBytesIEC(bytes: number) {
const units = ["B", "KiB", "MiB", "GiB", "TiB"]
let i = 0
let value = bytes
while (value >= 1024 && i < units.length - 1) {
value /= 1024
i++
}
return `${value.toFixed(1)} ${units[i]}`
}
//
// OUTPUT
//
return `**💾 Usage**
• **CPU:** ${load1}
• **Memory:** ${formatBytesIEC(Number(usedMem))} / ${formatBytesIEC(Number(totalMem))}
• **Disk:** ${formatBytesIEC(Number(usedDisk))} / ${formatBytesIEC(Number(totalDisk))}
• **Temperature:** ${tempData}
`
}
export async function getWeather() {
const res = await fetch(`https://wttr.in/Bengaluru?format="%c%C| %f | %h | %m | %w | %p"`)
const data = await res.text()
const [weather, temp, humidity, moon, winds, preci] = data.replace(/\"/g, '').split("|")
return `**🌥️ Weather Report**
• **Weather:** ${weather}
• **Temperature:** ${temp}
• **Humidity:** ${humidity}
• **Moon Phase:** ${moon}
• **Winds:** ${winds}
• **Precipitation:** ${preci}
`
}