Feat: Add Bset structure and Bset creation page

This commit is contained in:
globuzma 2024-11-21 17:15:45 +01:00
parent 523f4ddfe8
commit 079d2010de
16 changed files with 1459 additions and 29 deletions

View file

@ -1,8 +1,14 @@
import 'dotenv/config'
import 'https'
import fs from 'fs'
import pg from 'pg'
const { Client } = pg
const scryfallSets = await fetch('https://api.scryfall.com/sets');
console.log('Status Code:', scryfallSets.status);
const sets = await scryfallSets.json();
// Read the data from the exported fr_cards.json extracted from Scryfall Bulk Data
const fileBytes = fs.readFileSync(import.meta.dirname + '/data/fr_cards.json')
let scryfallData = JSON.parse(fileBytes)
@ -18,12 +24,26 @@ const client = new Client({
await client.connect()
try {
const setRes = await client.query('SELECT id FROM set')
const preUpdateSetRows = setRes.rows
let preUpdateSetIds = []
preUpdateSetRows.forEach(element => {
preUpdateSetIds.push(element.id)
});
for (const set of sets.data) {
if(!preUpdateSetIds.includes(set.id)){
const addingSetQuery = await client.query('INSERT INTO set(id, name_en, code, set_type, released_at, icon_svg_uri) VALUES($1, $2, $3, $4, $5, $6)', [set.id, set.name, set.code, set.set_type, set.released_at, set.icon_svg_uri])
}
}
// Select already imported cards in database
const res = await client.query('SELECT scryfall_id FROM carte')
const preUpdateRows = res.rows
let preUpdateIds = []
preUpdateRows.forEach(element => {
preUpdateIds.push(element.scryfall_id)
const cardsRes = await client.query('SELECT id FROM carte')
const preUpdateCardsRows = cardsRes.rows
let preUpdateCardsIds = []
preUpdateCardsRows.forEach(element => {
preUpdateCardsIds.push(element.id)
});
// Define counter for logging
@ -33,7 +53,7 @@ try {
// For each card check if we need to upload it to the database
for (const carte of scryfallData) {
if(!preUpdateIds.includes(carte.id)){
if(!preUpdateCardsIds.includes(carte.id)){
if(carte.printed_name == undefined) {
// If the card doesn't have a french name, print it to the console and skip
@ -45,7 +65,7 @@ try {
}
// Add the card to the database
const addingQuery = await client.query('INSERT INTO carte(id, scryfall_id, name_en, name_fr, released_at, small_image, normal_image, mana_cost, cmc, type_line_en, type_line_fr, oracle_text_en, oracle_text_fr, power, toughness, colors, keywords, set, set_name_en, set_type, rarity, cardmarket_uri) VALUES(gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)', [carte.id, carte.name, carte.printed_name, carte.released_at, carte.image_uris.small, carte.image_uris.normal, carte.mana_cost, carte.cmc, carte.type_line, carte.printed_type_line, carte.oracle_text, carte.printed_text, carte.power, carte.toughness, carte.colors, carte.keywords, carte.set, carte.set_name, carte.set_type, carte.rarity, carte.purchase_uris?.cardmarket])
const addingCardsQuery = await client.query('INSERT INTO carte(id, name_en, name_fr, released_at, small_image, normal_image, mana_cost, cmc, type_line_en, type_line_fr, oracle_text_en, oracle_text_fr, power, toughness, colors, keywords, set_id, rarity, cardmarket_uri) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)', [carte.id, carte.name, carte.printed_name, carte.released_at, carte.image_uris.small, carte.image_uris.normal, carte.mana_cost, carte.cmc, carte.type_line, carte.printed_type_line, carte.oracle_text, carte.printed_text, carte.power, carte.toughness, carte.colors, carte.keywords, carte.set_id, carte.rarity, carte.purchase_uris?.cardmarket])
total_inserted = total_inserted + 1
} else {
total_skipped = total_skipped + 1