Skip to content

Commit

Permalink
Extended support for bilibili param
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueCocoa committed Nov 16, 2016
1 parent 7eae390 commit 19d01df
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
Binary file modified dist/dplayer.zip
Binary file not shown.
49 changes: 47 additions & 2 deletions dplayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Plugin Name: DPlayer for WordPress
* Description: Wow, such a lovely HTML5 danmaku video player comes to WordPress
* Version: 1.1.3
* Version: 1.1.4
* Author: 0xBBC
* Author URI: https://blog.0xbbc.com/
* License: GPLv3
Expand Down Expand Up @@ -47,6 +47,50 @@ public static function dplayer_uninstall() {
delete_option( 'kblog_danmaku_token' );
}

public static function dplayer_bilibili_url_handler($bilibili_url) {
$aid = 0;
$page = 1;
$is_bilibili = false;
if (preg_match('/^[\d]+$/', $bilibili_url)) {
$aid = $bilibili_url;
$is_bilibili = true;
} else {
$parsed = parse_url($bilibili_url);
if ($parsed['host'] === 'www.bilibili.com') {
preg_match('/^\/video\/av([\d]+)(?:\/index_([\d]+)\.html)?/', $parsed['path'], $path_match);
if ($path_match) {
$is_bilibili = true;
$aid = $path_match[1];
$page = $path_match[2] == null ? 1 : $path_match[2];
preg_match('/^page=([\d]+)$/', $parsed['fragment'], $page_match);
if ($page_match) $page = $page_match[1];
}
}
}

if ($is_bilibili) {
if ($page == 1) {
return array(get_option( 'kblog_danmaku_url', '' ).'bilibili?aid='.$aid);
} else {
$cid = -1;
$json_response = @json_decode(gzdecode(file_get_contents('http://www.bilibili.com/widget/getPageList?aid='.$aid)), true);
if ($json_response) {
foreach ($json_response as $page_info) {
if ($page_info['page'] == $page) {
$cid = $page_info['cid'];
break;
}
}
}

if ($cid != -1) {
return array(get_option( 'kblog_danmaku_url', '' ).'bilibili?cid='.$cid);
}
}
}
return null;
}

public static function dplayer_load($atts = [], $content = null, $tag = '') {
// normalize attribute keys, lowercase
$atts = array_change_key_case((array)$atts, CASE_LOWER);
Expand Down Expand Up @@ -87,7 +131,8 @@ public static function dplayer_load($atts = [], $content = null, $tag = '') {
'token' => get_option( 'kblog_danmaku_token', '' ),
'api' => get_option( 'kblog_danmaku_url', '' ),
);
if ($atts['bilibili']) $danmaku['addition'] = array(get_option( 'kblog_danmaku_url', '' ).'bilibili?aid='.$atts['bilibili']);

if ($atts['bilibili']) $danmaku['addition'] = DPlayer::dplayer_bilibili_url_handler($atts['bilibili']);
$data['danmaku'] = ($atts['danmu'] != 'false') ? $danmaku : null;

$js = json_encode($data);
Expand Down
14 changes: 12 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: 0xbbc
Tags: video, player, shortcode
Requires at least: 3.0.1
Tested up to: 4.6.1
Stable tag: 1.1.3
Stable tag: 1.1.4
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html

Expand All @@ -22,7 +22,7 @@ Parameter 'screenshot', enable screenshot?. Optional and default false.
Parameter 'loop', enable loop?. Optional and default false.
Parameter 'preload', preload mode, 'auto', 'metatdata' or 'none'. Optional and default metadata.
Parameter 'hotkey', enable builtin hotkey? including left, right and Space. Optional and default true.
Parameter 'bilibili', bilibili视频AV号. Additional danmaku from bilibili
Parameter 'bilibili', bilibili视频AV号 或者 完整的bilibili视频链接. Additional danmaku from bilibili
Parameter 'danmu', should DPlayer load danmaku. Default false and it's optional.

== Installation ==
Expand All @@ -39,6 +39,16 @@ Parameter 'danmu', should DPlayer load danmaku. Default false and it's optional.

== Changelog ==

= 1.1.4 =
* Extended support for bilibili param, now you can use either
- bilibili='23333'
- bilibili='http://www.bilibili.com/video/av2333333/index_233.html#page=2333'
to load additional danmaku.

This plugin will give you exactly the danma that you need, which means,

If you give the original URL to the bilibili video, this plugin will identify the URL format, load the right danmaku. In this example, 'http://www.bilibili.com/video/av2333333/index_23333#page=233' is given. And the plugin knows that aid is 2333333 and your requested page is 2333. (According to bilibili, 'index_233.html#page=2333' means that you starts at page 233 but currently you're watching page 2333)

= 1.1.3 =
* Fixed bilibili danmaku support

Expand Down

0 comments on commit 19d01df

Please sign in to comment.