hürriyet rss çekme kodu

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

hürriyet rss çekme kodu

Mesaj gönderen muratca61 »

çalışan koda

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();
muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: hürriyet rss çekme kodu

Mesaj gönderen muratca61 »

kaynak eklendi

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')) : '';

            // Kaynak linki: ilgili <a> etiketinin href değerini al, base URL ile birleştir.
            $source_link_node = $xpath->query(".//a[contains(@class, 'category__list__item--cover')]", $news_node)->item(0);
            $source_url = '';
            if ($source_link_node) {
                $relative_url = trim($source_link_node->getAttribute('href'));
                $source_url = 'https://www.hurriyet.com.tr' . $relative_url;
            }

            $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;

            // Haber içeriğini oluştururken, içerik sonunda kaynak linkini de ekliyoruz.
            $post_content = '<p>' . esc_html($content) . '</p>';
            if ( $source_url ) {
                $post_content .= '<p>Kaynak: <a target="_blank" href="' . esc_url($source_url) . '" rel="noopener noreferrer">Hürriyet</a></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