diff --git a/download-images.js b/download-images.js new file mode 100644 index 0000000..ab787d4 --- /dev/null +++ b/download-images.js @@ -0,0 +1,43 @@ +const fs = require('fs'); +const path = require('path'); +const https = require('https'); +const mysql = require('mysql'); +const { exec } = require('child_process'); + +const downloadDir = path.join(require('os').homedir(), 'Downloads', 'pokemon-images'); + +// Create the download directory if it doesn't exist +if (!fs.existsSync(downloadDir)) { + fs.mkdirSync(downloadDir, { recursive: true }); +} + +// Database connection +const connection = mysql.createConnection({ + host: 'localhost', + user: 'root', + password: '', + database: 'pokedex', +}); + +connection.connect(); + +// Fetch Pokémon data +connection.query('SELECT id, image_url FROM pokemon', (error, results) => { + if (error) throw error; + + results.forEach((pokemon) => { + const file = fs.createWriteStream(path.join(downloadDir, `${pokemon.id}.png`)); + https.get(pokemon.image_url, (response) => { + response.pipe(file); + file.on('finish', () => { + file.close(); + console.log(`Downloaded: ${pokemon.image_url}`); + }); + }).on('error', (err) => { + fs.unlink(path.join(downloadDir, `${pokemon.id}.png`)); + console.error(`Error downloading ${pokemon.image_url}: ${err.message}`); + }); + }); + + connection.end(); +}); diff --git a/update-image-urls.js b/update-image-urls.js new file mode 100644 index 0000000..6e30a79 --- /dev/null +++ b/update-image-urls.js @@ -0,0 +1,30 @@ +const mysql = require('mysql'); + +// Database connection +const connection = mysql.createConnection({ + host: 'localhost', + user: 'root', + password: '', + database: 'pokedex', +}); + +connection.connect(); + +// Fetch Pokémon data +connection.query('SELECT id FROM pokemon', (error, results) => { + if (error) throw error; + + results.forEach((pokemon) => { + const newImageUrl = `./images/${pokemon.id}.png`; + connection.query( + 'UPDATE pokemon SET image_url = ? WHERE id = ?', + [newImageUrl, pokemon.id], + (updateError) => { + if (updateError) throw updateError; + console.log(`Updated image URL for Pokémon ID: ${pokemon.id}`); + } + ); + }); + + connection.end(); +}); diff --git a/v2/get-pokemon.php b/v2/get-pokemon.php index ead884f..fd229d2 100644 --- a/v2/get-pokemon.php +++ b/v2/get-pokemon.php @@ -1,8 +1,8 @@