From dc36380bdbf4f8c56095f803b5c0c4ed9f14ad83 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 1 Feb 2024 15:27:38 -0500 Subject: [PATCH] add updated_at --- db/migrations/20240201201248_last-updated.sql | 8 ++ db/schema.sql | 5 +- src/fetchCollection.js | 119 ++++++++++-------- src/routes/+page.svelte | 7 ++ 4 files changed, 82 insertions(+), 57 deletions(-) create mode 100644 db/migrations/20240201201248_last-updated.sql diff --git a/db/migrations/20240201201248_last-updated.sql b/db/migrations/20240201201248_last-updated.sql new file mode 100644 index 0000000..e844c8a --- /dev/null +++ b/db/migrations/20240201201248_last-updated.sql @@ -0,0 +1,8 @@ +-- migrate:up + +alter table game + add column updated_at text; + +-- migrate:down + +alter table game drop column updated_at; diff --git a/db/schema.sql b/db/schema.sql index 838036f..1368c56 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -8,11 +8,12 @@ CREATE TABLE game ( thumbnail text, bgg_id text, name text, - notes text, price integer, + notes text, price integer, updated_at text, FOREIGN KEY(username) REFERENCES bgg_user(username) ); -- Dbmate schema migrations INSERT INTO "schema_migrations" (version) VALUES ('20240130203355'), ('20240130203603'), - ('20240130205124'); + ('20240130205124'), + ('20240201201248'); diff --git a/src/fetchCollection.js b/src/fetchCollection.js index 60d6829..c05c1bc 100644 --- a/src/fetchCollection.js +++ b/src/fetchCollection.js @@ -7,88 +7,97 @@ const db = DB(db_file); db.pragma('journal_mode = WAL'); export async function fetch_guild_users(guild_id) { - const res = await fetch(`https://boardgamegeek.com/xmlapi2/guild?members=1&id=` + guild_id); + const res = await fetch( + `https://boardgamegeek.com/xmlapi2/guild?members=1&id=` + guild_id, + ); - if (res.status === 200) { - const $page = load(await res.text()); - const users = []; - $page('member').each(function (i, member) { - users.push($page(this).attr('name')); - }); + if (res.status === 200) { + const $page = load(await res.text()); + const users = []; + $page('member').each(function (i, member) { + users.push($page(this).attr('name')); + }); - const insert = db.prepare('INSERT OR IGNORE INTO bgg_user (username) VALUES(@username)'); + const insert = db.prepare( + 'INSERT OR IGNORE INTO bgg_user (username) VALUES(@username)', + ); - users.forEach((username) => insert.run({ username })); + users.forEach((username) => insert.run({ username })); - return users; - } + return users; + } - throw new Error('sad'); + throw new Error('sad'); } export function extract_user_forsale(page) { - const $ = load(page); + const $ = load(page); - const games = []; + const games = []; - const data = $('item').map(function (i, item) { - const data = {}; - data.bgg_id = $(this).attr('objectid'); - data.name = $(this).find('name').text(); - data.thumbnail = $(this).find('thumbnail').text(); - data.notes = $(this).find('conditiontext').text(); - const find_price = data.notes.match(/\$(\d+)/); - data.price = undefined; - if (find_price) data.price = parseInt(find_price[1]); + const data = $('item').map(function (i, item) { + const data = {}; + data.bgg_id = $(this).attr('objectid'); + data.name = $(this).find('name').text(); + data.thumbnail = $(this).find('thumbnail').text(); + data.notes = $(this).find('conditiontext').text(); + const find_price = data.notes.match(/\$(\d+)/); + data.price = undefined; + if (find_price) data.price = parseInt(find_price[1]); - games.push(data); - }); + games.push(data); + }); - return games; + return games; } async function fetch_user_forsale(username, n = 1) { - if (n > 5) throw new Error("couldn't get collection"); + if (n > 5) throw new Error("couldn't get collection"); - const res = await fetch( - `https://boardgamegeek.com/xmlapi2/collection?trade=1&username=${username}` - ); + const res = await fetch( + `https://boardgamegeek.com/xmlapi2/collection?trade=1&username=${username}`, + ); - if (res.status === 202) { - return new Promise((accept, reject) => { - setTimeout(() => { - fetch_user_forsale(username, n + 1).then(accept); - }, 2000); - }); - } + if (res.status === 202) { + return new Promise((accept, reject) => { + setTimeout(() => { + fetch_user_forsale(username, n + 1).then(accept); + }, 2000); + }); + } - if (res.status === 200) return extract_user_forsale(await res.text()); + if (res.status === 200) return extract_user_forsale(await res.text()); - 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) { - const games = await fetch_user_forsale(username); + const games = await fetch_user_forsale(username); - // TODO only change the games that changed - // TODO only change users that got added or removed - const insert = db.prepare( - 'INSERT into game (username, bgg_id, name, thumbnail, notes,price) VALUES(@username,@bgg_id,@name,@thumbnail,@notes,@price)' - ); + const updated_at = new Date().toISOString(); - await db.prepare('DELETE FROM game where username = @username').run({ username }); + // TODO only change the games that changed + // TODO only change users that got added or removed + const insert = db.prepare( + 'INSERT into game (username, bgg_id, name, thumbnail, notes,price,updated_at) VALUES(@username,@bgg_id,@name,@thumbnail,@notes,@price,@updated_at)', + ); - for (let game of games) { - console.log(game); - insert.run({ - username, - ...game - }); - } + await db + .prepare('DELETE FROM game where username = @username') + .run({ username }); + + for (let game of games) { + console.log(game); + insert.run({ + username, + updated_at, + ...game, + }); + } } await update_user_games('yenzie') - .then(() => console.log('is done')) - .catch((e) => console.error(e)); + .then(() => console.log('is done')) + .catch((e) => console.error(e)); /* const usernames = await fetch_guild_users('1610'); diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index a4bf553..7f5cc55 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -39,6 +39,7 @@ game notes price + last updated @@ -69,6 +70,7 @@ {row.notes} {row.price ?? ''} + {pretty_date(row.updated_at)} {/each} @@ -85,6 +87,11 @@ export let data; + function pretty_date(date) { + if (!date) return ''; + + return date.replace(/T.*/, ''); + } const handler = new DataHandler(data.games, { rowsPerPage: 500 }); const rows = handler.getRows();