61saat rss haber çekme

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

61saat rss haber çekme

Mesaj gönderen muratca61 »

otomatik sayfa yenilemede çeker

Kod:Tümünü seç

<?php
/*
Plugin Name: RSS Haber Scraper - 61Saat Trabzon
Plugin URI: https://example.com
Description: 61Saat Trabzon haberlerini RSS ile çeker ve WordPress’e otomatik ekler.
Version: 1.1
Author: Örnek Geliştirici
Author URI: https://example.com
License: GPL2
*/

if (!defined('ABSPATH')) {
    exit;
}

class RSS_Haber_Scraper {
    private $rss_url = 'https://www.61saat.com/rss/trabzon';

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

    public function add_admin_page() {
        add_menu_page(
            'RSS Haber Scraper',
            'RSS Haber Scraper',
            'manage_options',
            'rss-haber-scraper',
            array($this, 'admin_page_content'),
            'dashicons-rss',
            20
        );
    }

    public function admin_page_content() {
        ?>
        <div class="wrap">
            <h1>RSS Haber Scraper</h1>
            <p>RSS üzerinden haberleri manuel olarak çekmek için aşağıdaki butona tıklayın.</p>
            <button id="fetch-rss-news" class="button button-primary">Haberleri Çek</button>
            <div id="rss-news-log" style="margin-top: 20px; padding: 10px; background: #f9f9f9; border: 1px solid #ddd; max-height: 300px; overflow-y: auto;"></div>
        </div>
        <script>
        document.getElementById("fetch-rss-news").addEventListener("click", function() {
            let log = document.getElementById("rss-news-log");
            log.innerHTML += "<p>Haberler çekiliyor...</p>";

            fetch(ajaxurl, {
                method: "POST",
                headers: { "Content-Type": "application/x-www-form-urlencoded" },
                body: "action=rss_scrape"
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    log.innerHTML += "<p style='color: green;'>" + data.message + "</p>";
                } else {
                    log.innerHTML += "<p style='color: red;'>" + data.message + "</p>";
                }
            })
            .catch(error => {
                log.innerHTML += "<p style='color: red;'>Bağlantı hatası!</p>";
            });
        });
        </script>
        <?php
    }

    public function handle_scrape_request() {
        $rss_feed = simplexml_load_file($this->rss_url);
        if (!$rss_feed) {
            wp_send_json_error(array('message' => 'RSS bağlantısı başarısız.'));
        }

        $category_id = get_cat_ID('Trabzon');
        if ($category_id == 0) {
            $category_id = wp_create_category('Trabzon');
        }

        foreach ($rss_feed->channel->item as $item) {
            $title = (string) $item->title;
            $link = (string) $item->link;
            $description = (string) strip_tags($item->description);
            $content = (string) strip_tags($item->children('content', true)->encoded);
            $category = (string) $item->category;
            $image_url = (string) $item->enclosure['url'];

            if (post_exists($title)) continue;

            $tags = [];
            if (!empty($category)) $tags[] = strtolower($category);

            $extra_tags = $this->extract_tags_from_content($description . ' ' . $content);
            $tags = array_merge($tags, $extra_tags);
            $tags = array_unique($tags);
            $tags = array_slice($tags, 0, 5);

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

            $post_content = '<p>' . esc_html($description) . '</p><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',
                'post_category' => array($category_id)
            ));

            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ı.'));
    }

    private function extract_tags_from_content($content) {
        $words = explode(' ', $content);
        $tags = [];

        foreach ($words as $word) {
            $word = trim($word, ".,!?()[]{}\"'");

            if (preg_match('/^[A-ZĞÜŞİÖÇ]+$/u', $word) && strlen($word) > 2) {
                $tags[] = strtolower($word);
            } 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);
    }
}

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

Re: 61saat rss haber çekme

Mesaj gönderen muratca61 »

trabzonspor eklendi

Kod:Tümünü seç

<?php
/*
Plugin Name: RSS Haber Scraper - 61Saat Trabzon & Trabzonspor
Description: 61Saat Trabzon ve Trabzonspor haberlerini RSS ile çeker ve WordPress’e ekler.
Version: 1.5
Author: Örnek Geliştirici
License: GPL2
*/

if (!defined('ABSPATH')) {
    exit;
}

