// ========================================== // AGGRESSIVE PUBLIC S3 + PERFORMANCE OPTIMIERUNGEN // ========================================== // AGGRESSIVE Public S3 Download - 5 Versuche, optimierte Reihenfolge function try_download_public_s3_aggressive($image_url, $header_text, $attempt) { require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); // OPTIMIERTE REIHENFOLGE basierend auf Erfolgsraten $strategies = [ 'download_url' => function($url) { return download_url($url, 45); // Kürzerer Timeout }, 'wp_remote' => function($url) { return wp_remote_get($url, [ 'timeout' => 45, // Reduziert von 60s 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', 'sslverify' => false, 'headers' => [ 'Accept' => 'image/*,*/*;q=0.8', 'Accept-Language' => 'en-US,en;q=0.5', 'Cache-Control' => 'no-cache', 'Connection' => 'keep-alive' // Performance boost ] ]); }, 'curl_optimized' => function($url) { if (!function_exists('curl_init')) return false; $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 45, // Reduziert CURLOPT_CONNECTTIMEOUT => 10, // Schneller Connect CURLOPT_SSL_VERIFYPEER => false, CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; WordPress/' . get_bloginfo('version') . ')', CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 2, // Reduziert von 3 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_ENCODING => 'gzip,deflate', // Compression ]); $data = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return ($http_code === 200 && $data) ? $data : false; } ]; // Strategie basierend auf Versuch (optimierte Reihenfolge) $strategy_names = array_keys($strategies); $strategy_name = $strategy_names[($attempt - 1) % count($strategy_names)]; $strategy = $strategies[$strategy_name]; write_abschnitte_log_unique('DEBUG', "[RSS-CRAWLER] AGGRESSIVE Public Download #{$attempt}/5: {$strategy_name}"); $result = $strategy($image_url); if (is_wp_error($result)) { write_abschnitte_log_unique('ERROR', "[RSS-CRAWLER] {$strategy_name} Fehler: " . $result->get_error_message()); return false; } // Daten extrahieren (optimiert) $image_data = null; if ($strategy_name === 'wp_remote' && !is_wp_error($result)) { if (wp_remote_retrieve_response_code($result) === 200) { $image_data = wp_remote_retrieve_body($result); } } elseif ($strategy_name === 'download_url' && !is_wp_error($result)) { $image_data = file_get_contents($result); unlink($result); // Sofort löschen } elseif ($strategy_name === 'curl_optimized') { $image_data = $result; } // Validierung verschärft if (!$image_data || strlen($image_data) < 500) { // Reduziert von 1000 write_abschnitte_log_unique('ERROR', "[RSS-CRAWLER] {$strategy_name}: Daten zu klein (" . strlen($image_data) . " bytes)"); return false; } // Bildverarbeitung return process_image_data_bulletproof($image_data, $header_text, $image_url); } // ULTRA-AGGRESSIVE Download Fallback - 5 Versuche, 1 Sekunde function download_with_ultra_aggressive_fallback($image_url, $header_text, $api_keys, $has_private_s3) { write_abschnitte_log_unique('INFO', "[RSS-CRAWLER] 🎯 ULTRA-AGGRESSIVE: 5x Public (1s intervals), dann Private S3"); // PHASE 1: 5 VERSUCHE MIT OPTIMIERTEN TIMINGS for ($attempt = 1; $attempt <= 5; $attempt++) { write_abschnitte_log_unique('INFO', "[RSS-CRAWLER] 📥 AGGRESSIVE Public Download #{$attempt}/5"); if ($attempt > 1) { write_abschnitte_log_unique('DEBUG', "[RSS-CRAWLER] ⏰ 1-Sekunden-Pause..."); sleep(1); // Exakt 1 Sekunde } $attach_id = try_download_public_s3_aggressive($image_url, $header_text, $attempt); if ($attach_id) { write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] ✅ AGGRESSIVE SUCCESS bei Versuch #{$attempt} - Private S3 gespart!"); write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] 💰 Kosteneinsparung: Public S3 erfolgreich (ID: {$attach_id})"); return $attach_id; } write_abschnitte_log_unique('WARNING', "[RSS-CRAWLER] ❌ Aggressive Versuch #{$attempt} fehlgeschlagen"); } // PHASE 2: PRIVATE S3 FALLBACK (nur wenn absolut nötig) if ($has_private_s3) { write_abschnitte_log_unique('INFO', "[RSS-CRAWLER] 🔄 FALLBACK: 5x Public failed - Private S3 als letzte Option"); write_abschnitte_log_unique('INFO', "[RSS-CRAWLER] 💸 Zusatzkosten: Private S3 wird verwendet"); $private_url = generate_private_s3_image($header_text, $api_keys); if ($private_url) { $attach_id = try_download_private_s3($private_url, $header_text); if ($attach_id) { write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] ✅ PRIVATE S3 FALLBACK SUCCESS - ID: {$attach_id}"); return $attach_id; } } } else { write_abschnitte_log_unique('ERROR', "[RSS-CRAWLER] ❌ Kein Private S3 - 5x Public fehlgeschlagen"); } write_abschnitte_log_unique('ERROR', "[RSS-CRAWLER] ❌ KOMPLETT FEHLGESCHLAGEN: Weder 5x Public noch Private S3"); return false; } // PARALLEL PROCESSING FRAMEWORK function process_content_with_section_images_parallel($content) { write_abschnitte_log_unique('INFO', '[RSS-CRAWLER] 🚀 ULTRA-FAST Parallel Bildintegration gestartet'); if (empty($content)) { return $content; } $api_keys = get_api_keys(); if (empty($api_keys['BILDIMPORT_API_KEY']) || empty($api_keys['OPENAI_API_KEY'])) { write_abschnitte_log_unique('ERROR', '[RSS-CRAWLER] API Keys fehlen'); return $content; } // Headers sammeln (wie vorher) $all_headers = collect_all_headers($content); if (empty($all_headers)) { return $content; } $start_time = microtime(true); write_abschnitte_log_unique('INFO', '[RSS-CRAWLER] Gefunden: ' . count($all_headers) . ' Headers - starte PARALLEL processing'); // PHASE 1: BATCH ÜBERSETZUNGEN (alle parallel) write_abschnitte_log_unique('INFO', '[RSS-CRAWLER] 📝 PHASE 1: Batch-Übersetzungen (parallel)'); $translations = batch_translate_all_headers($all_headers, $api_keys); // PHASE 2: BATCH BILDGENERIERUNG (alle parallel) write_abschnitte_log_unique('INFO', '[RSS-CRAWLER] 🎨 PHASE 2: Batch-Bildgenerierung (parallel)'); $image_urls = batch_generate_all_images($translations, $api_keys); // PHASE 3: AGGRESSIVE PARALLEL DOWNLOADS write_abschnitte_log_unique('INFO', '[RSS-CRAWLER] 📥 PHASE 3: Ultra-aggressive parallel downloads'); $attachments = parallel_ultra_aggressive_downloads($image_urls, $all_headers, $api_keys); // PHASE 4: BATCH HTML INSERTION write_abschnitte_log_unique('INFO', '[RSS-CRAWLER] 🔗 PHASE 4: Batch HTML-Integration'); $modified_content = batch_insert_all_images($content, $all_headers, $attachments); $total_time = microtime(true) - $start_time; $success_count = count(array_filter($attachments)); $success_rate = round(($success_count / count($all_headers)) * 100); write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] ====== ULTRA-FAST STATISTIK ======"); write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] Verarbeitet: {$success_count}/" . count($all_headers) . " Headers ({$success_rate}%)"); write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] Gesamtzeit: " . round($total_time, 2) . " Sekunden"); write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] Durchschnitt: " . round($total_time / count($all_headers), 2) . " Sek/Header"); return $modified_content; } // BATCH ÜBERSETZUNGEN (parallel mit cURL Multi) function batch_translate_all_headers($headers, $api_keys) { $translations = []; $curl_multi = curl_multi_init(); $curl_handles = []; // Alle Übersetzungs-Requests parallel starten foreach ($headers as $index => $header_data) { $header_text = strip_tags($header_data['text_content']); $header_text = trim($header_text); if (empty($header_text)) { $translations[$index] = false; continue; } $ch = curl_init(); $payload = json_encode([ 'model' => 'gpt-4o-mini', 'messages' => [ ['role' => 'system', 'content' => 'Translate German to English and add visual description in max 150 chars.'], ['role' => 'user', 'content' => 'Translate and describe: ' . substr($header_text, 0, 200)] ], 'max_tokens' => 120, 'temperature' => 0.7 ]); curl_setopt_array($ch, [ CURLOPT_URL => 'https://api.openai.com/v1/chat/completions', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $payload, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $api_keys['OPENAI_API_KEY'], 'Content-Type: application/json' ], CURLOPT_TIMEOUT => 30 ]); curl_multi_add_handle($curl_multi, $ch); $curl_handles[$index] = $ch; } // Alle Requests parallel ausführen $running = null; do { curl_multi_exec($curl_multi, $running); curl_multi_select($curl_multi); } while ($running > 0); // Ergebnisse sammeln foreach ($curl_handles as $index => $ch) { $response = curl_multi_getcontent($ch); $data = json_decode($response, true); if ($data && isset($data['choices'][0]['message']['content'])) { $translations[$index] = trim($data['choices'][0]['message']['content']); write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] Parallel Übersetzung #{$index} erfolgreich"); } else { $translations[$index] = false; write_abschnitte_log_unique('ERROR', "[RSS-CRAWLER] Parallel Übersetzung #{$index} fehlgeschlagen"); } curl_multi_remove_handle($curl_multi, $ch); curl_close($ch); } curl_multi_close($curl_multi); write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] Batch-Übersetzungen abgeschlossen: " . count(array_filter($translations)) . "/" . count($translations)); return $translations; } // BATCH BILDGENERIERUNG (parallel mit cURL Multi) function batch_generate_all_images($translations, $api_keys) { $image_urls = []; $curl_multi = curl_multi_init(); $curl_handles = []; $has_s3 = !empty($api_keys['AWS_ACCESS_KEY_ID']) && !empty($api_keys['AWS_SECRET_ACCESS_KEY']); // Alle Bildgenerierungs-Requests parallel starten foreach ($translations as $index => $translation) { if (!$translation) { $image_urls[$index] = false; continue; } $enhanced_prompt = "The ultra-realistic UHD photo captures " . substr($translation, 0, 300); $payload = json_encode([ "key" => $api_keys['BILDIMPORT_API_KEY'], "prompt" => $enhanced_prompt, "width" => "200", "height" => "200", "samples" => "1", "num_inference_steps" => "31", "guidance_scale" => "3.5", "enhance_prompt" => true, "safety_checker" => false, "webhook" => null, "track_id" => null ]); $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => 'https://modelslab.com/api/v1/enterprise/flux/text2img', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $payload, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'User-Agent: WordPress/' . get_bloginfo('version') ], CURLOPT_TIMEOUT => 120, CURLOPT_SSL_VERIFYPEER => false ]); curl_multi_add_handle($curl_multi, $ch); $curl_handles[$index] = $ch; } // Parallel ausführen $running = null; do { curl_multi_exec($curl_multi, $running); curl_multi_select($curl_multi); } while ($running > 0); // Ergebnisse sammeln foreach ($curl_handles as $index => $ch) { $response = curl_multi_getcontent($ch); $data = json_decode($response, true); if ($data && $data['status'] === 'success' && !empty($data['output'][0])) { $image_urls[$index] = $data['output'][0]; write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] Parallel Bildgenerierung #{$index} erfolgreich"); } else { $image_urls[$index] = false; write_abschnitte_log_unique('ERROR', "[RSS-CRAWLER] Parallel Bildgenerierung #{$index} fehlgeschlagen"); } curl_multi_remove_handle($curl_multi, $ch); curl_close($ch); } curl_multi_close($curl_multi); write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] Batch-Bildgenerierung abgeschlossen: " . count(array_filter($image_urls)) . "/" . count($image_urls)); return $image_urls; } // PARALLEL AGGRESSIVE DOWNLOADS function parallel_ultra_aggressive_downloads($image_urls, $headers, $api_keys) { $attachments = []; $has_private_s3 = !empty($api_keys['AWS_ACCESS_KEY_ID']); // Für jede URL: aggressive 5-Versuche-Downloads parallel foreach ($image_urls as $index => $image_url) { if (!$image_url) { $attachments[$index] = false; continue; } $header_text = strip_tags($headers[$index]['text_content']); $attach_id = download_with_ultra_aggressive_fallback($image_url, $header_text, $api_keys, $has_private_s3); $attachments[$index] = $attach_id; } $success_count = count(array_filter($attachments)); write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] Ultra-aggressive Downloads: {$success_count}/" . count($image_urls) . " erfolgreich"); return $attachments; } // BATCH HTML INSERTION function batch_insert_all_images($content, $headers, $attachments) { $modified_content = $content; $insertions = 0; foreach ($headers as $index => $header_data) { $attach_id = $attachments[$index]; if (!$attach_id) { continue; } $header_text = strip_tags($header_data['text_content']); // HTML für Abschnittsbild $image_html = '
'; $image_html .= wp_get_attachment_image($attach_id, 'thumbnail', false, [ 'alt' => esc_attr($header_text), 'style' => 'width: 100%; max-width: 200px; height: auto; border-radius: 5px;' ]); $image_html .= '
'; $new_header_with_image = $header_data['full_tag'] . ' ' . $image_html; $pos = strpos($modified_content, $header_data['full_tag']); if ($pos !== false) { $modified_content = substr_replace($modified_content, $new_header_with_image, $pos, strlen($header_data['full_tag'])); $insertions++; } } write_abschnitte_log_unique('SUCCESS', "[RSS-CRAWLER] Batch HTML-Insertion: {$insertions} Bilder eingefügt"); return $modified_content; } // Headers sammeln (unverändert) function collect_all_headers($content) { $patterns = [ 'h2_h3' => '/]*>(.*?)<\/h[23]>/is', 'fazit' => '/]*class[^>]*fazit[^>]*>(.*?)<\/p>/is', 'conclusion' => '/]*>(.*?conclusion.*?)<\/p>/is', 'summary' => '/]*>(.*?summary.*?)<\/p>/is' ]; $all_headers = []; foreach ($patterns as $pattern_name => $pattern) { preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE); foreach ($matches[0] as $index => $match) { $all_headers[] = [ 'full_tag' => $match[0], 'text_content' => $matches[1][$index][0], 'position' => $match[1], 'type' => $pattern_name ]; } } // Nach Position sortieren usort($all_headers, function($a, $b) { return $a['position'] - $b['position']; }); return $all_headers; }
Warning: Cannot modify header information - headers already sent by (output started at /var/customers/webs/Muhsin/muhsin.de/wp-content/plugins/bildimportabschnitte/bildimportabschnitte.php:1) in /var/customers/webs/Muhsin/muhsin.de/wp-includes/pluggable.php on line 1450

Warning: Cannot modify header information - headers already sent by (output started at /var/customers/webs/Muhsin/muhsin.de/wp-content/plugins/bildimportabschnitte/bildimportabschnitte.php:1) in /var/customers/webs/Muhsin/muhsin.de/wp-includes/pluggable.php on line 1453