son dakika içerikleri çekme eklentisi hürriyet

Genel Forum
Cevapla
muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

son dakika içerikleri çekme eklentisi hürriyet

Mesaj gönderen muratca61 »

hurriyet-haber-scraper.php

Kod:Tümünü seç

<?php
/*
Plugin Name: Hurriyet Haber Scraper (Güncellenmiş Yapı)
Description: Hurriyet son dakika haberlerini belirlenen class yapısına uygun olarak çeker ve WordPress yazısı olarak kaydeder.
Version: 2.0
Author: Örnek Geliştirici
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Direkt erişimi engelle
}

class Hurriyet_Haber_Scraper {

    public function __construct() {
        add_action('admin_menu', array($this, 'add_plugin_page'));
        add_action('admin_post_hurriyet_scrape', array($this, 'handle_scrape_request'));
    }

    public function add_plugin_page() {
        add_menu_page(
            'Hurriyet Haber Scraper', 
            'Hurriyet Haber', 
            'manage_options', 
            'hurriyet-haber-scraper', 
            array($this, 'create_admin_page'), 
            'dashicons-admin-site-alt3'
        );
    }

    public function create_admin_page() {
        ?>
        <div class="wrap">
            <h1>Hurriyet Haber Scraper</h1>
            <?php if ( isset($_GET['message']) && $_GET['message'] === 'success' ) : ?>
                <div class="notice notice-success is-dismissible">
                    <p>Yeni haber yazısı oluşturuldu.</p>
                </div>
            <?php endif; ?>
            <form method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
                <input type="hidden" name="action" value="hurriyet_scrape">
                <?php submit_button('Haberleri Çek'); ?>
            </form>
        </div>
        <?php
    }

    public function handle_scrape_request() {
        $url = 'https://www.hurriyet.com.tr/son-dakika-haberleri/';
        
        $response = wp_remote_get($url);
        if ( is_wp_error($response) ) {
            wp_die('Hata: ' . $response->get_error_message());
        }

        $html = wp_remote_retrieve_body($response);
        if ( empty($html) ) {
            wp_die('Haberler çekilemedi.');
        }

        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        libxml_clear_errors();

        $xpath = new DOMXPath($dom);

        // "category__list__item son-dakika-haberleri" class'ına sahip haberleri bul
        $news_nodes = $xpath->query("//div[contains(@class, 'category__list__item') and contains(@class, 'son-dakika-haberleri')]");

        if ( $news_nodes->length === 0 ) {
            error_log('Hurriyet Haber Scraper: Haber bulunamadı.');
            wp_die('Haber bulunamadı.');
        }

        // İlk haberi al
        $news_node = $news_nodes->item(0);

        // ** Başlık Bilgisi **
        $title = '';
        $title_node = $xpath->query(".//h2/a", $news_node)->item(0);
        if ( $title_node ) {
            $title = trim($title_node->getAttribute('title'));
        }

        // ** İçerik Bilgisi **
        $content = '';
        $content_node = $xpath->query(".//p/a", $news_node)->item(0);
        if ( $content_node ) {
            $content = trim($content_node->nodeValue);
        }

        // ** Görsel URL'si **
        $image_url = '';
        $image_node = $xpath->query(".//a[contains(@class, 'category__list__item--cover')]/img", $news_node)->item(0);
        if ( $image_node ) {
            $image_url = trim($image_node->getAttribute('data-src'));
        }

        // Eğer "data-src" yoksa, "src" attribute'undan al
        if ( empty($image_url) && $image_node ) {
            $image_url = trim($image_node->getAttribute('src'));
        }

        // Eğer başlık veya içerik boşsa hata verelim.
        if ( empty($title) || empty($content) ) {
            error_log('Hurriyet Haber Scraper: Başlık veya içerik eksik. Başlık: ' . $title . ' - İçerik: ' . $content);
            wp_die('Haber bilgileri eksik.');
        }

        // Yazı içeriğini oluştur
        $post_content = '<p>' . esc_html($content) . '</p>';

        // WordPress'e yeni yazıyı ekleyelim
        $new_post = array(
            'post_title'   => wp_strip_all_tags($title),
            'post_content' => $post_content,
            'post_status'  => 'publish',
            'post_author'  => get_current_user_id(),
            'post_type'    => 'post'
        );

        $post_id = wp_insert_post($new_post);

        if ( is_wp_error($post_id) ) {
            wp_die('Yazı oluşturulamadı.');
        }

        // ** Görseli indirip öne çıkarılmış görsel yap **
        if ( !empty($image_url) ) {
            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, $post_id, $title, 'id');

            if ( ! is_wp_error($attachment_id) ) {
                set_post_thumbnail($post_id, $attachment_id);
            } else {
                error_log('Hurriyet Haber Scraper: Resim eklenemedi. Hata: ' . $attachment_id->get_error_message());
            }
        }

        // Admin paneline geri yönlendir
        wp_redirect(admin_url('admin.php?page=hurriyet-haber-scraper&message=success'));
        exit;
    }
}

new Hurriyet_Haber_Scraper();
muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: son dakika içerikleri çekme eklentisi hürriyet

Mesaj gönderen muratca61 »

etiket eklendi

Kod:Tümünü seç

<?php
/*
Plugin Name: Hurriyet Haber Scraper (Güncellenmiş Yapı - Etiket Desteği)
Description: Hurriyet son dakika haberlerini belirlenen class yapısına uygun olarak çeker, etiketleri ekler ve WordPress yazısı olarak kaydeder.
Version: 2.1
Author: Örnek Geliştirici
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Direkt erişimi engelle
}

class Hurriyet_Haber_Scraper {

    public function __construct() {
        add_action('admin_menu', array($this, 'add_plugin_page'));
        add_action('admin_post_hurriyet_scrape', array($this, 'handle_scrape_request'));
    }

    public function add_plugin_page() {
        add_menu_page(
            'Hurriyet Haber Scraper', 
            'Hurriyet Haber', 
            'manage_options', 
            'hurriyet-haber-scraper', 
            array($this, 'create_admin_page'), 
            'dashicons-admin-site-alt3'
        );
    }

    public function create_admin_page() {
        ?>
        <div class="wrap">
            <h1>Hurriyet Haber Scraper</h1>
            <?php if ( isset($_GET['message']) && $_GET['message'] === 'success' ) : ?>
                <div class="notice notice-success is-dismissible">
                    <p>Yeni haber yazısı oluşturuldu.</p>
                </div>
            <?php endif; ?>
            <form method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
                <input type="hidden" name="action" value="hurriyet_scrape">
                <?php submit_button('Haberleri Çek'); ?>
            </form>
        </div>
        <?php
    }

    public function handle_scrape_request() {
        $url = 'https://www.hurriyet.com.tr/son-dakika-haberleri/';
        
        $response = wp_remote_get($url);
        if ( is_wp_error($response) ) {
            wp_die('Hata: ' . $response->get_error_message());
        }

        $html = wp_remote_retrieve_body($response);
        if ( empty($html) ) {
            wp_die('Haberler çekilemedi.');
        }

        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        libxml_clear_errors();

        $xpath = new DOMXPath($dom);

        // "category__list__item son-dakika-haberleri" class'ına sahip haberleri bul
        $news_nodes = $xpath->query("//div[contains(@class, 'category__list__item') and contains(@class, 'son-dakika-haberleri')]");

        if ( $news_nodes->length === 0 ) {
            error_log('Hurriyet Haber Scraper: Haber bulunamadı.');
            wp_die('Haber bulunamadı.');
        }

        // İlk haberi al
        $news_node = $news_nodes->item(0);

        // ** Başlık Bilgisi **
        $title = '';
        $title_node = $xpath->query(".//h2/a", $news_node)->item(0);
        if ( $title_node ) {
            $title = trim($title_node->getAttribute('title'));
        }

        // ** İçerik Bilgisi **
        $content = '';
        $content_node = $xpath->query(".//p/a", $news_node)->item(0);
        if ( $content_node ) {
            $content = trim($content_node->nodeValue);
        }

        // ** Görsel URL'si **
        $image_url = '';
        $image_node = $xpath->query(".//a[contains(@class, 'category__list__item--cover')]/img", $news_node)->item(0);
        if ( $image_node ) {
            $image_url = trim($image_node->getAttribute('data-src'));
        }

        // Eğer "data-src" yoksa, "src" attribute'undan al
        if ( empty($image_url) && $image_node ) {
            $image_url = trim($image_node->getAttribute('src'));
        }

        // ** Etiketleri Çekme **
        $tags = [];
        $tag_nodes = $xpath->query(".//a[contains(@class, 'category__list__item--tag')]", $news_node);
        
        foreach ($tag_nodes as $tag_node) {
            $tag = trim($tag_node->nodeValue);
            $tag = strtolower(str_replace('#', '', $tag)); // Küçük harfe çevir ve # işaretini kaldır
            if (!empty($tag)) {
                $tags[] = $tag;
            }
        }

        // Eğer başlık veya içerik boşsa hata verelim.
        if ( empty($title) || empty($content) ) {
            error_log('Hurriyet Haber Scraper: Başlık veya içerik eksik. Başlık: ' . $title . ' - İçerik: ' . $content);
            wp_die('Haber bilgileri eksik.');
        }

        // Yazı içeriğini oluştur
        $post_content = '<p>' . esc_html($content) . '</p>';

        // WordPress'e yeni yazıyı ekleyelim
        $new_post = array(
            'post_title'   => wp_strip_all_tags($title),
            'post_content' => $post_content,
            'post_status'  => 'publish',
            'post_author'  => get_current_user_id(),
            'post_type'    => 'post'
        );

        $post_id = wp_insert_post($new_post);

        if ( is_wp_error($post_id) ) {
            wp_die('Yazı oluşturulamadı.');
        }

        // ** Etiketleri Yazıya Ekleme **
        if (!empty($tags)) {
            wp_set_post_tags($post_id, $tags);
        }

        // ** Görseli indirip öne çıkarılmış görsel yap **
        if ( !empty($image_url) ) {
            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, $post_id, $title, 'id');

            if ( ! is_wp_error($attachment_id) ) {
                set_post_thumbnail($post_id, $attachment_id);
            } else {
                error_log('Hurriyet Haber Scraper: Resim eklenemedi. Hata: ' . $attachment_id->get_error_message());
            }
        }

        // Admin paneline geri yönlendir
        wp_redirect(admin_url('admin.php?page=hurriyet-haber-scraper&message=success'));
        exit;
    }
}

new Hurriyet_Haber_Scraper();
muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: son dakika içerikleri çekme eklentisi hürriyet

Mesaj gönderen muratca61 »

başlık mevcutsa diğerine geçecek

Kod:Tümünü seç

<?php
/*
Plugin Name: Hurriyet Haber Scraper (Yinelenen Başlıkları Atlayan Versiyon)
Description: Hurriyet son dakika haberlerini belirlenen class yapısına uygun olarak çeker, başlık daha önce eklendiyse sıradaki habere geçer, etiketleri ekler ve WordPress yazısı olarak kaydeder.
Version: 2.2
Author: Örnek Geliştirici
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Direkt erişimi engelle
}

class Hurriyet_Haber_Scraper {

    public function __construct() {
        add_action('admin_menu', array($this, 'add_plugin_page'));
        add_action('admin_post_hurriyet_scrape', array($this, 'handle_scrape_request'));
    }

    public function add_plugin_page() {
        add_menu_page(
            'Hurriyet Haber Scraper', 
            'Hurriyet Haber', 
            'manage_options', 
            'hurriyet-haber-scraper', 
            array($this, 'create_admin_page'), 
            'dashicons-admin-site-alt3'
        );
    }

    public function create_admin_page() {
        ?>
        <div class="wrap">
            <h1>Hurriyet Haber Scraper</h1>
            <?php if ( isset($_GET['message']) && $_GET['message'] === 'success' ) : ?>
                <div class="notice notice-success is-dismissible">
                    <p>Yeni haber yazısı oluşturuldu.</p>
                </div>
            <?php endif; ?>
            <form method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
                <input type="hidden" name="action" value="hurriyet_scrape">
                <?php submit_button('Haberleri Çek'); ?>
            </form>
        </div>
        <?php
    }

    public function handle_scrape_request() {
        $url = 'https://www.hurriyet.com.tr/son-dakika-haberleri/';
        
        $response = wp_remote_get($url);
        if ( is_wp_error($response) ) {
            wp_die('Hata: ' . $response->get_error_message());
        }

        $html = wp_remote_retrieve_body($response);
        if ( empty($html) ) {
            wp_die('Haberler çekilemedi.');
        }

        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        libxml_clear_errors();

        $xpath = new DOMXPath($dom);

        // "category__list__item son-dakika-haberleri" class'ına sahip tüm haberleri al
        $news_nodes = $xpath->query("//div[contains(@class, 'category__list__item') and contains(@class, 'son-dakika-haberleri')]");

        if ( $news_nodes->length === 0 ) {
            wp_die('Haber bulunamadı.');
        }

        foreach ($news_nodes as $news_node) {
            // ** Başlık Bilgisi **
            $title = '';
            $title_node = $xpath->query(".//h2/a", $news_node)->item(0);
            if ( $title_node ) {
                $title = trim($title_node->getAttribute('title'));
            }

            // Eğer bu başlık zaten sistemde varsa, sonraki habere geç
            if ( post_exists($title) ) {
                continue;
            }

            // ** İçerik Bilgisi **
            $content = '';
            $content_node = $xpath->query(".//p/a", $news_node)->item(0);
            if ( $content_node ) {
                $content = trim($content_node->nodeValue);
            }

            // ** Görsel URL'si **
            $image_url = '';
            $image_node = $xpath->query(".//a[contains(@class, 'category__list__item--cover')]/img", $news_node)->item(0);
            if ( $image_node ) {
                $image_url = trim($image_node->getAttribute('data-src'));
            }

            if ( empty($image_url) && $image_node ) {
                $image_url = trim($image_node->getAttribute('src'));
            }

            // ** Etiketleri Çekme **
            $tags = [];
            $tag_nodes = $xpath->query(".//a[contains(@class, 'category__list__item--tag')]", $news_node);
            
            foreach ($tag_nodes as $tag_node) {
                $tag = trim($tag_node->nodeValue);
                $tag = strtolower(str_replace('#', '', $tag)); // Küçük harfe çevir ve # işaretini kaldır
                if (!empty($tag)) {
                    $tags[] = $tag;
                }
            }

            // Eğer başlık veya içerik boşsa sonraki habere geç
            if ( empty($title) || empty($content) ) {
                continue;
            }

            // Yazı içeriğini oluştur
            $post_content = '<p>' . esc_html($content) . '</p>';

            // WordPress'e yeni yazıyı ekleyelim
            $new_post = array(
                'post_title'   => wp_strip_all_tags($title),
                'post_content' => $post_content,
                'post_status'  => 'publish',
                'post_author'  => get_current_user_id(),
                'post_type'    => 'post'
            );

            $post_id = wp_insert_post($new_post);

            if ( is_wp_error($post_id) ) {
                continue;
            }

            // ** Etiketleri Yazıya Ekleme **
            if (!empty($tags)) {
                wp_set_post_tags($post_id, $tags);
            }

            // ** Görseli indirip öne çıkarılmış görsel yap **
            if ( !empty($image_url) ) {
                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, $post_id, $title, 'id');

                if ( ! is_wp_error($attachment_id) ) {
                    set_post_thumbnail($post_id, $attachment_id);
                }
            }

            // Eğer yeni bir haber bulundu ve eklendiyse döngüyü sonlandır
            wp_redirect(admin_url('admin.php?page=hurriyet-haber-scraper&message=success'));
            exit;
        }

        // Eğer hiç yeni haber bulunamazsa hata mesajı ver
        wp_die('Yeni haber bulunamadı.');
    }
}

new Hurriyet_Haber_Scraper();
muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: son dakika içerikleri çekme eklentisi hürriyet

Mesaj gönderen muratca61 »

sayfada gezindikçe otomatik çekecek. yeni dosya

Kod:Tümünü seç

hurriyet-scraper.js

Kod:Tümünü seç

jQuery(document).ready(function($) {
    $.ajax({
        url: hurriyetScraperAjax.ajax_url,
        type: 'POST',
        data: { action: 'hurriyet_scrape' },
        success: function(response) {
            if (response.success) {
                console.log(response.data.message);
            } else {
                console.log('Haber bulunamadı: ' + response.data.message);
            }
        },
        error: function() {
            console.log('Bağlantı hatası.');
        }
    });
});

Kod:Tümünü seç

<?php
/*
Plugin Name: Hurriyet Haber Scraper (Otomatik)
Plugin URI: https://example.com
Description: Hurriyet son dakika haberlerini her sayfa görüntülendiğinde kontrol eder ve ekler.
Version: 3.0
Author: Örnek Geliştirici
Author URI: https://example.com
License: GPL2
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Direkt erişimi engelle
}

class Hurriyet_Haber_AutoScraper {

    public function __construct() {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
        add_action('wp_ajax_hurriyet_scrape', array($this, 'handle_scrape_request'));
        add_action('wp_ajax_nopriv_hurriyet_scrape', array($this, 'handle_scrape_request'));
    }

    // Javascript dosyasını ekleme
    public function enqueue_scripts() {
        wp_enqueue_script('hurriyet-scraper', plugin_dir_url(__FILE__) . 'hurriyet-scraper.js', array('jquery'), null, true);
        wp_localize_script('hurriyet-scraper', 'hurriyetScraperAjax', array('ajax_url' => admin_url('admin-ajax.php')));
    }

    // Haberleri çekme ve ekleme işlemi
    public function handle_scrape_request() {
        $url = 'https://www.hurriyet.com.tr/son-dakika-haberleri/';
        $response = wp_remote_get($url);

        if ( is_wp_error($response) ) {
            wp_send_json_error(array('message' => 'Bağlantı hatası.'));
        }

        $html = wp_remote_retrieve_body($response);
        if ( empty($html) ) {
            wp_send_json_error(array('message' => 'Haberler alınamadı.'));
        }

        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        libxml_clear_errors();
        $xpath = new DOMXPath($dom);

        $news_nodes = $xpath->query("//div[contains(@class, 'category__list__item') and contains(@class, 'son-dakika-haberleri')]");

        foreach ($news_nodes as $news_node) {
            $title_node = $xpath->query(".//h2/a", $news_node)->item(0);
            if (!$title_node) continue;

            $title = trim($title_node->getAttribute('title'));
            if (post_exists($title)) continue;

            $content_node = $xpath->query(".//p/a", $news_node)->item(0);
            $content = $content_node ? trim($content_node->nodeValue) : '';

            $image_node = $xpath->query(".//a[contains(@class, 'category__list__item--cover')]/img", $news_node)->item(0);
            $image_url = $image_node ? trim($image_node->getAttribute('data-src')) : '';

            $tags = [];
            $tag_nodes = $xpath->query(".//a[contains(@class, 'category__list__item--tag')]", $news_node);
            foreach ($tag_nodes as $tag_node) {
                $tag = strtolower(trim(str_replace('#', '', $tag_node->nodeValue)));
                if ($tag) $tags[] = $tag;
            }

            if (empty($title) || empty($content)) continue;

            $post_content = '<p>' . esc_html($content) . '</p>';
            $post_id = wp_insert_post(array(
                'post_title'   => wp_strip_all_tags($title),
                'post_content' => $post_content,
                'post_status'  => 'publish',
                'post_author'  => get_current_user_id(),
                'post_type'    => 'post'
            ));

            if (!is_wp_error($post_id)) {
                if (!empty($tags)) wp_set_post_tags($post_id, $tags);

                if (!empty($image_url)) {
                    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, $post_id, $title, 'id');
                    if (!is_wp_error($attachment_id)) set_post_thumbnail($post_id, $attachment_id);
                }

                wp_send_json_success(array('message' => 'Yeni haber eklendi.'));
            }
        }

        wp_send_json_error(array('message' => 'Yeni haber bulunamadı.'));
    }
}

new Hurriyet_Haber_AutoScraper();

muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: son dakika içerikleri çekme eklentisi hürriyet

Mesaj gönderen muratca61 »

otomatik etiket ekleyecek. uygulama artık otomatik çalışıyor. admin arayüzü yok.

Kod:Tümünü seç

<?php
/*
Plugin Name: Hurriyet Haber Scraper (Otomatik)
Plugin URI: https://example.com
Description: Hurriyet son dakika haberlerini her sayfa görüntülendiğinde kontrol eder ve ekler.
Version: 3.1
Author: Örnek Geliştirici
Author URI: https://example.com
License: GPL2
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Direkt erişimi engelle
}

class Hurriyet_Haber_AutoScraper {

    public function __construct() {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
        add_action('wp_ajax_hurriyet_scrape', array($this, 'handle_scrape_request'));
        add_action('wp_ajax_nopriv_hurriyet_scrape', array($this, 'handle_scrape_request'));
    }

    public function enqueue_scripts() {
        wp_enqueue_script('hurriyet-scraper', plugin_dir_url(__FILE__) . 'hurriyet-scraper.js', array('jquery'), null, true);
        wp_localize_script('hurriyet-scraper', 'hurriyetScraperAjax', array('ajax_url' => admin_url('admin-ajax.php')));
    }

    public function handle_scrape_request() {
        $url = 'https://www.hurriyet.com.tr/son-dakika-haberleri/';
        $response = wp_remote_get($url);

        if ( is_wp_error($response) ) {
            wp_send_json_error(array('message' => 'Bağlantı hatası.'));
        }

        $html = wp_remote_retrieve_body($response);
        if ( empty($html) ) {
            wp_send_json_error(array('message' => 'Haberler alınamadı.'));
        }

        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        libxml_clear_errors();
        $xpath = new DOMXPath($dom);

        $news_nodes = $xpath->query("//div[contains(@class, 'category__list__item') and contains(@class, 'son-dakika-haberleri')]");

        foreach ($news_nodes as $news_node) {
            $title_node = $xpath->query(".//h2/a", $news_node)->item(0);
            if (!$title_node) continue;

            $title = trim($title_node->getAttribute('title'));
            if (post_exists($title)) continue;

            $content_node = $xpath->query(".//p/a", $news_node)->item(0);
            $content = $content_node ? trim($content_node->nodeValue) : '';

            $image_node = $xpath->query(".//a[contains(@class, 'category__list__item--cover')]/img", $news_node)->item(0);
            $image_url = $image_node ? trim($image_node->getAttribute('data-src')) : '';

            $tags = [];
            $tag_nodes = $xpath->query(".//a[contains(@class, 'category__list__item--tag')]", $news_node);
            foreach ($tag_nodes as $tag_node) {
                $tag = strtolower(trim(str_replace('#', '', $tag_node->nodeValue)));
                if ($tag) $tags[] = $tag;
            }

            // Ekstra etiketleri belirleme
            $extra_tags = $this->extract_tags_from_content($content);
            $tags = array_merge($tags, $extra_tags);
            $tags = array_unique($tags);
            $tags = array_slice($tags, 0, 5); // Maksimum 5 etiket al

            if (empty($title) || empty($content)) continue;

            $post_content = '<p>' . esc_html($content) . '</p>';
            $post_id = wp_insert_post(array(
                'post_title'   => wp_strip_all_tags($title),
                'post_content' => $post_content,
                'post_status'  => 'publish',
                'post_author'  => get_current_user_id(),
                'post_type'    => 'post'
            ));

            if (!is_wp_error($post_id)) {
                if (!empty($tags)) wp_set_post_tags($post_id, $tags);

                if (!empty($image_url)) {
                    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, $post_id, $title, 'id');
                    if (!is_wp_error($attachment_id)) set_post_thumbnail($post_id, $attachment_id);
                }

                wp_send_json_success(array('message' => 'Yeni haber eklendi.'));
            }
        }

        wp_send_json_error(array('message' => 'Yeni haber bulunamadı.'));
    }

    // İçerikten büyük harfli kelimeleri alıp küçük harfe çevirerek etiket ekleme fonksiyonu
    private function extract_tags_from_content($content) {
        $words = explode(' ', $content);
        $tags = [];

        foreach ($words as $word) {
            $word = trim($word, ".,!?()[]{}\"'"); // Noktalama işaretlerini temizle

            // TAMAMI BÜYÜK HARFLİ KELİMELERİ BUL
            if (preg_match('/^[A-ZĞÜŞİÖÇ]+$/u', $word) && strlen($word) > 2) {
                $tags[] = strtolower($word);
            }

            // CÜMLE BAŞI HARİÇ BÜYÜK HARFLE BAŞLAYANLARI BUL
            elseif (preg_match('/^[A-ZĞÜŞİÖÇ][a-zğüşıöç]+$/u', $word) && !in_array($word, ['Ve', 'Bir', 'İçin', 'Ancak'])) {
                $tags[] = strtolower($word);
            }
        }

        return array_slice(array_unique($tags), 0, 5); // Maksimum 5 etiket
    }
}

new Hurriyet_Haber_AutoScraper();
Cevapla