Update low-resolution image update script to use new database and fetch Pokémon IDs

This commit is contained in:
vista-man
2025-03-25 17:22:22 +01:00
parent 3060fb2894
commit 81fac43f76

View File

@@ -1,12 +1,10 @@
const mysql = require('mysql'); const mysql = require('mysql');
const fs = require('fs');
const path = require('path');
const connection = mysql.createConnection({ const connection = mysql.createConnection({
host: 'localhost', host: 'localhost',
user: 'root', user: 'root',
password: '', password: '',
database: 'pokedex' database: 'pokedex1'
}); });
connection.connect((err) => { connection.connect((err) => {
@@ -19,19 +17,19 @@ connection.connect((err) => {
}); });
function updateLowResImages() { function updateLowResImages() {
const imagesDir = path.join(__dirname, 'small-images'); const query = 'SELECT id FROM pokemon';
fs.readdir(imagesDir, (err, files) => { connection.query(query, (err, results) => {
if (err) { if (err) {
console.error('Error reading the images directory:', err); console.error('Error fetching Pokémon IDs:', err);
return; return;
} }
files.forEach((file) => { results.forEach((row) => {
const id = path.parse(file).name; const id = row.id;
const imageUrlLow = `/small-images/${file}`; const imageUrlLow = `./small-images/${id}.png`;
const query = 'UPDATE pokemon SET image_url_low = ? WHERE id = ?'; const updateQuery = 'UPDATE pokemon SET image_url_low = ? WHERE id = ?';
connection.query(query, [imageUrlLow, id], (err, result) => { connection.query(updateQuery, [imageUrlLow, id], (err, result) => {
if (err) { if (err) {
console.error(`Error updating image_url_low for Pokémon ID ${id}:`, err); console.error(`Error updating image_url_low for Pokémon ID ${id}:`, err);
return; return;