Spaces:
Paused
Paused
File size: 5,263 Bytes
9913f6e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | // Automated draft transfer system - watches for new scraped JSON files
// and generates browser transfer code automatically
const fs = require('fs');
const path = require('path');
class AutoDraftTransfer {
constructor() {
this.scriptsDir = __dirname;
this.lastProcessed = null;
}
// Find the latest scraped JSON file
findLatestScrapedFile() {
const files = fs.readdirSync(this.scriptsDir)
.filter(file => file.startsWith('1688-') && file.endsWith('.json'))
.map(file => ({
name: file,
path: path.join(this.scriptsDir, file),
time: fs.statSync(path.join(this.scriptsDir, file)).mtime.getTime()
}))
.sort((a, b) => b.time - a.time);
return files.length > 0 ? files[0] : null;
}
// Convert scraped product to admin draft format
convertToAdminDraft(scrapedProduct) {
return {
name: scrapedProduct.title_en || scrapedProduct.product_name || 'Untitled Product',
chineseName: scrapedProduct.title_cn || '',
description: scrapedProduct.description_ar || scrapedProduct.description || '',
price: scrapedProduct.converted_price_iqd || 0,
basePriceRMB: scrapedProduct.price_rmb || 0,
image: scrapedProduct.main_image || '',
images: scrapedProduct.images || [],
status: 'DRAFT',
isActive: false,
isFeatured: false,
purchaseUrl: scrapedProduct.product_url || '',
specs: {
moq: scrapedProduct.moq || 1,
company: scrapedProduct.company_name || '',
location: scrapedProduct.company_location || '',
weight_kg: scrapedProduct.weight_kg || 0.5,
original_price_rmb: scrapedProduct.price_rmb || 0,
converted_price_iqd: scrapedProduct.converted_price_iqd || 0
},
storeEvaluation: {
responseRate: scrapedProduct.response_rate || '',
transactionRate: scrapedProduct.transaction_rate || ''
},
weight: scrapedProduct.weight_kg || 0.5,
length: 20,
width: 15,
height: 10,
domesticShippingFee: 0,
id: `local-${Date.now()}`,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
isLocal: true
};
}
// Generate browser transfer code
generateBrowserTransferCode(scrapedData) {
const drafts = Array.isArray(scrapedData)
? scrapedData.map(product => this.convertToAdminDraft(product))
: [this.convertToAdminDraft(scrapedData)];
const jsonString = JSON.stringify(drafts)
.replace(/'/g, "\\'")
.replace(/\n/g, ' ');
return `
// AUTO-GENERATED DRAFT TRANSFER CODE
// Paste this in browser console at http://localhost:5173/admin/products
localStorage.setItem('admin_local_drafts', '${jsonString}');
console.log('β
${drafts.length} draft product(s) loaded into localStorage');
console.log('π Refresh the admin products page to see your drafts');
// Products transferred:
${drafts.map((draft, index) => `// ${index + 1}. ${draft.name}`).join('\n')}
`;
}
// Process the latest file
processLatestFile() {
const latestFile = this.findLatestScrapedFile();
if (!latestFile) {
console.log('β No scraped JSON files found in:', this.scriptsDir);
console.log('π‘ Run the scraper first to generate product data');
return;
}
if (this.lastProcessed === latestFile.name) {
console.log('π Latest file already processed:', latestFile.name);
console.log('π‘ Run the scraper again to get new products');
return;
}
try {
const scrapedData = JSON.parse(fs.readFileSync(latestFile.path, 'utf8'));
console.log('π― Found scraped file:', latestFile.name);
console.log('π Products found:', Array.isArray(scrapedData) ? scrapedData.length : 1);
console.log('\n' + '='.repeat(60));
const transferCode = this.generateBrowserTransferCode(scrapedData);
console.log(transferCode);
console.log('='.repeat(60));
console.log('π INSTRUCTIONS:');
console.log('1. Open http://localhost:5173/admin/products in your browser');
console.log('2. Press F12 β Console tab');
console.log('3. Paste the code above and press Enter');
console.log('4. Refresh the page to see drafts');
this.lastProcessed = latestFile.name;
// Also save to file for easy access
const transferFile = path.join(this.scriptsDir, 'browser-transfer-code.js');
fs.writeFileSync(transferFile, transferCode);
console.log('\nπΎ Transfer code also saved to: browser-transfer-code.js');
} catch (error) {
console.error('β Error processing file:', error.message);
}
}
// Watch for new files (optional)
startWatching() {
console.log('π Watching for new scraped files... (Ctrl+C to stop)');
fs.watch(this.scriptsDir, (eventType, filename) => {
if (filename && filename.startsWith('1688-') && filename.endsWith('.json')) {
console.log('\nπ New scraped file detected:', filename);
this.processLatestFile();
}
});
}
}
// Run the processor
const processor = new AutoDraftTransfer();
processor.processLatestFile();
// Uncomment next line to enable automatic watching:
// processor.startWatching(); |