From 2fe7ddc2e0ed331981b35c127ce3dc567546b89a Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 4 Feb 2024 12:39:47 -0500 Subject: [PATCH 1/2] add updateUsers script --- NOTES | 3 +++ src/fetchCollection.js | 23 ++--------------------- src/lib/db.js | 7 +++++++ src/scripts/updateUsers.js | 13 +++++++++++++ 4 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 src/lib/db.js create mode 100644 src/scripts/updateUsers.js diff --git a/NOTES b/NOTES index dd666d0..f78a9cd 100644 --- a/NOTES +++ b/NOTES @@ -23,3 +23,6 @@ nginx configuration // DONE: add apache redirection PORT=8097 node build/index.js + + +sqlite3 games.db '.mode json' '.once out.json' 'select * from game' diff --git a/src/fetchCollection.js b/src/fetchCollection.js index d30a0c8..237e577 100644 --- a/src/fetchCollection.js +++ b/src/fetchCollection.js @@ -1,12 +1,7 @@ import { load } from 'cheerio'; import fetch from 'node-fetch'; -import DB from 'better-sqlite3'; -const db_file = process.env.DATABASE_URL.replace('sqlite3:', ''); -const db = DB(db_file); -db.pragma('journal_mode = WAL'); - -export async function fetch_guild_users(guild_id) { +export async function fetch_guild_users(db, guild_id) { const res = await fetch( `https://boardgamegeek.com/xmlapi2/guild?members=1&id=` + guild_id, ); @@ -71,7 +66,7 @@ async function fetch_user_forsale(username, n = 1) { throw new Error("couldn't get the collection for " + username); } -async function update_user_games(username) { +export async function update_user_games(db, username) { const games = await fetch_user_forsale(username); const updated_at = new Date().toISOString(); @@ -87,7 +82,6 @@ async function update_user_games(username) { .run({ username }); for (let game of games) { - console.log(game); insert.run({ username, updated_at, @@ -95,16 +89,3 @@ async function update_user_games(username) { }); } } -await update_user_games('gamingduo2') - .then(() => console.log('is done')) - .catch((e) => console.error(e)); - -/* -const usernames = await fetch_guild_users('1610'); - -await Promise.all( - usernames.map( username => { - return update_user_games(username).then( () => console.log(username, 'is done')).catch( e => console.error(e)); - }) -) -*/ diff --git a/src/lib/db.js b/src/lib/db.js new file mode 100644 index 0000000..b18cc7e --- /dev/null +++ b/src/lib/db.js @@ -0,0 +1,7 @@ +import DB from 'better-sqlite3'; + +const db_file = process.env.DATABASE_URL.replace('sqlite3:', ''); + +export const db = DB(db_file); +db.pragma('journal_mode = WAL'); + diff --git a/src/scripts/updateUsers.js b/src/scripts/updateUsers.js new file mode 100644 index 0000000..7a05fb2 --- /dev/null +++ b/src/scripts/updateUsers.js @@ -0,0 +1,13 @@ +import { db } from '../lib/db.js'; + +import { update_user_games } from '../fetchCollection.js'; + +const users = db.prepare('SELECT username from bgg_user').all(); + +await Promise.all( + users.map(({ username }) => { + return update_user_games(db, username) + .then(() => console.log(username, 'is done')) + .catch((e) => console.error(e)); + }), +); From 5b082319f977d5e783e752cb15edd537ca452c54 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 4 Feb 2024 12:40:51 -0500 Subject: [PATCH 2/2] add Taskfile --- Taskfile.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Taskfile.yaml diff --git a/Taskfile.yaml b/Taskfile.yaml new file mode 100644 index 0000000..e126acd --- /dev/null +++ b/Taskfile.yaml @@ -0,0 +1,16 @@ +# https://taskfile.dev + +version: '3' + +vars: + GREETING: Hello, World! + +tasks: + update-game: + cmds: + - node src/scripts/updateUsers.js + + default: + cmds: + - echo "{{.GREETING}}" + silent: true