mirror of
https://github.com/Alvin-Zilverstand/school.git
synced 2026-03-07 05:52:46 +01:00
Add initial files and configuration for Pokedex project, including SVG icons and VSCode settings
This commit is contained in:
43
projects/challange 8/pokedex/download-images.js
Normal file
43
projects/challange 8/pokedex/download-images.js
Normal file
@@ -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();
|
||||
});
|
||||
Reference in New Issue
Block a user