Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
208 lines (188 loc) · 7.43 KB
/
Copy pathPlugin.php
File metadata and controls
208 lines (188 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
require "Feed.php";
/**
* Bilibili 动态同步插件。
*
* @package BilibiliEcho
* @author pluvet
* @version 1.0.0
* @link https://www.pluvet.com
*/
class BilibiliEcho_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @throws Typecho_Db_Exception
*/
public static function activate()
{
// Typecho_Plugin::factory('Widget_Archive') -> header = array(__CLASS__, 'header');
return '插件启用成功,别忘了设置一下哟';
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @access public
* @return void
*/
public static function deactivate()
{ }
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$rssHub = new Typecho_Widget_Helper_Form_Element_Text(
'rssHub',
NULL,
"https://rsshub.app",
_t('rssHub链接(默认为主站链接,请不要输入最后的/)')
);
$form->addInput($rssHub);
$uid = new Typecho_Widget_Helper_Form_Element_Text(
'uid',
NULL,
1585955812,
_t('用户id')
);
$form->addInput($uid);
$expireTime = new Typecho_Widget_Helper_Form_Element_Text(
'expireTime',
NULL,
24,
_t(
'缓存时间(单位为小时)',
_t('(建议大于12小时,以减轻服务器负担。如果设置过低可能无效,因为 api 服务器本身也带有缓存。')
)
);
$form->addInput($expireTime);
$count = new Typecho_Widget_Helper_Form_Element_Text(
'count',
NULL,
5,
_t('显示条数')
);
$form->addInput($count);
$ignore = new Typecho_Widget_Helper_Form_Element_Text(
'ignore',
NULL,
'',
_t(
'忽略规则 regex',
_t('如果本设置项不为空,那么如果正则匹配数量大于零,则对应的动态会忽略不显示。忽略规则和选取规则只能填写一个,多选按忽略规则计。')
)
);
$form->addInput($ignore);
$select = new Typecho_Widget_Helper_Form_Element_Text(
'select',
NULL,
'',
_t(
'选取规则 regex',
_t('如果本设置项不为空,那么只有正则匹配数量大于零的的动态会显示。忽略规则和选取规则只能填写一个,多选按忽略规则计。')
)
);
$form->addInput($select);
$share = new Typecho_Widget_Helper_Form_Element_Radio('share', array(
0 => _t('否'),
1 => _t('是')
), 0, _t('显示转发内容'));
$form->addInput($share->addRule('enum', _t('请选择一种'), array(0, 1)));
$disableEmbed = new Typecho_Widget_Helper_Form_Element_Radio('disableEmbed', array(
0 => _t('否'),
1 => _t('是')
), 0, _t('显示视频(iframe,建议不开启)'));
$form->addInput($disableEmbed->addRule('enum', _t('请选择一种'), array(0, 1)));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{ }
public static function renderDynamics()
{
$rssHub = Typecho_Widget::widget('Widget_Options')->plugin('BilibiliEcho')->rssHub;
$uid = Typecho_Widget::widget('Widget_Options')->plugin('BilibiliEcho')->uid;
if (!is_numeric($uid)) echo "错误:uid 必须是数字";
$expireTime = Typecho_Widget::widget('Widget_Options')->plugin('BilibiliEcho')->expireTime;
$count = intval(Typecho_Widget::widget('Widget_Options')->plugin('BilibiliEcho')->count);
$share = intval(Typecho_Widget::widget('Widget_Options')->plugin('BilibiliEcho')->share);
$disableEmbed = intval(Typecho_Widget::widget('Widget_Options')->plugin('BilibiliEcho')->disableEmbed);
$ignore = Typecho_Widget::widget('Widget_Options')->plugin('BilibiliEcho')->ignore;
$select = Typecho_Widget::widget('Widget_Options')->plugin('BilibiliEcho')->select;
if($disableEmbed){
$url = $rssHub . "/bilibili/user/dynamic/" . $uid;
}else{
$url = $rssHub . "/bilibili/user/dynamic/" . $uid ."/disableEmbed";
}
$spaceUrl = "https://space.bilibili.com/" . $uid;
Feed::$cacheDir = __DIR__ . '/tmp';
Feed::$cacheExpire = $expireTime . ' hours';
$rss = Feed::loadRss($url);
$author = str_replace(" 的 bilibili 动态", "", $rss->title);
// echo 'Title: ', $rss->title;
// echo 'Description: ', $rss->description;
// echo 'Link: ', $rss->link;
$counter = 0;
?>
<div class="widget">
<h3 class="widget-title">
<i class="fa-brands fa-bilibili" style="color:#00A0D8;"></i> 观星动态
</h3>
<ul class="list--withIcon">
<?php foreach ($rss->item as $item) :
if ($counter == $count) break;
$content = $item->description;
if (strpos($content, "转发自") > 0 && !$share) {
continue;
}
if (isset($ignore) && ($ignore!='')) {
if (preg_match($ignore, $content)) {
continue;
}
} else if (isset($select) && ($select!='')) {
if (!preg_match($select, $content)) {
continue;
}
}
$content = preg_replace("/\[.*?\]/", "", $content);
$content = str_replace("\n", '<br>', $content);
if (strpos($content, '<br>') === 0)
{
$content = substr($content, 4);
}
$content = str_replace("<img", '<img style="box-sizing: border-box;border-radius: 4px; border: 1px solid #DADADA; padding: 2px;margin-top: 5px;object-fit: cover;"', $content);
?>
<li class="list-item">
<span style="font-size: .9em;">
<?php
echo '<a style="font-size: .9em; color: #5DADE2;" href="' . $item->link . '">@' . htmlspecialchars($author) . '</a> ';
?>
<span style="color: #aaa; font-size: .6em;"><time class="lately-a" datetime="<?php echo date("Y-m-d H:i:s", intval($item->timestamp)); ?>" itemprop="datePublished">
<?php echo date("Y-m-d H:i:s", (int)$item->timestamp); ?></time></span>
</span>
<?php
echo '<p style="padding-top: 10px;color: rgba(0, 0, 0, .6);font-size: 14px;">' . $content . '</p>';
?>
<br />
</li>
<?php
$counter++;
endforeach; ?>
<center><a href="<?php echo $spaceUrl ?>">查看更多动态</a></center>
<br>
</ul>
</div>
<?php
}
}