class RSS_Haber_Scraper {
    private $rss_sources = [
        'https://www.61saat.com/rss/trabzon' => 'Trabzon',
        'https://www.61saat.com/rss/trabzonspor' => 'Trabzonspor'
    ];

    public function __construct() {
        add_action('admin_menu', array($this, 'add_admin_page'));
        add_action('wp_ajax_rss_scrape', array($this, 'handle_scrape_request'));
        add_action('init', array($this, 'fetch_single_news_on_page_load'));
    }

    // Admin panelinde haber çekme sayfası
    public function add_admin_page() {
        add_menu_page(
            'RSS Haber Scraper',
            'RSS Haber Scraper',
            'manage_options',
            'rss-haber-scraper',
            array($this, 'admin_page_content'),
            'dashicons-rss',
            20
        );
    }

    public function admin_page_content() {
        ?>
        <div class="wrap">
            <h1>RSS Haber Scraper</h1>
            <p>Manuel olarak haber çekmek için aşağıdaki butona tıklayın.</p>
            <button id="fetch-rss-news" class="button button-primary">Haberleri Çek</button>
            <div id="rss-news-log" style="margin-top: 20px; padding: 10px; background: #f9f9f9; border: 1px solid #ddd; max-height: 300px; overflow-y: auto;"></div>
        </div>
        <script>
        document.getElementById("fetch-rss-news").addEventListener("click", function() {
            let log = document.getElementById("rss-news-log");
            log.innerHTML += "<p>Haberler çekiliyor...</p>";

            fetch(ajaxurl, {
                method: "POST",
                headers: { "Content-Type": "application/x-www-form-urlencoded" },
                body: "action=rss_scrape"
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    log.innerHTML += "<p style='color: green;'>" + data.message + "</p>";
                } else {
                    log.innerHTML += "<p style='color: red;'>" + data.message + "</p>";
                }
            })
            .catch(error => {
                log.innerHTML += "<p style='color: red;'>Bağlantı hatası!</p>";
            });
        });
        </script>
        <?php
    }

    public function fetch_single_news_on_page_load() {
        if (!is_admin() && !is_feed()) { 
            $this->fetch_one_rss_news();
        }
    }

    public function handle_scrape_request() {
        $this->fetch_one_rss_news();
        wp_send_json_success(array('message' => 'Bir haber çekildi.'));
    }

    private function fetch_one_rss_news() {
        require_once(ABSPATH . 'wp-admin/includes/post.php'); // 🛠️ post_exists() için gerekli!

        static $last_rss_index = 0;

        $rss_keys = array_keys($this->rss_sources);
        $rss_url = $rss_keys[$last_rss_index];
        $category_name = $this->rss_sources[$rss_url];

        $last_rss_index = ($last_rss_index + 1) % count($rss_keys);

        // RSS verisini kontrol et
        $rss_feed = @simplexml_load_file($rss_url);
        if (!$rss_feed) {
            error_log('RSS yüklenemedi: ' . $rss_url);
            return;
        }

        $category_id = get_cat_ID($category_name);
        if ($category_id == 0) {
            $category_id = wp_create_category($category_name);
        }

        foreach ($rss_feed->channel->item as $item) {
            $title = (string) $item->title;
            $link = (string) $item->link;
            $description = (string) strip_tags($item->description);
            $content = (string) strip_tags($item->children('content', true)->encoded);
            $image_url = isset($item->enclosure['url']) ? (string) $item->enclosure['url'] : '';

            if (post_exists($title)) { // 🔄 Önceden eklenen haberleri atla!
                continue;
            }

            $tags = $this->extract_tags_from_content($description . ' ' . $content);

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

            $post_content = '<p>' . esc_html($description) . '</p><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',
                'post_category' => array($category_id)
            ));

            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);
                }

                return;
            }
        }
    }

    private function extract_tags_from_content($content) {
        $words = explode(' ', $content);
        $tags = [];

        foreach ($words as $word) {
            $word = trim($word, ".,!?()[]{}\"'");

            if (preg_match('/^[A-ZĞÜŞİÖÇ]+$/u', $word) && strlen($word) > 2) {
                $tags[] = strtolower($word);
            } elseif (preg_match('/^[A-ZĞÜŞİÖÇ][a-zğüşıöç]+$/u', $word)) {
                $tags[] = strtolower($word);
            }
        }

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

new RSS_Haber_Scraper();
Cevapla