blowater/DevOps/stats.ts
2024-01-02 01:28:10 +08:00

51 lines
1.1 KiB
TypeScript

import { DB } from "https://deno.land/x/sqlite@v3.7.2/mod.ts";
import { NostrKind } from "../libs/nostr.ts/nostr.ts";
import { ConnectionPool } from "../libs/nostr.ts/relay-pool.ts";
// Open a database
const db = new DB("stats.sqlite");
db.execute(`
CREATE TABLE IF NOT EXISTS stats (
pubkey TEXT,
eventID TEXT,
PRIMARY KEY (pubkey, eventID)
)
`);
const pool = new ConnectionPool();
const urls = [
"wss://relay.damus.io",
"wss://nos.lol",
"wss://eden.nostr.land",
"wss://brb.io",
"wss://sg.qemura.xyz",
"wss://nostr-sg.com",
"wss://nostr-pub.wellorder.net",
"wss://relay.snort.social",
"wss://offchain.pub",
];
for (const url of urls) {
pool.addRelayURL(url);
}
const r = await pool.newSub("stats", {
kinds: [NostrKind.Custom_App_Data],
});
if (r instanceof Error) {
throw r;
}
for await (const { res: e, url } of r.chan) {
console.log(url);
if (e.type != "EVENT") {
continue;
}
const pub = e.event.pubkey;
try {
db.query("INSERT INTO stats (pubkey, eventID) VALUES (?, ?)", [pub, e.event.id]);
} catch (e) {
console.log(e.message);
}
}