WordPress TorrentMac 插件:自动抓取 TorrentMac 网站并发布文章
prefix . 'torrent_links';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
post_id mediumint(9) NOT NULL,
torrent_link varchar(255) NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// 插件停用时执行的函数
function torrent_mac_deactivate() {
// 删除自定义数据库表
global $wpdb;
$table_name = $wpdb->prefix . 'torrent_links';
$sql = "DROP TABLE IF EXISTS $table_name;";
$wpdb->query($sql);
}
// 添加自定义的 Meta Box
add_action('add_meta_boxes', 'torrent_mac_add_meta_box');
function torrent_mac_add_meta_box() {
// 添加自定义 Meta Box 到文章编辑页面
add_meta_box('torrent_mac_meta_box', 'Torrent Link', 'torrent_mac_meta_box_callback', 'post', 'normal', 'high');
}
// 自定义 Meta Box 的回调函数
function torrent_mac_meta_box_callback($post) {
// 获取文章对应的 torrent 链接
global $wpdb;
$table_name = $wpdb->prefix . 'torrent_links';
$results = $wpdb->get_results("SELECT torrent_link FROM $table_name WHERE post_id = $post->ID");
if (count($results) > 0) {
$torrent_link = $results[0]->torrent_link;
} else {
$torrent_link = '';
}
// 显示 Meta Box 的 HTML 表单
echo '';
}
// 保存文章时执行的钩子函数
add_action('save_post', 'torrent_mac_save_post');
function torrent_mac_save_post($post_id) {
// 保存文章时更新对应的 torrent 链接
if (isset($_POST['torrent_link'])) {
global $wpdb;
$table_name = $wpdb->prefix . 'torrent_links';
$torrent_link = $_POST['torrent_link'];
$results = $wpdb->get_results("SELECT * FROM $table_name WHERE post_id = $post_id");
if (count($results) > 0) {
$wpdb->update($table_name, array('torrent_link' => $torrent_link), array('post_id' => $post_id));
} else {
$wpdb->insert($table_name, array('post_id' => $post_id, 'torrent_link' => $torrent_link));
}
}
}
// 添加 WordPress Cron Job,每小时执行一次
add_action('torrent_mac_cron_job', 'torrent_mac_cron_job_callback');
function torrent_mac_cron_job_callback() {
// 获取最新的文章链接
$response = wp_remote_request('https://www.torrentmac.net/category/applications/');
if (is_wp_error($response)) {
// 请求错误处理
error_log('Error scraping TorrentMac: ' . $response->get_error_message());
return;
}
$body = wp_remote_retrieve_body($response);
$dom = new DOMDocument();
@$dom->loadHTML($body);
$xpath = new DOMXPath($dom);
$articles = $xpath->query('//article');
$latest_article = $articles->item(0);
$latest_article_link = $latest_article->getElementsByTagName('a')->item(0)->getAttribute('href');
// 检查是否已经发布过最新的文章
$existing_post_id = url_to_postid($latest_article_link);
if ($existing_post_id) {
// 如果已经发布过,直接返回
return;
}
// 获取最新的文章标题、内容和 torrent 文件链接
$latest_article_title = $latest_article->getElementsByTagName('h2')->item(0)->textContent;
$latest_article_content = $latest_article->getElementsByTagName('div')->item(0)->textContent;
$latest_article_response = wp_remote_request($latest_article_link, array(
'headers' => array(
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Referer' => 'https://www.torrentmac.net/',
),
));
if (is_wp_error($latest_article_response)) {
// 请求错误处理
error_log('Error scraping TorrentMac: ' . $latest_article_response->get_error_message());
return;
}
$latest_article_body = wp_remote_retrieve_body($latest_article_response);
@$latest_article_dom->loadHTML($latest_article_body);
$latest_article_dom = new DOMDocument();
$latest_article_dom->loadHTML($latest_article_body);
$latest_article_xpath = new DOMXPath($latest_article_dom);
$latest_article_torrent_link = $latest_article_xpath->query('//a[contains(@href, ".torrent")]')->item(0)->getAttribute('href');
// 下载 torrent 文件
$torrent_file = wp_remote_get($latest_article_torrent_link);
if (is_wp_error($torrent_file)) {
// 请求错误处理
error_log('Error downloading torrent file: ' . $torrent_file->get_error_message());
return;
}
// 将 torrent 文件保存到本地
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['path'] . '/' . basename($latest_article_torrent_link);
file_put_contents($file_path, $torrent_file['body']);
// 创建 WordPress 文章
$post_id = wp_insert_post(array(
'post_title' => $latest_article_title,
'post_content' => $latest_article_content,
'post_status' => 'publish',
));
// 添加 torrent 链接到自定义段
update_post_meta($post_id, 'torrent_link', $file_path);
}
// 注册 WordPress Cron Job
add_action('wp', 'torrent_mac_register_cron_job');
function torrent_mac_register_cron_job() {
if (!wp_next_scheduled('torrent_mac_cron_job')) {
wp_schedule_event(time(), 'hourly', 'torrent_mac_cron_job');
}
}
?>
原文地址: https://www.cveoy.top/t/topic/lQw5 著作权归作者所有。请勿转载和采集!