Add real Lorcana import using Lorcast API
- Replaced mock Lorcana import with real API integration - Uses Lorcast API (https://api.lorcast.com/v0/cards/search) for comprehensive card data - Added proper set code mapping (tfc->1, rotf->2, ink->3) - Includes card images, prices, stats, and detailed metadata - Created dedicated Lorcana import scripts for standalone use - Maintains duplicate checking and proper error handling - Supports all 3 Lorcana sets: The First Chapter, Rise of the Floodborn, Into the Inklands
This commit is contained in:
parent
b6f76297bc
commit
e046515a38
4 changed files with 1641 additions and 30 deletions
1301
bulk-import-results-2025-07-24T17-20-21-039Z.json
Normal file
1301
bulk-import-results-2025-07-24T17-20-21-039Z.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -56,28 +56,40 @@ export default async function handler(req, res) {
|
|||
|
||||
console.log(`Starting import for Lorcana set: ${setCode}`);
|
||||
|
||||
// For now, we'll use a placeholder approach since Lorcana doesn't have a public API
|
||||
// You can replace this with actual Lorcana API calls when available
|
||||
const mockCards = [
|
||||
{
|
||||
id: `lorcana-${setCode}-001`,
|
||||
name: 'Sample Lorcana Card',
|
||||
set: { name: setCode, id: setCode },
|
||||
number: '001',
|
||||
rarity: 'Common',
|
||||
supertype: 'Character',
|
||||
types: ['Character'],
|
||||
flavorText: 'A sample Lorcana card',
|
||||
images: { small: null, large: null }
|
||||
}
|
||||
];
|
||||
// Map set codes to Lorcast set codes
|
||||
const setCodeMap = {
|
||||
'tfc': '1', // The First Chapter
|
||||
'rotf': '2', // Rise of the Floodborn
|
||||
'ink': '3' // Into the Inklands
|
||||
};
|
||||
|
||||
console.log(`Using mock data for set ${setCode} (${mockCards.length} cards)`);
|
||||
const lorcastSetCode = setCodeMap[setCode];
|
||||
if (!lorcastSetCode) {
|
||||
return res.status(400).json({ error: `Unknown set code: ${setCode}` });
|
||||
}
|
||||
|
||||
// Fetch cards from Lorcast API
|
||||
const response = await fetchWithRetry(`https://api.lorcast.com/v0/cards/search?q=set:${lorcastSetCode}&unique=prints`);
|
||||
|
||||
const data = await response.json();
|
||||
const cards = data.results || [];
|
||||
|
||||
if (cards.length === 0) {
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: `No cards found for set ${setCode}`,
|
||||
imported: 0,
|
||||
skipped: 0,
|
||||
total: 0
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`Found ${cards.length} cards for set ${setCode}`);
|
||||
|
||||
let importedCount = 0;
|
||||
let skippedCount = 0;
|
||||
|
||||
for (const card of mockCards) {
|
||||
for (const card of cards) {
|
||||
try {
|
||||
// Check if card already exists
|
||||
const existingCard = await sql`
|
||||
|
|
@ -89,16 +101,30 @@ export default async function handler(req, res) {
|
|||
continue;
|
||||
}
|
||||
|
||||
// Extract price data
|
||||
let currentPrice = null;
|
||||
if (card.prices?.usd) {
|
||||
currentPrice = parseFloat(card.prices.usd);
|
||||
} else if (card.prices?.usd_foil) {
|
||||
currentPrice = parseFloat(card.prices.usd_foil);
|
||||
}
|
||||
|
||||
// Determine rarity
|
||||
let rarity = card.rarity || 'Common';
|
||||
if (rarity.includes('Enchanted')) {
|
||||
rarity = 'Enchanted';
|
||||
} else if (rarity.includes('Legendary')) {
|
||||
rarity = 'Legendary';
|
||||
} else if (rarity.includes('Rare')) {
|
||||
rarity = 'Rare';
|
||||
if (rarity === 'Super_rare') {
|
||||
rarity = 'Super Rare';
|
||||
}
|
||||
|
||||
// Extract card type and text
|
||||
const cardType = card.type?.[0] || 'Character';
|
||||
const cardText = card.text || null;
|
||||
const flavorText = card.flavor_text || null;
|
||||
|
||||
// Extract stats
|
||||
const strength = card.strength || null;
|
||||
const willpower = card.willpower || null;
|
||||
const lore = card.lore || null;
|
||||
|
||||
// Insert card into database
|
||||
await sql`
|
||||
INSERT INTO cards (
|
||||
|
|
@ -107,12 +133,12 @@ export default async function handler(req, res) {
|
|||
power, toughness, image_url, stock_image_url,
|
||||
current_price, market_price, scryfall_id, verified
|
||||
) VALUES (
|
||||
${card.name}, ${card.set.name}, ${card.set.id}, ${card.number},
|
||||
${rarity}, 'Lorcana', null, null, ${card.supertype || 'Character'},
|
||||
${JSON.stringify(card.types || [])}, ${card.flavorText || null},
|
||||
null, null,
|
||||
${card.images?.small || null}, ${card.images?.large || null},
|
||||
null, null, ${card.id}, true
|
||||
${card.name}, ${card.set.name}, ${card.set.code}, ${card.collector_number},
|
||||
${rarity}, 'Lorcana', ${card.cost}, ${card.cost}, ${cardType},
|
||||
${JSON.stringify(card.ink ? [card.ink] : [])}, ${cardText || flavorText},
|
||||
${strength}, ${willpower || lore},
|
||||
${card.image_uris?.digital?.small || null}, ${card.image_uris?.digital?.large || null},
|
||||
${currentPrice}, null, ${card.id}, true
|
||||
)
|
||||
`;
|
||||
|
||||
|
|
@ -130,7 +156,7 @@ export default async function handler(req, res) {
|
|||
message: `Import completed for set ${setCode}`,
|
||||
imported: importedCount,
|
||||
skipped: skippedCount,
|
||||
total: mockCards.length
|
||||
total: cards.length
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
|
|
|
|||
98
scripts/import-lorcana-simple.js
Normal file
98
scripts/import-lorcana-simple.js
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import fetch from 'node-fetch';
|
||||
import fs from 'fs';
|
||||
|
||||
// Lorcana sets to import
|
||||
const LORCANA_SETS = [
|
||||
{ code: 'tfc', name: 'The First Chapter' },
|
||||
{ code: 'rotf', name: 'Rise of the Floodborn' },
|
||||
{ code: 'ink', name: 'Into the Inklands' }
|
||||
];
|
||||
|
||||
async function importLorcanaSet(setCode, setName) {
|
||||
try {
|
||||
console.log(`Importing Lorcana set: ${setName} (${setCode})`);
|
||||
|
||||
const response = await fetch(`http://localhost:3000/api/cards/import-lorcana`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
setCode: setCode,
|
||||
setName: setName
|
||||
})
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
console.log(`✅ Successfully imported ${result.imported || 0} cards from ${setName}`);
|
||||
return { success: true, importedCount: result.imported || 0 };
|
||||
} else {
|
||||
console.log(`❌ Failed to import ${setName}: ${result.error}`);
|
||||
return { success: false, error: result.error };
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`❌ Error importing ${setName}: ${error.message}`);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
async function importAllLorcana() {
|
||||
console.log('🎨 Starting Lorcana card import...\n');
|
||||
|
||||
const results = {
|
||||
total: 0,
|
||||
successful: 0,
|
||||
failed: 0,
|
||||
sets: []
|
||||
};
|
||||
|
||||
for (const set of LORCANA_SETS) {
|
||||
const result = await importLorcanaSet(set.code, set.name);
|
||||
results.sets.push({ ...set, ...result });
|
||||
|
||||
if (result.success) {
|
||||
results.successful++;
|
||||
results.total += result.importedCount || 0;
|
||||
} else {
|
||||
results.failed++;
|
||||
}
|
||||
|
||||
// Add a delay between sets
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
}
|
||||
|
||||
// Print summary
|
||||
console.log('\n📊 Lorcana Import Summary:');
|
||||
console.log('========================');
|
||||
console.log(`Total sets: ${LORCANA_SETS.length}`);
|
||||
console.log(`Successful: ${results.successful}`);
|
||||
console.log(`Failed: ${results.failed}`);
|
||||
console.log(`Total cards imported: ${results.total}`);
|
||||
console.log(`Success rate: ${((results.successful / LORCANA_SETS.length) * 100).toFixed(1)}%`);
|
||||
|
||||
// Save detailed results
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
const filename = `lorcana-import-results-${timestamp}.json`;
|
||||
|
||||
fs.writeFileSync(filename, JSON.stringify(results, null, 2));
|
||||
console.log(`\n📄 Detailed results saved to: ${filename}`);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// Run the import if this script is executed directly
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
importAllLorcana()
|
||||
.then(() => {
|
||||
console.log('\n🎉 Lorcana import completed!');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('❌ Import failed:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
export { importAllLorcana, importLorcanaSet };
|
||||
186
scripts/import-lorcana.js
Normal file
186
scripts/import-lorcana.js
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
import fetch from 'node-fetch';
|
||||
import fs from 'fs';
|
||||
|
||||
// Lorcana sets to import
|
||||
const LORCANA_SETS = [
|
||||
{ code: 'tfc', name: 'The First Chapter', lorcastCode: '1' },
|
||||
{ code: 'rotf', name: 'Rise of the Floodborn', lorcastCode: '2' },
|
||||
{ code: 'ink', name: 'Into the Inklands', lorcastCode: '3' }
|
||||
];
|
||||
|
||||
// Helper function to delay execution
|
||||
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
// Helper function to fetch with retry logic
|
||||
async function fetchWithRetry(url, maxRetries = 3, delayMs = 2000) {
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'User-Agent': 'TCG-Vault/1.0',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
timeout: 30000 // 30 second timeout
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
return response;
|
||||
}
|
||||
|
||||
// If it's a 404, don't retry
|
||||
if (response.status === 404) {
|
||||
throw new Error(`Set not found: ${response.status}`);
|
||||
}
|
||||
|
||||
// If it's a 504 or 503, wait longer before retry
|
||||
if (response.status === 504 || response.status === 503) {
|
||||
console.log(`Attempt ${attempt}: Got ${response.status}, waiting ${delayMs * attempt}ms before retry...`);
|
||||
await delay(delayMs * attempt);
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new Error(`Lorcana API error: ${response.status}`);
|
||||
} catch (error) {
|
||||
if (attempt === maxRetries) {
|
||||
throw error;
|
||||
}
|
||||
console.log(`Attempt ${attempt} failed:`, error.message);
|
||||
await delay(delayMs * attempt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function importLorcanaSet(setCode, setName, lorcastCode) {
|
||||
try {
|
||||
console.log(`\n🎨 Importing Lorcana set: ${setName} (${setCode})`);
|
||||
|
||||
// Fetch cards directly from Lorcast API
|
||||
const response = await fetchWithRetry(`https://api.lorcast.com/v0/cards/search?q=set:${lorcastCode}&unique=prints`);
|
||||
const data = await response.json();
|
||||
const cards = data.results || [];
|
||||
|
||||
if (cards.length === 0) {
|
||||
console.log(`❌ No cards found for set ${setName}`);
|
||||
return { success: false, error: 'No cards found', importedCount: 0 };
|
||||
}
|
||||
|
||||
console.log(`📊 Found ${cards.length} cards for set ${setName}`);
|
||||
|
||||
let importedCount = 0;
|
||||
let skippedCount = 0;
|
||||
|
||||
for (const card of cards) {
|
||||
try {
|
||||
// Check if card already exists in database
|
||||
const checkResponse = await fetch('http://localhost:3000/api/cards/search', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
query: card.name,
|
||||
game: 'Lorcana',
|
||||
set: card.set.code
|
||||
})
|
||||
});
|
||||
|
||||
const checkData = await checkResponse.json();
|
||||
const existingCard = checkData.cards?.find(c => c.scryfall_id === card.id);
|
||||
|
||||
if (existingCard) {
|
||||
skippedCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Import card via our API
|
||||
const importResponse = await fetch('http://localhost:3000/api/cards/import-lorcana', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
setCode: setCode,
|
||||
cardData: card
|
||||
})
|
||||
});
|
||||
|
||||
const importResult = await importResponse.json();
|
||||
|
||||
if (importResult.success) {
|
||||
importedCount++;
|
||||
} else {
|
||||
skippedCount++;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error processing card ${card.name}:`, error.message);
|
||||
skippedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ Import completed for ${setName}: ${importedCount} imported, ${skippedCount} skipped`);
|
||||
return { success: true, importedCount, skippedCount, total: cards.length };
|
||||
|
||||
} catch (error) {
|
||||
console.log(`❌ Error importing ${setName}: ${error.message}`);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
async function importAllLorcana() {
|
||||
console.log('🎨 Starting Lorcana card import...\n');
|
||||
|
||||
const results = {
|
||||
total: 0,
|
||||
successful: 0,
|
||||
failed: 0,
|
||||
sets: []
|
||||
};
|
||||
|
||||
for (const set of LORCANA_SETS) {
|
||||
const result = await importLorcanaSet(set.code, set.name, set.lorcastCode);
|
||||
results.sets.push({ ...set, ...result });
|
||||
|
||||
if (result.success) {
|
||||
results.successful++;
|
||||
results.total += result.importedCount || 0;
|
||||
} else {
|
||||
results.failed++;
|
||||
}
|
||||
|
||||
// Add a delay between sets
|
||||
await delay(2000);
|
||||
}
|
||||
|
||||
// Print summary
|
||||
console.log('\n📊 Lorcana Import Summary:');
|
||||
console.log('========================');
|
||||
console.log(`Total sets: ${LORCANA_SETS.length}`);
|
||||
console.log(`Successful: ${results.successful}`);
|
||||
console.log(`Failed: ${results.failed}`);
|
||||
console.log(`Total cards imported: ${results.total}`);
|
||||
console.log(`Success rate: ${((results.successful / LORCANA_SETS.length) * 100).toFixed(1)}%`);
|
||||
|
||||
// Save detailed results
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||
const filename = `lorcana-import-results-${timestamp}.json`;
|
||||
|
||||
fs.writeFileSync(filename, JSON.stringify(results, null, 2));
|
||||
console.log(`\n📄 Detailed results saved to: ${filename}`);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// Run the import if this script is executed directly
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
importAllLorcana()
|
||||
.then(() => {
|
||||
console.log('\n🎉 Lorcana import completed!');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('❌ Import failed:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
export { importAllLorcana, importLorcanaSet };
|
||||
Loading…
Reference in a new issue