Merge branch 'last-updated'
This commit is contained in:
commit
1a476ddc1d
8
db/migrations/20240201201248_last-updated.sql
Normal file
8
db/migrations/20240201201248_last-updated.sql
Normal file
@ -0,0 +1,8 @@
|
||||
-- migrate:up
|
||||
|
||||
alter table game
|
||||
add column updated_at text;
|
||||
|
||||
-- migrate:down
|
||||
|
||||
alter table game drop column updated_at;
|
@ -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');
|
||||
|
@ -7,7 +7,9 @@ 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());
|
||||
@ -16,7 +18,9 @@ export async function fetch_guild_users(guild_id) {
|
||||
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 }));
|
||||
|
||||
@ -51,7 +55,7 @@ async function fetch_user_forsale(username, n = 1) {
|
||||
if (n > 5) throw new Error("couldn't get collection");
|
||||
|
||||
const res = await fetch(
|
||||
`https://boardgamegeek.com/xmlapi2/collection?trade=1&username=${username}`
|
||||
`https://boardgamegeek.com/xmlapi2/collection?trade=1&username=${username}`,
|
||||
);
|
||||
|
||||
if (res.status === 202) {
|
||||
@ -70,19 +74,24 @@ async function fetch_user_forsale(username, n = 1) {
|
||||
async function update_user_games(username) {
|
||||
const games = await fetch_user_forsale(username);
|
||||
|
||||
const updated_at = new Date().toISOString();
|
||||
|
||||
// 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)'
|
||||
'INSERT into game (username, bgg_id, name, thumbnail, notes,price,updated_at) VALUES(@username,@bgg_id,@name,@thumbnail,@notes,@price,@updated_at)',
|
||||
);
|
||||
|
||||
await db.prepare('DELETE FROM game where username = @username').run({ username });
|
||||
await db
|
||||
.prepare('DELETE FROM game where username = @username')
|
||||
.run({ username });
|
||||
|
||||
for (let game of games) {
|
||||
console.log(game);
|
||||
insert.run({
|
||||
username,
|
||||
...game
|
||||
updated_at,
|
||||
...game,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -39,6 +39,7 @@
|
||||
<Th {handler} orderBy="name">game</Th>
|
||||
<th>notes</th>
|
||||
<Th {handler} orderBy="price">price</Th>
|
||||
<th>last updated</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -69,6 +70,7 @@
|
||||
</td>
|
||||
<td>{row.notes}</td>
|
||||
<td>{row.price ?? ''}</td>
|
||||
<td><small>{pretty_date(row.updated_at)}</small></td>
|
||||
</tr>
|
||||
{/each}
|
||||
</tbody>
|
||||
@ -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();
|
||||
</script>
|
||||
|
Loading…
Reference in New Issue
Block a user