/** * STAR-GEBURTSTAGE ENGINE (MIX1.DE - DYNAMISCHE URLS FÜR HEUTE & MORGEN) */ add_action('wp_ajax_musicstar_get_birthdays', 'musicstar_ajax_get_birthdays'); add_action('wp_ajax_nopriv_musicstar_get_birthdays', 'musicstar_ajax_get_birthdays'); function musicstar_ajax_get_birthdays() { $data = musicstar_get_birthdays(true); wp_send_json_success($data); } function musicstar_scrape_mix1_single_day($month, $day) { // Greift gezielt auf die Tages-URL von mix1 zu (z.B. https://www.mix1.de/geburtstage/09-04/) $url = sprintf('https://www.mix1.de/geburtstage/%02d-%02d/', $month, $day); $response = wp_remote_get($url, [ 'timeout' => 8, 'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' ]); $artists = []; if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) { $html = wp_remote_retrieve_body($response); // Regex liest Namen, Geburtsdatum und Alter zuverlässig aus if (preg_match_all('/([^\d\n\<\>\*]+?)\s+(\d{2}\.\d{2}\.\d{4})\s+(?:heute\s+)?(\d+)\s+Jahre/u', $html, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $name = trim(strip_tags($match[1])); if (strlen($name) > 1 && !isset($artists[$name])) { $artists[$name] = [ 'name' => $name, 'birthdate' => trim($match[2]), 'age' => trim($match[3]) . ' Jahre', ]; } } } } return array_values($artists); } function musicstar_get_birthdays($force_refresh = false) { $today_ts = current_time('timestamp'); $tomorrow_ts = strtotime('+1 day', $today_ts); $month_today = date('m', $today_ts); $day_today = date('d', $today_ts); $month_tomorrow = date('m', $tomorrow_ts); $day_tomorrow = date('d', $tomorrow_ts); $transient_key = 'musicstar_birthdays_fixed_' . $month_today . '_' . $day_today; if (!$force_refresh) { $cached = get_transient($transient_key); if ($cached) return $cached; } // Abruf über die spezifischen Tages-URLs $today_items = musicstar_scrape_mix1_single_day($month_today, $day_today); $tomorrow_items = musicstar_scrape_mix1_single_day($month_tomorrow, $day_tomorrow); // Moderationstexte für On-Air $mod_today = "Kerzen an im Sektor! Wir gratulieren allen heutigen Geburtstagskindern aus der Musikwelt herzlich!"; if (!empty($today_items)) { $first = $today_items[0]; $mod_today = "Heute feiert unter anderem {$first['name']} den {$first['age']} Geburtstag. Wir gratulieren ganz herzlich aus dem Radio MusicStar Studio!"; } $mod_tomorrow = "Vorausschau für morgen: Auch morgen feiert der Sektor einige Musik-Stars!"; if (!empty($tomorrow_items)) { $first_m = $tomorrow_items[0]; $mod_tomorrow = "Morgen feiert unter anderem {$first_m['name']} den {$first_m['age']} Geburtstag. Schon mal vormerken!"; } $result = [ 'today_date' => date('d.m.Y', $today_ts), 'today_items' => array_slice($today_items, 0, 10), 'today_moderation' => $mod_today, 'tomorrow_date' => date('d.m.Y', $tomorrow_ts), 'tomorrow_items' => array_slice($tomorrow_items, 0, 10), 'tomorrow_moderation' => $mod_tomorrow, ]; set_transient($transient_key, $result, 14400); // 4 Stunden Cache return $result; }
Beitrags-Kommentare (0)