From 44c3f028bdbdb84107fa9b5b96f79d8010d2f7d6 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 1 Feb 2024 13:49:55 -0500 Subject: [PATCH] add prettier to project --- .prettierrc | 8 --- .prettierrc.cjs | 12 +++++ src/fetchCollection.js | 116 +++++++++++++++++++++-------------------- 3 files changed, 71 insertions(+), 65 deletions(-) delete mode 100644 .prettierrc create mode 100644 .prettierrc.cjs diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index 9573023..0000000 --- a/.prettierrc +++ /dev/null @@ -1,8 +0,0 @@ -{ - "useTabs": true, - "singleQuote": true, - "trailingComma": "none", - "printWidth": 100, - "plugins": ["prettier-plugin-svelte"], - "overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }] -} diff --git a/.prettierrc.cjs b/.prettierrc.cjs new file mode 100644 index 0000000..688495e --- /dev/null +++ b/.prettierrc.cjs @@ -0,0 +1,12 @@ +module.exports = { + semi: true, + trailingComma: 'all', + singleQuote: true, + printWidth: 80, + tabWidth: 4, + useTabs: false, + plugins: ['prettier-plugin-svelte'], + svelteSortOrder: 'options-markup-scripts-styles', + svelteStrictMode: false, + svelteAllowShorthand: true, +}; diff --git a/src/fetchCollection.js b/src/fetchCollection.js index 10558f0..60d6829 100644 --- a/src/fetchCollection.js +++ b/src/fetchCollection.js @@ -2,91 +2,93 @@ 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_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) { - const res = await fetch(`https://boardgamegeek.com/xmlapi2/guild?members=1&id=`+guild_id); +export async function fetch_guild_users(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"); +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}`); + 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)' ); - - await db.prepare('DELETE FROM game where username = @username').run({username}); - - for ( let game of games ) { - console.log(game); - insert.run({ - username, - ...game - }); - } + // 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)' + ); + await db.prepare('DELETE FROM game where username = @username').run({ username }); + for (let game of games) { + console.log(game); + insert.run({ + username, + ...game + }); + } } -await update_user_games('yenzie').then( () => console.log('is done')).catch( e => console.error(e)); +await update_user_games('yenzie') + .then(() => console.log('is done')) + .catch((e) => console.error(e)); /* const usernames = await fetch_guild_users('1610');