-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBiliGet.php
172 lines (165 loc) · 7.14 KB
/
BiliGet.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
163
164
165
166
167
168
169
170
171
172
<?php
/**
* @file
* @ingroup Extensions
* @author Lehmaning
* @license GPL-3.0-or-later
* @todo 并行处理多个请求
*/
class BiliGet {
/**
* @param Parser &$parser
*/
public static function registerTags( &$parser ) {
$parser->setHook( 'biliget' , [ __CLASS__ , 'getInfo' ] );
}
/**
* @param string $url
* @see https://www.runoob.com/php/php-ref-curl.html#div-comment-36119
*/
public static function getUrl($url) {
$headerArray = array("Content-type:application/json;","Accept:application/json");
//$headerArray[] = "Origin: https://www.bilibili.com";
//$userAgent = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.7113.93 Safari/537.36";
if(!empty($wfSetUserAgent)) curl_setopt($ch, CURLOPT_USERAGENT, $wfSetUserAgent);
if(!empty($wfSetBiliCookie)) curl_setopt($ch, CURLOPT_COOKIE, $wfSetBiliCookie);
if(!empty($aid)) curl_setopt($ch, CURLOPT_REFERER, "https://www.bilibili.com/av$aid");
if(!empty($bid)) curl_setopt($ch, CURLOPT_REFERER, "https://www.bilibili.com/BV$bvid");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
return $output;
}
public static function url2aid($url) {
$aid_data = [];
$aid_pattern = "#(?:https?:\/\/(?:www\.)?bilibili\.com\/video\/)?av(\d+)(\?p=\d{1,3})?#";
if(preg_match($aid_pattern, $url, $preg)) {
$aid_data = [
'aid' => $preg[1],
'part' => $preg[2]
];
}
return $aid_data;
}
public static function url2bvid($url) {
$bvid_data = [];
$bvid_pattern = "#(?:https?:\/\/(?:www\.)?bilibili\.com\/video\/)?BV([a-zA-Z0-9]+)(\?p=\d{1,3})?#";
if(preg_match($bvid_pattern, $url, $preg)) {
$bvid_data = [
'bvid' => $preg[1],
'part' => $preg[2]
];
}
return $bvid_data;
}
public static function handleDesc($text) {
$vid_pattern = "#(BV[a-zA-Z0-9]+|(av|sm|ac|om)(\d+)|watch\?v=[a-zA-Z0-9])#";
$url_pattern = "/https?:\/\/(www\.)?[a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_\+.~#?&//=]*)/m"; //@see https://stackoverflow.com/a/3809435
$output_desc = preg_replace($url_pattern, "<a href=\"$1\">$1</a>", trim($text));
return $output_desc;
}
/*
* @param Parser &$parser
* @param string $input
* @param Array $argv
* @param PPFrame $frame
*/
public static function getInfo($input, $argv, $parser, $frame) {
global $wfSetBiliCookie;
global $wfSetUserAgent;
$parser->getOutput()->addModules('ext.biliget.list');
$input_arr = explode("
", $input);
if ( !empty( self::url2aid($input) ) ) {
$aid_data = ( count($input_arr) == 1 ) ? self::url2aid($input) : self::url2aid($input_arr);
$aid = $aid_data['aid'];
$part = $aid_data['part'];
$id = "av$aid";
$data = self::getUrl("https://api.bilibili.com/x/web-interface/view?aid=$aid", $wfSetBiliCookie);
$url = "https://bilibili.com/video/av$aid?p=$part";
if( !empty($part) ) $link .= "?p=$part";
}
if ( !empty( self::url2bvid($input) ) ) {
$bvid_data = ( count($input_arr) == 1 ) ? self::url2bvid($input) : self::url2bvid($input_arr);
$bvid = $bvid_data['bvid'];
$part = $bvid_data['part'];
$id = "BV$bvid";
$data = self::getUrl("https://api.bilibili.com/x/web-interface/view?bvid=$bvid");
$url = "https://bilibili.com/video/BV$bvid";
if( !empty($part) ) $link .= "?p=$part";
}
if ( empty($id) ) return '';
$code = $data['code']; // 判断响应代码
if ($code==0) {
$cover = $data['data']['pic'];
$uploader = $data['data']['owner']['name'];
$desc = str_replace("\n", "<br />", $data['data']['desc']);
$title = $data['data']['title'];
$pubdate = date("Y-m-d H:i", $data['data']['pubdate']);
$duration = gmstrftime("%M:%S", $data['data']['pages'][0]['duration']);
if ( !empty($argv['type']) && $argv['type']=='raw' ) {
$output = $parser->recursiveTagParse("
{{#vardefine:封面|$cover}}
{{#vardefine:UP主|$uploader}}
{{#vardefine:简介|$desc}}
{{#vardefine:标题|$title}}
{{#vardefine:发布日期|$pubdate}}
{{#vardefine:视频时长|$duaration}}
", $frame);
//@require https://www.mediawiki.org/wiki/Extension:Variables
return $output;
}
else if ( !empty($argv['type'] ) ) { // 处理 type 参数
$type = array(
'封面' => $cover,
'UP主'=> $uploader,
'简介' => $desc,
'标题' => $title,
'发布日期' => $pubdate,
'时长' => $duration
);
if ( strpos($argv['type'], " ") ) {
$output = '';
foreach ($type as $key => $value) {
$output .= "{{#vardefine:$key|$value}}";
}
return $parser->recursiveTagParse($output, $frame);
}
return $type[trim($argv['type'])];
}
else { // 默认输出
return "
<div class=\"bili-info-card\">
<div class=\"bili-info\">
<div class=\"bili-info-cover\"><img src=\"$cover\" width=\"160px\" loading=\"lazy\" /></div>
<p class=\"bili-info-duration\">$duration</p>
</div>
<div class=\"bili-info\">
<p class=\"bili-info-title\" style=\"margin-bottom: 0\"><a href=\"$url\" title=\"$id\">$title</a></p>
<p class=\"bili-info-date\" style=\"margin-top: 0; margin-bottom: 0\">$pubdate</p>
<p class=\"bili-info-uploader\" style=\"margin-top: 0;\">UP主:$uploader</p>
<p class=\"bili-info-desc\">$desc</p>
</div>
</div>";
}
}
else {// 收到错误代码
$message = $data['message'];
return "
<div class=\"bili-info-card\">
<div class=\"bili-info\" style=\"width: 80%\">
<p>错误码:$code</p>
</div>
<div class=\"bili-info\">
<p class=\"bili-info-title\">$message</p>
</div>
</div>";
}
}
}