2024-11-19 15:04:41 +00:00
|
|
|
import 'dotenv/config'
|
2024-11-21 16:15:45 +00:00
|
|
|
import 'https'
|
2024-11-19 15:04:41 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import pg from 'pg'
|
|
|
|
const { Client } = pg
|
|
|
|
|
2024-11-21 16:15:45 +00:00
|
|
|
const scryfallSets = await fetch('https://api.scryfall.com/sets');
|
|
|
|
console.log('Status Code:', scryfallSets.status);
|
|
|
|
|
|
|
|
const sets = await scryfallSets.json();
|
|
|
|
|
2024-11-19 15:04:41 +00:00
|
|
|
// 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)
|
|
|
|
|
|
|
|
// Connect to postgres database
|
|
|
|
const client = new Client({
|
|
|
|
user: process.env.DATABASE_USER,
|
|
|
|
password: process.env.DATABASE_PASSWORD,
|
|
|
|
host: process.env.DATABASE_HOST,
|
|
|
|
port: process.env.DATABASE_PORT,
|
|
|
|
database: process.env.DATABASE_DB
|
|
|
|
})
|
|
|
|
await client.connect()
|
|
|
|
|
|
|
|
try {
|
2024-11-21 16:15:45 +00:00
|
|
|
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)){
|
2024-12-05 16:08:50 +00:00
|
|
|
const addingSetQuery = await client.query('INSERT INTO set(id, name_en, sanitized_name, code, set_type, released_at, icon_svg_uri) VALUES($1, $2, $3, $4, $5, $6, $7)', [set.id, set.name, set.name.replace(/[^a-zA-Z0-9]/gim,"-").toLowerCase(), set.code, set.set_type, set.released_at, set.icon_svg_uri])
|
2024-11-21 16:15:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-19 15:04:41 +00:00
|
|
|
// Select already imported cards in database
|
2024-11-21 16:15:45 +00:00
|
|
|
const cardsRes = await client.query('SELECT id FROM carte')
|
|
|
|
const preUpdateCardsRows = cardsRes.rows
|
|
|
|
let preUpdateCardsIds = []
|
|
|
|
preUpdateCardsRows.forEach(element => {
|
|
|
|
preUpdateCardsIds.push(element.id)
|
2024-11-19 15:04:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Define counter for logging
|
|
|
|
let total_no_fr_name = 0
|
|
|
|
let total_inserted = 0
|
|
|
|
let total_skipped = 0
|
|
|
|
|
|
|
|
// For each card check if we need to upload it to the database
|
|
|
|
for (const carte of scryfallData) {
|
2024-11-21 16:15:45 +00:00
|
|
|
if(!preUpdateCardsIds.includes(carte.id)){
|
2024-12-05 16:08:50 +00:00
|
|
|
let type = null
|
|
|
|
const card_type = carte.type_line.toLowerCase()
|
|
|
|
|
|
|
|
if(card_type.includes("creature")){
|
|
|
|
type = "creature"
|
|
|
|
} else if (card_type.includes("planeswalker")) {
|
|
|
|
type = "planeswalker"
|
|
|
|
} else if (card_type.includes("artifact")) {
|
|
|
|
type = "artifact"
|
|
|
|
} else if (card_type.includes("instant")) {
|
|
|
|
type = "instant"
|
|
|
|
} else if (card_type.includes("enchantment")) {
|
|
|
|
type = "enchantment"
|
|
|
|
} else if (card_type.includes("sorcery")) {
|
|
|
|
type = "sorcery"
|
|
|
|
} else if (card_type.includes("land")) {
|
|
|
|
type = "land"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-19 15:04:41 +00:00
|
|
|
if(carte.printed_name == undefined) {
|
|
|
|
// If the card doesn't have a french name, print it to the console and skip
|
|
|
|
|
|
|
|
//console.log("Erreur sur la carte : " + carte.name)
|
|
|
|
//console.log("Scryfall URI : " + carte.scryfall_uri)
|
|
|
|
//console.log("API URI : " + carte.uri)
|
|
|
|
total_no_fr_name = total_no_fr_name + 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the card to the database
|
2024-12-05 16:08:50 +00:00
|
|
|
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, type, sanitized_name) VALUES($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.color_identity, carte.keywords, carte.set_id, carte.rarity, carte.purchase_uris?.cardmarket, type, carte.name.replace(/[^a-zA-Z0-9]/gim,"-").toLowerCase()])
|
2024-11-19 15:04:41 +00:00
|
|
|
total_inserted = total_inserted + 1
|
|
|
|
} else {
|
|
|
|
total_skipped = total_skipped + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
console.log("Un total de " + total_no_fr_name + " cartes n'ont pas de nom français.")
|
|
|
|
console.log("Un total de " + total_inserted + " cartes ont été insérées.")
|
|
|
|
console.log("Un total de " + total_skipped + " cartes ont été ignorées.")
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
} finally {
|
|
|
|
await client.end()
|
|
|
|
}
|