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)); + }), +);