Added script to populate relays from external source

This commit is contained in:
Alex Gleason 2023-08-14 18:46:35 -05:00
parent f13616a740
commit e3ade42f58
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 28 additions and 3 deletions

View File

@ -6,21 +6,22 @@
"dev": "deno run --allow-read --allow-write=data --allow-env --allow-net --unstable --watch src/server.ts",
"debug": "deno run --allow-read --allow-write=data --allow-env --allow-net --unstable --inspect src/server.ts",
"test": "DB_PATH=\":memory:\" deno test --allow-read --allow-write=data --allow-env --unstable src",
"check": "deno check --unstable src/server.ts"
"check": "deno check --unstable src/server.ts",
"relays:sync": "deno run -A --unstable scripts/relays.ts sync",
},
"imports": {
"@/": "./src/",
"~/": "./"
},
"lint": {
"include": ["src/"],
"include": ["src/", "scripts/"],
"rules": {
"tags": ["recommended"],
"exclude": ["no-explicit-any"]
}
},
"fmt": {
"include": ["src/"],
"include": ["src/", "scripts/"],
"useTabs": false,
"lineWidth": 120,
"indentWidth": 2,

23
scripts/relays.ts Normal file
View File

@ -0,0 +1,23 @@
import { addRelays } from '@/db/relays.ts';
import { filteredArray } from '@/schema.ts';
import { relaySchema } from '~/src/utils.ts';
switch (Deno.args[0]) {
case 'sync':
await sync(Deno.args.slice(1));
break;
default:
console.log('Usage: deno run -A scripts/relays.ts sync <url>');
}
async function sync([url]: string[]) {
if (!url) {
console.error('Error: please provide a URL');
Deno.exit(1);
}
const response = await fetch(url);
const data = await response.json();
const values = filteredArray(relaySchema).parse(data) as `wss://${string}`[];
await addRelays(values);
console.log(`Done: added ${values.length} relays.`);
}

View File

@ -164,6 +164,7 @@ export {
paginationSchema,
parseBody,
parseNip05,
relaySchema,
sha256,
};