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
|
<?php
class RutubeBridge extends BridgeAbstract
{
const NAME = 'Rutube';
const URI = 'https://rutube.ru';
const MAINTAINER = 'em92';
const DESCRIPTION = 'Выводит ленту видео';
const PARAMETERS = [
'По каналу' => [
'c' => [
'name' => 'ИД канала',
'exampleValue' => 1342940, // Мятежник Джек
'type' => 'number',
'required' => true
],
],
'По плейлисту' => [
'p' => [
'name' => 'ИД плейлиста',
'exampleValue' => 83641, // QRUSH
'type' => 'number',
'required' => true
],
],
'По результатам поиска' => [
's' => [
'name' => 'Запрос',
'exampleValue' => 'SUREN',
'required' => true,
]
]
];
protected $title;
public function getURI()
{
if ($this->getInput('c')) {
return self::URI . '/channel/' . strval($this->getInput('c')) . '/videos/';
} elseif ($this->getInput('p')) {
return self::URI . '/plst/' . strval($this->getInput('p')) . '/';
} elseif ($this->getInput('s')) {
return self::URI . '/search/?suggest=1&query=' . strval($this->getInput('s'));
} else {
return parent::getURI();
}
}
public function getIcon()
{
return 'https://static.rutube.ru/static/favicon.ico';
}
public function getName()
{
if (is_null($this->title)) {
return parent::getName();
} else {
return $this->title . ' - ' . parent::getName();
}
}
private function getJSONData($html)
{
$jsonDataRegex = '/window.reduxState = (.*);/';
preg_match($jsonDataRegex, $html, $matches) or returnServerError('Could not find reduxState');
$map = [
'\x26' => '&',
'\x3c' => '<',
'\x3d' => '=',
'\x3e' => '>',
'\x3f' => '?',
];
$jsonString = str_replace(array_keys($map), array_values($map), $matches[1]);
return json_decode($jsonString, false);
}
private function getVideosFromReduxState()
{
$link = $this->getURI();
$html = getContents($link);
$reduxState = $this->getJSONData($html);
$videos = [];
if ($this->getInput('c')) {
$videosMethod = 'videos(' . $this->getInput('c') . ')';
$channelInfoMethod = 'channelInfo({"userChannelId":' . $this->getInput('c') . '})';
$videos = $reduxState->api->queries->$videosMethod->data->results;
$this->title = $reduxState->api->queries->$channelInfoMethod->data->name;
} elseif ($this->getInput('p')) {
$playListVideosMethod = 'getPlaylistVideos(' . $this->getInput('p') . ')';
$videos = $reduxState->api->queries->$playListVideosMethod->data->results;
$playListMethod = 'getPlaylist(' . $this->getInput('p') . ')';
$this->title = $reduxState->api->queries->$playListMethod->data->title;
} elseif ($this->getInput('s')) {
$this->title = 'Поиск ' . $this->getInput('s');
}
return $videos;
}
private function getVideosFromSearchAPI()
{
$contents = getContents(self::URI . '/api/search/video/?suggest=1&client=wdp&query=' . $this->getInput('s'));
$json = json_decode($contents);
return $json->results;
}
public function collectData()
{
if ($this->getInput('c') || $this->getInput('p')) {
$videos = $this->getVideosFromReduxState();
} else {
$videos = $this->getVideosFromSearchAPI();
}
foreach ($videos as $video) {
$item = [];
$item['title'] = $video->title;
$item['uri'] = $video->video_url;
$content = '<a href="' . $video->video_url . '">';
$content .= '<img src="' . $video->thumbnail_url . '" />';
$content .= '</a><br/>';
$content .= nl2br(
// Converting links in plaintext
// Copied from https://stackoverflow.com/a/12590772
preg_replace(
'$(https?://[a-z0-9_./?=&#-]+)(?![^<>]*>)$i',
' <a href="$1" target="_blank">$1</a> ',
$video->description . ' '
)
);
$item['timestamp'] = $video->publication_ts;
$item['author'] = $video->author->name;
$item['content'] = $content;
$this->items[] = $item;
}
}
}
|