2024-12-16 22:38:47 +00:00

197 lines
6.7 KiB
JavaScript

const fs = require('fs');
const https = require('https');
const readline = require('readline');
const path = require('path');
const { execSync } = require('child_process');
// Check if Puppeteer is installed, and install it if not
try {
require.resolve('puppeteer');
} catch (e) {
console.log("Puppeteer not found. Installing...");
try {
execSync('npm install puppeteer', { stdio: 'inherit' });
console.log("Puppeteer installed successfully.");
} catch (installError) {
console.error("Failed to install Puppeteer. Ensure you have npm installed and try again.");
process.exit(1);
}
}
const puppeteer = require('puppeteer'); // Import Puppeteer after ensuring it's installed
(async () => {
// Get the game ID from the command-line arguments
const gameId = process.argv[2];
if (!gameId) {
console.error("Usage: node script.js <gameid>");
process.exit(1); // Exit if no game ID is provided
}
const csvFilePath = './helper/ArtDB.csv';
const outputDir = './icons/art/tmp';
// Ensure the output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// Function to search the CSV file for the game ID
const findUrlForGameId = async (gameId) => {
const fileStream = fs.createReadStream(csvFilePath);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
for await (const line of rl) {
const [id, urlPart] = line.split('|');
if (id === gameId) {
return `https://www.ign.com/games/${urlPart}`; // Construct the full URL
}
}
return null; // Return null if no match is found
};
const url = await findUrlForGameId(gameId);
if (!url) {
console.error(`Game ID "${gameId}" not found in ArtDB.csv`);
process.exit(1);
}
const browser = await puppeteer.launch();
const page = await browser.newPage();
try {
console.log(`Navigating to: ${url}`);
await page.goto(url, { waitUntil: 'networkidle2' });
// Find the first image with src starting with 'https://assets-prd.ignimgs.com'
let imgUrl = await page.evaluate(() => {
const img = document.querySelector('img[src^="https://assets-prd.ignimgs.com"]');
if (img) {
return img.src.split('?')[0]; // Remove query parameters
}
return null;
});
// Fallback to 'https://media.ign.com' if no image is found
if (!imgUrl) {
console.log("No image found on assets-prd.ignimgs.com. Checking media.ign.com...");
imgUrl = await page.evaluate(() => {
const img = document.querySelector('img[src^="https://media.ign.com"]');
if (img) {
return img.src;
}
return null;
});
}
// Fallback to 'https://ps2media.ign.com' if no image is found
if (!imgUrl) {
console.log("No image found on assets-prd.ignimgs.com. Checking ps2media.ign.com...");
imgUrl = await page.evaluate(() => {
const img = document.querySelector('img[src^="https://ps2media.ign.com"]');
if (img) {
return img.src;
}
return null;
});
}
// Fallback to 'https://ps3media.ign.com' if no image is found
if (!imgUrl) {
console.log("No image found on assets-prd.ignimgs.com. Checking ps3media.ign.com...");
imgUrl = await page.evaluate(() => {
const img = document.querySelector('img[src^="https://ps3media.ign.com"]');
if (img) {
return img.src;
}
return null;
});
}
// Fallback to 'https://media.gamestats.com' if no image is found
if (!imgUrl) {
console.log("No image found on assets-prd.ignimgs.com. Checking media.gamestats.com...");
imgUrl = await page.evaluate(() => {
const img = document.querySelector('img[src^="https://media.gamestats.com"]');
if (img) {
return img.src.split('?')[0]; // Remove query parameters
}
return null;
});
}
// Fallback to 'https://assets1.ignimgs.com' if no image is found
if (!imgUrl) {
console.log("No image found on assets-prd.ignimgs.com. Checking assets1.ignimgs.com...");
imgUrl = await page.evaluate(() => {
const img = document.querySelector('img[src^="https://assets1.ignimgs.com"]');
if (img) {
return img.src;
}
return null;
});
}
if (imgUrl) {
// Get the file extension from the URL
const fileExtension = path.extname(imgUrl).split('?')[0]; // Ensures query strings don't interfere
const fileName = path.join(outputDir, `${gameId}${fileExtension}`);
console.log(`Downloading image from: ${imgUrl}`);
// Use https.get for all image downloads
const downloadImage = (url, destination) => {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(destination);
const options = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
}
};
https.get(url, options, (response) => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to download image: ${response.statusCode}`));
return;
}
response.pipe(file);
file.on('finish', () => file.close(resolve));
}).on('error', (err) => {
fs.unlink(destination, () => reject(err)); // Delete incomplete file
});
});
};
try {
await downloadImage(imgUrl, fileName);
console.log(`Saved as: ${fileName}`);
} catch (downloadError) {
console.error(`Error downloading image: ${downloadError.message}`);
}
} else {
console.log("No image found on either source.");
}
} catch (error) {
console.error(`Failed to fetch the page: ${error.message}`);
}
await browser.close();
})();