Merge branch 'update-script'

This commit is contained in:
Yanick Champoux 2024-02-04 12:40:57 -05:00
commit a52712f447
5 changed files with 41 additions and 21 deletions

3
NOTES
View File

@ -23,3 +23,6 @@ nginx configuration
// DONE: add apache redirection // DONE: add apache redirection
PORT=8097 node build/index.js PORT=8097 node build/index.js
sqlite3 games.db '.mode json' '.once out.json' 'select * from game'

16
Taskfile.yaml Normal file
View File

@ -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

View File

@ -1,12 +1,7 @@
import { load } from 'cheerio'; import { load } from 'cheerio';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import DB from 'better-sqlite3';
const db_file = process.env.DATABASE_URL.replace('sqlite3:', ''); export async function fetch_guild_users(db, guild_id) {
const db = DB(db_file);
db.pragma('journal_mode = WAL');
export async function fetch_guild_users(guild_id) {
const res = await fetch( const res = await fetch(
`https://boardgamegeek.com/xmlapi2/guild?members=1&id=` + guild_id, `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); 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 games = await fetch_user_forsale(username);
const updated_at = new Date().toISOString(); const updated_at = new Date().toISOString();
@ -87,7 +82,6 @@ async function update_user_games(username) {
.run({ username }); .run({ username });
for (let game of games) { for (let game of games) {
console.log(game);
insert.run({ insert.run({
username, username,
updated_at, 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));
})
)
*/

7
src/lib/db.js Normal file
View File

@ -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');

View File

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