/** * =================================================================== * HÀM CÀO DỮ LIỆU VÀ TẠO BÀI VIẾT * =================================================================== */ function jvi_scrape_and_create_post($url, $is_bulk = false) { $response = wp_remote_get($url, ['timeout' => 30, 'user-agent' => 'Mozilla/5.0']); if (is_wp_error($response)) return ['status' => 'error', 'message' => 'Lỗi kết nối: ' . $response->get_error_message()]; if (wp_remote_retrieve_response_code($response) != 200) return ['status' => 'error', 'message' => 'Không thể kết nối đến URL (Mã lỗi: ' . wp_remote_retrieve_response_code($response) . ').']; $html_content = wp_remote_retrieve_body($response); $dom = new DOMDocument(); libxml_use_internal_errors(true); // Thử load HTML với try-catch để bắt lỗi try { $dom->loadHTML(mb_convert_encoding($html_content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); } catch (Exception $e) { error_log('Lỗi loadHTML: ' . $e->getMessage()); libxml_clear_errors(); return ['status' => 'error', 'message' => 'Lỗi phân tích HTML: ' . $e->getMessage()]; } libxml_clear_errors(); error_log('Lỗi XML (nếu có): ' . print_r(libxml_get_errors(), true)); $xpath = new DOMXPath($dom); $is_javboys = strpos($url, 'javboys.tv') !== false; $is_jpboy = strpos($url, 'jpboy1069.net') !== false; // Cập nhật XPath để lấy tiêu đề từ bên trong

$title_node = $xpath->query('//h1[contains(@class, "entry-title")]//a')->item(0); $title = $title_node ? trim($title_node->nodeValue) : ''; if (empty($title)) { // Thử lấy từ

trực tiếp với nhiều điều kiện hơn $title_node = $xpath->query('//h1[contains(@class, "post-title") or contains(@class, "entry-title") or contains(@class, "title") or @class=""]')->item(0); $title = $title_node ? trim($title_node->nodeValue) : ''; if (empty($title)) { error_log('Không tìm thấy tiêu đề cho URL: ' . $url); return ['status' => 'error', 'message' => 'Không tìm thấy tiêu đề.']; } } // Lấy ảnh đại diện $image_node = $xpath->query("//meta[@property='og:image']")->item(0); $image_url = $image_node ? $image_node->getAttribute('content') : ''; if (empty($image_url)) { error_log('Không tìm thấy ảnh đại diện cho URL: ' . $url); return ['status' => 'error', 'message' => 'Không tìm thấy ảnh đại diện.']; } // Lấy iframe video với điều kiện mở rộng $iframe_node = $xpath->query('//div[contains(@class, "videohere") or contains(@class, "video-container") or contains(@class, "embed")]//iframe')->item(0); $embed_code = $iframe_node ? $dom->saveHTML($iframe_node) : ''; $video_url = $iframe_node ? $iframe_node->getAttribute('src') : ''; // Nếu không lấy được video_url trực tiếp, thử trích xuất từ nội dung if (empty($video_url) && !empty($embed_code)) { preg_match('/src="([^"]+)"/', $embed_code, $matches); $video_url = !empty($matches[1]) ? $matches[1] : ''; } // Lấy tag/thể loại (chỉ cho javboys.tv) $tags = []; if ($is_javboys) { $tag_nodes = $xpath->query('//div[contains(@class, "tags") or contains(@class, "categories")]//a'); foreach ($tag_nodes as $tag_node) { $tag = trim($tag_node->nodeValue); if (!empty($tag)) { $tags[] = sanitize_title($tag); } } } require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attachment_id = media_sideload_image($image_url, 0, $title, 'id'); if (is_wp_error($attachment_id)) return ['status' => 'error', 'message' => 'Lỗi tải ảnh: ' . $attachment_id->get_error_message()]; // Tạo nội dung bài viết ban đầu với iframe cũ $post_content = $embed_code; $new_post_args = [ 'post_title' => wp_strip_all_tags($title), 'post_content' => $post_content, 'post_status' => 'draft', 'post_author' => get_current_user_id(), ]; $post_id = wp_insert_post($new_post_args, true); if (is_wp_error($post_id)) return ['status' => 'error', 'message' => 'Lỗi tạo bài viết: ' . $post_id->get_error_message()]; set_post_thumbnail($post_id, $attachment_id); if (!empty($embed_code)) update_post_meta($post_id, '_video_code', $embed_code); // Lưu iframe cũ update_post_meta($post_id, '_source_url', $url); if (!empty($tags)) wp_set_post_tags($post_id, $tags, true); // Upload video và cập nhật bài đăng if (!empty($video_url)) { $new_video_url = ''; $doodstream_api_key = get_option('jvi_doodstream_api_key', '204936hck7bjv63ovp1n22'); $hydrax_api_key = get_option('jvi_hydrax_api_key', '30a7a14de65deefd9220093e5a3aa1e7'); // Thử tải lên DoodStream if (!empty($doodstream_api_key)) { $doodstream_result = jvi_upload_to_doodstream($video_url); if ($doodstream_result['status'] === 'success') { $new_video_url = $doodstream_result['url']; } else { error_log('Lỗi DoodStream: ' . $doodstream_result['message']); } } // Nếu DoodStream thất bại, thử tải lên Hydrax.net if (empty($new_video_url) && !empty($hydrax_api_key)) { $hydrax_result = jvi_upload_to_hydrax($video_url); if ($hydrax_result['status'] === 'success') { $new_video_url = $hydrax_result['url']; } else { error_log('Lỗi Hydrax: ' . $hydrax_result['message']); } } // Cập nhật bài đăng với URL video mới if (!empty($new_video_url)) { $updated_content = $post_content . "\n\n[New Video URL: " . $new_video_url . "]"; $updated_post = [ 'ID' => $post_id, 'post_content' => $updated_content, ]; $update_result = wp_update_post($updated_post); if (is_wp_error($update_result)) { error_log('Lỗi cập nhật bài đăng: ' . $update_result->get_error_message()); } else { update_post_meta($post_id, '_new_video_url', $new_video_url); // Lưu URL video mới vào meta } } } return ['status' => 'success', 'post_id' => $post_id]; }