好的,以下是一个基本的Typecho采集插件的示例,它可以通过将指定网站的内容导入到Typecho中来创建新的文章。请注意,这只是一个初步的框架,您可能需要根据您的具体需求和网站来进行修改和定制。
<?php
/**
* Name: Typecho Site Collect Plugin
* Description: Collect articles from specified websites and import them into Typecho.
* Version: 1.0
* Author: ChatGPT
* Author URL: https://example.com/
* Plugin URL: https://example.com/plugin
*/
class Typecho_Collect_Plugin implements Typecho_Plugin_Interface
{
// 定义默认的采集间隔时间
const INTERVAL = 86400;
// 需要采集的目标站点列表
private static $targetSites = array(
'example1.com',
'example2.com'
);
private static $db;
private static $widgets = array();
/**
* 激活插件,建立数据库表
*/
public static function activate()
{
self::$db = Typecho_Db::get();
$prefix = self::$db->getPrefix();
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `{$prefix}collect` (
`cid` int(11) unsigned NOT NULL DEFAULT '0',
`site` varchar(128) NOT NULL,
`url` varchar(255) NOT NULL,
`status` varchar(16) DEFAULT NULL,
`datetime` datetime DEFAULT NULL,
PRIMARY KEY (`cid`,`site`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SQL;
self::$db->query($sql);
}
/**
* 禁用插件,删除数据库表
*/
public static function deactivate()
{
self::$db = Typecho_Db::get();
$prefix = self::$db->getPrefix();
$sql = <<<SQL
DROP TABLE IF EXISTS `{$prefix}collect`;
SQL;
self::$db->query($sql);
}
/**
* 插件配置面板
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$interval = new Typecho_Widget_Helper_Form_Element_Text('interval', NULL, self::INTERVAL, '采集间隔时间(单位:秒)');
$form->addInput($interval);
$sites = new Typecho_Widget_Helper_Form_Element_Textarea('sites', NULL, implode(PHP_EOL, self::$targetSites), '目标站点列表(每行一个)');
$form->addInput($sites);
}
/**
* 采集定时任务
*/
public static function task()
{
$interval = Typecho_Widget::widget('Widget_Options')->plugin('Typecho_Collect')->interval;
if (!$interval) {
$interval = self::INTERVAL;
}
$lastTime = Typecho_Widget::widget('Widget_Options')->plugin('Typecho_Collect')->lastTime;
if (!$lastTime) {
$lastTime = time() - $interval;
}
if (time() - $lastTime > $interval) {
Typecho_Widget::widget('Widget_Options')->plugin('Typecho_Collect')->lastTime = time();
self::$targetSites = array_map('trim', explode(PHP_EOL, Typecho_Widget::widget('Widget_Options')->plugin('Typecho_Collect')->sites));
foreach (self::$targetSites as $site) {
self::collect($site);
}
}
}
/**
* 采集指定站点的文章
* @param $site
*/
public static function collect($site)
{
$url = 'https://' . $site . '/api/collect.php';
// TODO: 发送 POST 请求并将结果解析为数组
foreach ($data as $datum) {
if (self::import($datum)) {
echo("{$datum['url']} import success!\n");
} else {
echo("{$datum['url']} import failure!\n");
}
}
}
/**
* 导入一篇文章到 Typecho
* @param $data
*/
public static function import($data)
{
$cid = self::$db->fetchRow(self::$db->select('cid')->from('table:collect')->where('cid = ? AND site = ?', $data['cid'], $data['site']));
if ($cid) {
return FALSE;
}
$content = self::getArticleContent($data['url']);
if (!$content) {
return FALSE;
}
$author = self::$widgets['Users']->getUser(1);
if (!$author || $author['activated'] == 0) {
return FALSE;
}
$title = htmlspecialchars($data['title']);
$slug = self::generateSlug($title);
$date = date('Y-m-d H:i:s', strtotime($data['datetime']));
$post = array(
'title' => $title,
'slug' => $slug,
'text' => $content,
'authorId' => 1,
'created' => $date,
'modified' => $date,
'allowComment' => '1'
);
$cid = Typecho_Widget::widget('Widget_Contents_Post_Create')->insert($post);
if (!$cid) {
return FALSE;
}
$row = array(
'cid' => $cid,
'site' => $data['site'],
'url' => $data['url'],
'status' => 'success',
'datetime' => $date
);
self::$db->query(self::$db->insert('table:collect')->rows($row));
return TRUE;
}
/**
* 生成 Slug
* @param $text
* @return string