94 lines
3.2 KiB
JavaScript
94 lines
3.2 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import https from 'https';
|
|
|
|
const API_KEY = "z8LbaRbolg9A7BKNmGbitUY5JsUuSTogwFLTR6dMu4cY53zstDjWNmbR";
|
|
const TARGET_DIR = path.resolve("./public/images");
|
|
|
|
if (!fs.existsSync(TARGET_DIR)) {
|
|
fs.mkdirSync(TARGET_DIR, { recursive: true });
|
|
}
|
|
|
|
const queries = [
|
|
{ category: 'hero_bartender', query: 'charismatic male bartender mixing drink bar', count: 4 },
|
|
{ category: 'speakeasy', query: 'dark whiskey cocktail bar luxury amber', count: 6 },
|
|
{ category: 'alchemist', query: 'molecular mixology smoke cocktail dry ice beaker', count: 6 },
|
|
{ category: 'editorial', query: 'minimalist cocktail aesthetic elegant glass garnish', count: 6 },
|
|
{ category: 'tropical', query: 'tropical exotic cocktail colorful passionfruit tiki summer', count: 6 },
|
|
{ category: 'instagram', query: 'craft cocktail bartender pouring aesthetic bar party', count: 8 },
|
|
];
|
|
|
|
function fetchJson(url) {
|
|
return new Promise((resolve, reject) => {
|
|
https.get(url, { headers: { Authorization: API_KEY } }, (res) => {
|
|
let data = '';
|
|
res.on('data', (chunk) => data += chunk);
|
|
res.on('end', () => {
|
|
try {
|
|
resolve(JSON.parse(data));
|
|
} catch (e) {
|
|
reject(e);
|
|
}
|
|
});
|
|
}).on('error', reject);
|
|
});
|
|
}
|
|
|
|
function downloadFile(url, dest) {
|
|
return new Promise((resolve, reject) => {
|
|
const file = fs.createWriteStream(dest);
|
|
https.get(url, (response) => {
|
|
if (response.statusCode === 301 || response.statusCode === 302) {
|
|
return downloadFile(response.headers.location, dest).then(resolve).catch(reject);
|
|
}
|
|
response.pipe(file);
|
|
file.on('finish', () => {
|
|
file.close(resolve);
|
|
});
|
|
}).on('error', (err) => {
|
|
fs.unlink(dest, () => reject(err));
|
|
});
|
|
});
|
|
}
|
|
|
|
async function run() {
|
|
console.log("Starting image downloads from Pexels...");
|
|
const manifest = {};
|
|
|
|
for (const q of queries) {
|
|
console.log(`Searching for: ${q.query}...`);
|
|
try {
|
|
const data = await fetchJson(`https://api.pexels.com/v1/search?query=${encodeURIComponent(q.query)}&per_page=${q.count}&orientation=landscape`);
|
|
manifest[q.category] = [];
|
|
|
|
if (data.photos && data.photos.length > 0) {
|
|
for (let i = 0; i < data.photos.length; i++) {
|
|
const photo = data.photos[i];
|
|
const filename = `${q.category}_${i + 1}.jpg`;
|
|
const filePath = path.join(TARGET_DIR, filename);
|
|
const imgUrl = photo.src.large || photo.src.large2x || photo.src.original;
|
|
|
|
console.log(` Downloading ${filename} (${photo.photographer})...`);
|
|
await downloadFile(imgUrl, filePath);
|
|
|
|
manifest[q.category].push({
|
|
id: photo.id,
|
|
file: `/images/${filename}`,
|
|
alt: photo.alt || `${q.category} cocktail image`,
|
|
photographer: photo.photographer,
|
|
avg_color: photo.avg_color
|
|
});
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error(`Error processing query ${q.query}:`, err.message);
|
|
}
|
|
}
|
|
|
|
// Save manifest
|
|
fs.writeFileSync(path.join(TARGET_DIR, "manifest.json"), JSON.stringify(manifest, null, 2));
|
|
console.log("Image downloads complete! Manifest created at public/images/manifest.json");
|
|
}
|
|
|
|
run();
|