save changes

This commit is contained in:
2025-08-16 11:33:05 +02:00
parent fbe1ceafaf
commit b623317f0d
8 changed files with 479 additions and 14 deletions

View File

@ -18,14 +18,22 @@ async function downloadImage(url: string, filepath: string) {
export async function downloadPinterestImages(keyword: string, numberOfPages: number): Promise<string[]> {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
await page.setViewport({ width: 1280, height: 800 });
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36');
await page.setViewport({ width: 1920, height: 1080 });
const encodedKeyword = encodeURIComponent(keyword);
await page.goto(`https://www.pinterest.com/search/pins/?q=${encodedKeyword}`, { waitUntil: 'networkidle2' });
logger.debug('Searching for:', keyword);
try {
await page.waitForSelector('img[src*="i.pinimg.com"]', { timeout: 10000 });
} catch (error) {
logger.error('Could not find images on the page. Pinterest might have changed its layout or is blocking the scraper.');
await browser.close();
return [];
}
let imageCount = 0;
const downloadedUrls = new Set<string>();
const downloadedImagePaths: string[] = [];
@ -35,18 +43,15 @@ export async function downloadPinterestImages(keyword: string, numberOfPages: nu
try {
const imageUrls = await page.evaluate(() => {
const images = Array.from(document.querySelectorAll('img[src*="i.pinimg.com"]'));
const urls = images.map(img => {
const srcset = (img as HTMLImageElement).srcset;
return images.map(img => {
const image = img as HTMLImageElement;
const srcset = image.srcset;
if (srcset) {
const sources = srcset.split(',').map(s => s.trim());
const source4x = sources.find(s => s.endsWith(' 4x'));
if (source4x) {
return source4x.split(' ')[0];
}
const sources = srcset.split(',').map(s => s.trim().split(' ')[0]);
return sources[sources.length - 1];
}
return null;
});
return urls.filter((url): url is string => url !== null);
return image.src;
}).filter(Boolean);
});
for (const url of imageUrls) {

View File

@ -0,0 +1,46 @@
import * as fs from 'fs/promises';
import * as path from 'path';
import axios from 'axios';
import dotenv from 'dotenv';
dotenv.config();
const COMFY_BASE_URL = process.env.COMFY_BASE_URL?.replace(/\/$/, '');
const COMFY_OUTPUT_DIR = process.env.COMFY_OUTPUT_DIR;
async function generateImage(prompt: string, newFileName: string): Promise<string> {
const workflow = JSON.parse(await fs.readFile('src/comfyworkflows/generate_image.json', 'utf-8'));
workflow['6']['inputs']['text'] = prompt;
const response = await axios.post(`${COMFY_BASE_URL}/prompt`, { prompt: workflow });
const promptId = response.data.prompt_id;
let history;
do {
await new Promise(resolve => setTimeout(resolve, 1000));
const historyResponse = await axios.get(`${COMFY_BASE_URL}/history/${promptId}`);
history = historyResponse.data[promptId];
} while (!history || Object.keys(history.outputs).length === 0);
const files = await fs.readdir(COMFY_OUTPUT_DIR!);
const generatedFiles = files.filter(file => file.startsWith('RADOMVIDEOMAKERIMG'));
const fileStats = await Promise.all(
generatedFiles.map(async (file) => {
const stat = await fs.stat(path.join(COMFY_OUTPUT_DIR!, file));
return { file, mtime: stat.mtime };
})
);
fileStats.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
const latestFile = fileStats[0].file;
const newFilePath = path.resolve('./generated', newFileName);
await fs.mkdir('./generated', { recursive: true });
await fs.rename(path.join(COMFY_OUTPUT_DIR!, latestFile), newFilePath);
return newFilePath;
}
export { generateImage };