-
Notifications
You must be signed in to change notification settings - Fork 1k
/
PikabuBridge.php
162 lines (142 loc) · 5.22 KB
/
PikabuBridge.php
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
<?php
class PikabuBridge extends BridgeAbstract
{
const NAME = 'Пикабу';
const URI = 'https://pikabu.ru';
const DESCRIPTION = 'Выводит посты по тегу, сообществу или пользователю';
const MAINTAINER = 'em92';
const PARAMETERS_FILTER = [
'name' => 'Фильтр',
'type' => 'list',
'values' => [
'Горячее' => 'hot',
'Свежее' => 'new',
],
'defaultValue' => 'hot',
];
const PARAMETERS = [
'По тегу' => [
'tag' => [
'name' => 'Тег',
'exampleValue' => 'it',
'required' => true
],
'filter' => self::PARAMETERS_FILTER
],
'По сообществу' => [
'community' => [
'name' => 'Сообщество',
'exampleValue' => 'linux',
'required' => true
],
'filter' => self::PARAMETERS_FILTER
],
'По пользователю' => [
'user' => [
'name' => 'Пользователь',
'exampleValue' => 'admin',
'required' => true
]
]
];
protected $title = null;
public function getURI()
{
if ($this->getInput('tag')) {
return self::URI . '/tag/' . rawurlencode($this->getInput('tag')) . '/' . rawurlencode($this->getInput('filter'));
} elseif ($this->getInput('user')) {
return self::URI . '/@' . rawurlencode($this->getInput('user'));
} elseif ($this->getInput('community')) {
$uri = self::URI . '/community/' . rawurlencode($this->getInput('community'));
if ($this->getInput('filter') != 'hot') {
$uri .= '/' . rawurlencode($this->getInput('filter'));
}
return $uri;
} else {
return parent::getURI();
}
}
public function getIcon()
{
return 'https://cs.pikabu.ru/assets/favicon.ico';
}
public function getName()
{
if (is_null($this->title)) {
return parent::getName();
} else {
return $this->title . ' - ' . parent::getName();
}
}
public function collectData()
{
$link = $this->getURI();
$text_html = getContents($link);
$text_html = iconv('windows-1251', 'utf-8', $text_html);
$html = str_get_html($text_html);
$this->title = $html->find('title', 0)->innertext;
foreach ($html->find('article.story') as $post) {
$time = $post->find('time.story__datetime', 0);
if (is_null($time)) {
continue;
}
$el_to_remove_selectors = [
'.story__read-more',
'script',
'svg.story-image__stretch',
];
foreach ($el_to_remove_selectors as $el_to_remove_selector) {
foreach ($post->find($el_to_remove_selector) as $el) {
$el->outertext = '';
}
}
foreach ($post->find('[data-type=gifx]') as $el) {
$src = $el->getAttribute('data-source');
$el->outertext = '<img src="' . $src . '">';
}
foreach ($post->find('img') as $img) {
$src = $img->getAttribute('src');
if (!$src) {
$src = $img->getAttribute('data-src');
if (!$src) {
continue;
}
}
$img->outertext = '<img src="' . $src . '">';
// it is assumed, that img's parents are links to post itself
// we don't need them
$img->parent()->outertext = $img->outertext;
}
$categories = [];
foreach ($post->find('.tags__tag') as $tag) {
if ($tag->getAttribute('data-tag')) {
$categories[] = $tag->innertext;
}
}
$title_element = $post->find('.story__title-link', 0);
if (str_contains($title_element->href, 'from=cpm')) {
// skip sponsored posts
continue;
}
$title = $title_element->plaintext;
$community_link = $post->find('.story__community-link', 0);
// adding special marker for "Maybe News" section
// these posts are fake
if (!is_null($community_link) && $community_link->getAttribute('href') == '/community/maybenews') {
$title = '[' . trim($community_link->plaintext) . '] ' . $title;
}
$item = [];
$item['categories'] = $categories;
$item['author'] = trim($post->find('.user__nick', 0)->plaintext);
$item['title'] = $title;
$item['content'] = strip_tags(
backgroundToImg($post->find('.story__content-inner', 0)->innertext),
'<br><p><img><a><s>
'
);
$item['uri'] = $title_element->href;
$item['timestamp'] = strtotime($time->getAttribute('datetime'));
$this->items[] = $item;
}
}
}