function rss_crawler_generate_summary($content, $title) { mb_internal_encoding('UTF-8'); // API-Key Check if (!defined('OPENAI_API_KEY') || empty(OPENAI_API_KEY)) { if (function_exists('rss_crawler_log')) { rss_crawler_log("FEHLER: OPENAI_API_KEY nicht definiert", 'ERROR'); } return ['content' => $content, 'excerpt' => '', 'title' => $title]; } // Nur den Originaltext für den Prompt auf 7000 Zeichen beschränken $prepared_content = mb_substr( preg_replace('/\s+/u', ' ', wp_strip_all_tags( html_entity_decode($content) ) ), 0, 7000 ); // Verbesserter Prompt für GPT-3.5-turbo mit klarerer Struktur und Beispielen $prompt = << 120, // Erhöhtes Timeout für mehr Stabilität 'headers' => [ 'Authorization' => 'Bearer ' . OPENAI_API_KEY, 'Content-Type' => 'application/json; charset=utf-8' ], 'body' => wp_json_encode([ 'model' => 'gpt-3.5-turbo', 'messages' => [ [ 'role' => 'system', 'content' => $system_prompt ], [ 'role' => 'user', 'content' => $prompt ] ], // Optimierte Parameter 'temperature' => 0.8, 'max_tokens' => 4096, 'frequency_penalty' => 0.5, 'presence_penalty' => 0.5 ], JSON_UNESCAPED_UNICODE) ]); // Fehlerbehandlung für API-Response if (is_wp_error($response)) { if (function_exists('rss_crawler_log')) { rss_crawler_log("RSS-Crawler API FEHLER: " . $response->get_error_message(), 'ERROR'); } return ['content' => $content, 'excerpt' => '', 'title' => $title]; } $response_code = wp_remote_retrieve_response_code($response); if ($response_code !== 200) { if (function_exists('rss_crawler_log')) { $response_body = wp_remote_retrieve_body($response); $error_details = json_decode($response_body, true); rss_crawler_log("RSS-Crawler API HTTP FEHLER: " . $response_code, 'ERROR'); if (isset($error_details['error']['message'])) { rss_crawler_log("API Fehlermeldung: " . $error_details['error']['message'], 'ERROR'); } // Versuch mit kleinerer Anfrage return rss_crawler_generate_summary_retry($content, $title, $prepared_content); } return ['content' => $content, 'excerpt' => '', 'title' => $title]; } // Response auswerten $response_body = wp_remote_retrieve_body($response); $response_data = json_decode($response_body, true); if (json_last_error() !== JSON_ERROR_NONE) { if (function_exists('rss_crawler_log')) { rss_crawler_log("RSS-Crawler JSON FEHLER: " . json_last_error_msg(), 'ERROR'); } return ['content' => $content, 'excerpt' => '', 'title' => $title]; } if (!isset($response_data['choices'][0]['message']['content'])) { if (function_exists('rss_crawler_log')) { rss_crawler_log("RSS-Crawler RESPONSE FEHLER: Ungültiges Response-Format", 'ERROR'); } return ['content' => $content, 'excerpt' => '', 'title' => $title]; } $generated = $response_data['choices'][0]['message']['content']; // Log der erhaltenen Antwort für Debugging-Zwecke if (function_exists('rss_crawler_log')) { rss_crawler_log("GPT-3.5-turbo RAW OUTPUT (erste 500 Zeichen): " . substr($generated, 0, 500)); } // Nach fehlenden Tags suchen und korrigieren $generated = rss_crawler_fix_missing_tags($generated); // Verbesserte Parser-Funktion für GPT-3.5-Turbo-Output // Original-Text in Zeilen aufteilen und Leerzeilen verwerfen $lines = preg_split('/\r\n|\r|\n/', $generated); $h1 = ""; $intro = ""; $h2 = ""; $h2_content = ""; $hashtags = ""; $content_sections = []; $current_section = null; $parsing_mode = null; // Durchlaufe alle Zeilen und extrahiere die Inhalte nach den Markierungen foreach ($lines as $line) { $line = trim($line); // Überspringe komplett leere Zeilen if (empty($line)) { continue; } // Verbesserte Erkennungsmuster mit mehr Flexibilität if (preg_match('/^\[H1\]\s*(.+)$/i', $line, $matches)) { $h1 = trim($matches[1]); $parsing_mode = 'h1'; if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER: H1 gefunden: " . $h1); } } elseif (preg_match('/^\[INTRO\]\s*(.+)$/i', $line, $matches)) { $intro = trim($matches[1]); $parsing_mode = 'intro'; if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER: INTRO gefunden: " . substr($intro, 0, 100) . "..."); } } elseif (preg_match('/^\[H2\]\s*(.+)$/i', $line, $matches)) { $h2 = trim($matches[1]); $parsing_mode = 'h2'; if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER: H2 gefunden: " . $h2); } } elseif (preg_match('/^\[H2-ABSATZ\]\s*(.+)$/i', $line, $matches)) { $h2_content = trim($matches[1]); $parsing_mode = 'h2_content'; if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER: H2-ABSATZ gefunden: " . substr($h2_content, 0, 100) . "..."); } } elseif (preg_match('/^\[H3\]\s*(.+)$/i', $line, $matches)) { // Speichere den vorherigen Abschnitt, falls vorhanden if ($current_section) { $content_sections[] = $current_section; if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER: H3-Sektion abgeschlossen und gespeichert."); } } $current_section = [ 'title' => trim($matches[1]), 'content' => '' ]; $parsing_mode = 'h3'; if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER: H3 gefunden: " . $current_section['title']); } } elseif (preg_match('/^\[H3-ABSATZ\]\s*(.+)$/i', $line, $matches)) { if ($current_section) { $current_section['content'] = trim($matches[1]); if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER: H3-ABSATZ gefunden: " . substr($current_section['content'], 0, 100) . "..."); } } $parsing_mode = 'h3_content'; } elseif (preg_match('/^\[HASHTAGS\]\s*(.+)$/i', $line, $matches)) { $hashtags = trim($matches[1]); $parsing_mode = 'hashtags'; if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER: HASHTAGS gefunden: " . $hashtags); } } // Wenn keine neue Sektion beginnt, ergänze den aktuellen Inhalt elseif (!empty($line) && $parsing_mode) { switch ($parsing_mode) { case 'intro': $intro .= ' ' . $line; break; case 'h2_content': $h2_content .= ' ' . $line; break; case 'h3_content': if ($current_section) { $current_section['content'] .= ' ' . $line; } break; } } else { // Unerwartete Zeile ohne Kontext if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER WARNUNG: Unerwartete Zeile ohne Kontext: " . $line); } } } // Letzten Abschnitt speichern, falls vorhanden if ($current_section) { $content_sections[] = $current_section; if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER: Letzte H3-Sektion gespeichert."); } } // Fallback für fehlenden Absatz nach H2 if (empty($h2_content) && !empty($h2)) { $h2_content = 'Im Folgenden betrachten wir die verschiedenen Aspekte dieser brisanten Lage im Detail.'; if (function_exists('rss_crawler_log')) { rss_crawler_log("PARSER WARNUNG: H2-ABSATZ fehlt, Fallback verwendet"); } } // Statistiken loggen if (function_exists('rss_crawler_log')) { // Token-Statistiken if (isset($response_data['usage'])) { rss_crawler_log("Prompt Tokens: " . $response_data['usage']['prompt_tokens']); rss_crawler_log("Completion Tokens: " . $response_data['usage']['completion_tokens']); rss_crawler_log("Total Tokens: " . $response_data['usage']['total_tokens']); } // H3 und P Zählung $h3_count = count($content_sections); rss_crawler_log("Anzahl H3-Überschriften: " . $h3_count); // Verarbeitungszeit $processing_time = microtime(true) - $start_time; rss_crawler_log("Verarbeitungszeit: " . round($processing_time, 2) . " Sekunden"); } // WICHTIG: Generiere einen komplett neuen Content, der den alten ERSETZT // Erstelle den vollständigen Content mit Intro, H2, usw. $formatted_content = ''; // Intro hinzufügen - MIT kursiv (i-Tag) if (!empty($intro)) { $formatted_content .= '

' . esc_html($intro) . '

'; } else { if (function_exists('rss_crawler_log')) { rss_crawler_log("FEHLER: Kein INTRO generiert!", 'ERROR'); } } // H2 hinzufügen if (!empty($h2)) { $formatted_content .= '

' . esc_html($h2) . '

'; // Absatz nach H2 if (!empty($h2_content)) { $formatted_content .= '

' . esc_html($h2_content) . '

'; } } else { if (function_exists('rss_crawler_log')) { rss_crawler_log("FEHLER: Keine H2 generiert!", 'ERROR'); } } // H3/P Paare hinzufügen foreach ($content_sections as $index => $section) { if (!empty($section['title'])) { $formatted_content .= '

' . esc_html($section['title']) . '

'; } if (!empty($section['content'])) { $formatted_content .= '

' . esc_html($section['content']) . '

'; } else { if (function_exists('rss_crawler_log')) { rss_crawler_log("FEHLER: Kein Content für H3 #" . ($index + 1), 'ERROR'); } } } // Hashtags direkt in den Content einfügen if (!empty($hashtags)) { $formatted_content .= '

Hashtags: ' . esc_html($hashtags) . '

'; } // WICHTIG: IMMER den AI-generierten Titel verwenden if (empty($h1)) { // Fallback falls kein H1 generiert wurde $new_title = "Satirischer Artikel: " . $title; if (function_exists('rss_crawler_log')) { rss_crawler_log("WARNUNG: Keine H1 generiert, Fallback-Titel verwendet", 'WARNING'); } } else { // Den von AI generierten Titel verwenden $new_title = $h1; } // Sicherheitsprüfung: Stellen sicher, dass $formatted_content nicht leer ist if (empty($formatted_content)) { if (function_exists('rss_crawler_log')) { rss_crawler_log("KRITISCHER FEHLER: Kein Content erzeugt! Verwende Originalcontent als Fallback", 'ERROR'); } $formatted_content = $content; // Fallback zum Originalcontent } register_shutdown_function(function() { sleep(3); if (function_exists('rss_crawler_log')) { rss_crawler_log("=== WARTE 3 SEKUNDEN NACH RETURN ==="); } }); // Die Zeichenersetzung anwenden (falls vorhanden) if (function_exists('rss_crawler_replace_punctuation')) { $transformed_content = rss_crawler_replace_punctuation($formatted_content); $transformed_excerpt = !empty($intro) ? rss_crawler_replace_punctuation($intro) : ''; $transformed_title = !empty($h1) ? rss_crawler_replace_punctuation($h1) : $new_title; } else { // Fallback falls die Funktion nicht existiert $transformed_content = $formatted_content; $transformed_excerpt = $intro; $transformed_title = $new_title; } sleep(2); if (function_exists('rss_crawler_log')) { rss_crawler_log("=== WARTE 2 SEKUNDEN VOR RETURN ==="); } // KRITISCH: Gib KEINEN excerpt zurück - lass das Theme den Titel anzeigen return [ 'content' => $transformed_content, 'excerpt' => '', // KEIN Excerpt zurückgeben! 'title' => $transformed_title ]; } function rss_crawler_generate_summary_retry($content, $title, $prepared_content) { if (function_exists('rss_crawler_log')) { rss_crawler_log("Starte RETRY mit reduzierter Komplexität..."); } // Kürzerer Originaltext $shorter_content = mb_substr($prepared_content, 0, 3500); // Einfacherer System-Prompt $system_prompt = "Erstelle eine satirische Version des Textes. Benutze für jeden Abschnitt exakt die passenden Tags: [H1], [INTRO], [H2], [H2-ABSATZ], [H3], [H3-ABSATZ], [HASHTAGS]."; // Vereinfachter User-Prompt $prompt = "Erstelle eine hyperradikale Satire des folgenden Textes. Benutze folgende Tags: [H1] für Überschrift [INTRO] für Einleitung [H2] für Unterüberschrift [H2-ABSATZ] für Absatz nach H2 [H3] für weitere Überschriften (ca. 5-8) [H3-ABSATZ] für Absätze nach H3 [HASHTAGS] für Hashtags am Ende Originaltext: " . $shorter_content; // API-Aufruf mit vereinfachten Parametern $response = wp_remote_post('https://api.openai.com/v1/chat/completions', [ 'timeout' => 120, 'headers' => [ 'Authorization' => 'Bearer ' . OPENAI_API_KEY, 'Content-Type' => 'application/json; charset=utf-8' ], 'body' => wp_json_encode([ 'model' => 'gpt-3.5-turbo', 'messages' => [ [ 'role' => 'system', 'content' => $system_prompt ], [ 'role' => 'user', 'content' => $prompt ] ], 'temperature' => 0.8, 'max_tokens' => 2048 // Reduzierte Token-Anzahl ], JSON_UNESCAPED_UNICODE) ]); // Einfache Fehlerbehandlung if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { if (function_exists('rss_crawler_log')) { rss_crawler_log("RETRY FEHLGESCHLAGEN: Gebe Originalinhalt zurück", 'ERROR'); } return ['content' => $content, 'excerpt' => '', 'title' => $title]; } // Minimal-Verarbeitung $response_body = wp_remote_retrieve_body($response); $response_data = json_decode($response_body, true); if (!isset($response_data['choices'][0]['message']['content'])) { return ['content' => $content, 'excerpt' => '', 'title' => $title]; } $generated = $response_data['choices'][0]['message']['content']; $generated = rss_crawler_fix_missing_tags($generated); // Parse den generierten Text return rss_crawler_parse_and_format($generated, $content, $title); } /** * Hilfsfunktion zur einheitlichen Verarbeitung der generierten Texte */ function rss_crawler_parse_and_format($generated, $original_content, $original_title) { // Parsing-Logik aus der Hauptfunktion $lines = preg_split('/\r\n|\r|\n/', $generated); $h1 = ""; $intro = ""; $h2 = ""; $h2_content = ""; $hashtags = ""; $content_sections = []; $current_section = null; $parsing_mode = null; // Durchlaufe alle Zeilen und extrahiere die Inhalte foreach ($lines as $line) { $line = trim($line); if (empty($line)) continue; if (preg_match('/^\[H1\]\s*(.+)$/i', $line, $matches)) { $h1 = trim($matches[1]); $parsing_mode = 'h1'; } elseif (preg_match('/^\[INTRO\]\s*(.+)$/i', $line, $matches)) { $intro = trim($matches[1]); $parsing_mode = 'intro'; } elseif (preg_match('/^\[H2\]\s*(.+)$/i', $line, $matches)) { $h2 = trim($matches[1]); $parsing_mode = 'h2'; } elseif (preg_match('/^\[H2-ABSATZ\]\s*(.+)$/i', $line, $matches)) { $h2_content = trim($matches[1]); $parsing_mode = 'h2_content'; } elseif (preg_match('/^\[H3\]\s*(.+)$/i', $line, $matches)) { if ($current_section) { $content_sections[] = $current_section; } $current_section = [ 'title' => trim($matches[1]), 'content' => '' ]; $parsing_mode = 'h3'; } elseif (preg_match('/^\[H3-ABSATZ\]\s*(.+)$/i', $line, $matches)) { if ($current_section) { $current_section['content'] = trim($matches[1]); } $parsing_mode = 'h3_content'; } elseif (preg_match('/^\[HASHTAGS\]\s*(.+)$/i', $line, $matches)) { $hashtags = trim($matches[1]); $parsing_mode = 'hashtags'; } // Wenn keine neue Sektion beginnt, ergänze den aktuellen Inhalt elseif (!empty($line) && $parsing_mode) { switch ($parsing_mode) { case 'intro': $intro .= ' ' . $line; break; case 'h2_content': $h2_content .= ' ' . $line; break; case 'h3_content': if ($current_section) { $current_section['content'] .= ' ' . $line; } break; } } } // Letzten Abschnitt speichern if ($current_section) { $content_sections[] = $current_section; } // Formatierung des Inhalts $formatted_content = ''; if (!empty($intro)) { $formatted_content .= '

' . esc_html($intro) . '

'; } if (!empty($h2)) { $formatted_content .= '

' . esc_html($h2) . '

'; if (!empty($h2_content)) { $formatted_content .= '

' . esc_html($h2_content) . '

'; } } foreach ($content_sections as $section) { if (!empty($section['title'])) { $formatted_content .= '

' . esc_html($section['title']) . '

'; } if (!empty($section['content'])) { $formatted_content .= '

' . esc_html($section['content']) . '

'; } } if (!empty($hashtags)) { $formatted_content .= '

Hashtags: ' . esc_html($hashtags) . '

'; } // Titel festlegen $new_title = !empty($h1) ? $h1 : "Satirischer Artikel: " . $original_title; // Falls alles fehlschlägt, Original-Content verwenden if (empty($formatted_content)) { $formatted_content = $original_content; } // Zeichenersetzung anwenden, falls verfügbar if (function_exists('rss_crawler_replace_punctuation')) { $transformed_content = rss_crawler_replace_punctuation($formatted_content); $transformed_title = rss_crawler_replace_punctuation($new_title); } else { $transformed_content = $formatted_content; $transformed_title = $new_title; } return [ 'content' => $transformed_content, 'excerpt' => '', 'title' => $transformed_title ]; } /** * Hilfsfunktion, um die Absatzmarkierungen für GPT-3.5-Turbo zu verbessern * Falls GPT trotzdem keine klaren Absatzmarkierungen liefert */ function rss_crawler_fix_missing_tags($generated_content) { // Typische Muster für fehlende Tags erkennen und korrigieren $patterns = [ // Format: H1 ohne Tag '/^(.{10,75}(?::|-)(?:.{3,20}))\s*\n/m' => "[H1] $1\n", // Format: Intro-Absatz mit Du-Ansprache ohne Tag '/^(Du\s.{50,500}(?:\?|\.))$/m' => "[INTRO] $1", // Format: H2 Überschrift-ähnliche Zeilen ohne Tag '/^(.{10,80}(?::|-)(?:.{3,20})\s*[🔍📊📈📌])\s*$/m' => "[H2] $1", // Format: Absatz nach H2-ähnlicher Struktur ohne Tag '/\[H2\].*\n(?!\[)(.{50,500})/s' => "[H2]\n[H2-ABSATZ] $1", // Format: H3-ähnliche Zeilen ohne Tag '/^(.{10,75}(?::|-)(?:.{3,20})\s*[🔍📊📈📌📢])\s*$/m' => "[H3] $1", // Format: Absatz nach H3 ohne Tag '/\[H3\].*\n(?!\[)(.{50,500})/s' => "[H3]\n[H3-ABSATZ] $1", // Format: Hashtag-Listen ohne Tag '/^((?:#[a-zA-Z0-9]+\s){5,12})$/m' => "[HASHTAGS] $1" ]; // Alle Muster anwenden $fixed_content = $generated_content; foreach ($patterns as $pattern => $replacement) { $fixed_content = preg_replace($pattern, $replacement, $fixed_content); } return $fixed_content; } function rss_crawler_get_first_image($content) { $pattern = '/]+>/i'; if (preg_match($pattern, $content, $matches)) { return $matches[0]; } return ''; }
Warning: Cannot modify header information - headers already sent by (output started at /var/customers/webs/Muhsin/muhsin.de/wp-content/plugins/rss-crawler/rss-crawler.php:1) in /var/customers/webs/Muhsin/muhsin.de/wp-includes/pluggable.php on line 1435

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