blowater/DevOps/stats.ts

51 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-06-30 14:05:57 +00:00
import { DB } from "https://deno.land/x/sqlite@v3.7.2/mod.ts";
2023-08-28 17:58:05 +00:00
import { NostrKind } from "../lib/nostr-ts/nostr.ts";
import { ConnectionPool } from "../lib/nostr-ts/relay.ts";
2023-06-30 14:05:57 +00:00
// 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.CustomAppData],
});
if (r instanceof Error) {
throw r;
}
2023-08-31 00:05:28 +00:00
for await (const { res: e, url } of r.chan) {
2023-06-30 14:05:57 +00:00
console.log(url);
2023-07-01 08:56:02 +00:00
if (e.type != "EVENT") {
2023-06-30 14:05:57 +00:00
continue;
}
2023-07-01 08:56:02 +00:00
const pub = e.event.pubkey;
2023-06-30 14:05:57 +00:00
try {
2023-07-01 08:56:02 +00:00
db.query("INSERT INTO stats (pubkey, eventID) VALUES (?, ?)", [pub, e.event.id]);
} catch (e) {
console.log(e.message);
}
2023-06-30 14:05:57 +00:00
}