效果

我的哔哩哔哩

用法

我的环境是typecho博客程序+handsome主题,其他的没试过。

  1. 在blog根目录建立code目录。
  2. php文件存储为bilibili.php,并打开https://你的域名/code/bilibili.php,来更新up主头像。
  3. 在typecho新建独立页面,把独立页面代码拷贝进去,发布(我css很烂,有能力的改一下吧)。
  4. 添加一个crontab定时任务,时间设置为*/30 9-22 * * *,内容是curl https://你的域名/code/bilibili.php?action=update,意思是从早上9点到晚上22点,每半小时运行一次,你也可以把30改成15,就变成15分钟运行一次。
  5. php文件中的$UP就是你关注的up主列表了。

代码

独立页面代码

# 我的关注
!!!
<style>
#post-content{
background-color: #ddd;
}
#bili{
overflow: hidden;
padding-left: 20px;
border-radius: 20px;
}
#bili .bili_div{
position: relative;
width: 470px;
height: 180px;
float: left;
padding: 5px;
padding-bottom: 20px;
border-bottom: 2px dashed #ffc500;
}
#bili .bili_head{
position: absolute;
left: 0px;
top: 0px;
width: 50px;
height: 50px;
border-radius: 180px;
}
#bili .bili_main{
font-size: 12px;
padding-left: 50px;
}
#bili .bili_name{
padding: 0px 0px 0px 0px;
}
#bili .bili_name a{
text-decoration: none;
font-size: 13px;
font-weight: bold;
color: #000;
}
#bili .bili_time{
float: right;
font-weight: normal;
}
#bili .bili_title{
color:#333;
height: 24px;
overflow: hidden;
}
#bili .bili_title a{
text-decoration: none;
color: #f35;
}
#bili .bili_desc{
float: left;
margin-left: 10px;
line-height: 20px;
width: 220px;
height: 100px;
overflow: hidden;
}
#bili .bili_img{
float: left;
height:100px;
height: 112px;
width: 179px;
border-radius: 5px;
}
#bili .bili_bot{
margin-top: 5px;
}
</style>
<script>
$.ajax({
    dataType: "json",
    url: '/code/bilibili.json',
    catch: false,
    beforeSend:function(request) {
        request.setRequestHeader("Cache-control", "max-age: 600");
    }
})
//获取json
.success(function(bili_obj){
    //不更新
    bilibili_html(bili_obj);
});
function bilibili_html(obj){
$('#bili').html('');
$.each(obj.list,function(k,i){
    var creat_time = new Date(i.created * 1000).toLocaleString();
    $('#bili').append(
    '<div class="bili_div">'
    +'<div class="bili_head" style="background:url(/code/static/head_' + i.upimg + '.jpg)"></div>'
    +'<div class="bili_main"><div class="bili_name">'
    +'<a href="http://space.bilibili.com/' + i.mid + '" target="_blank">' + i.author + '</a>'
    +'<span class="bili_time">' + creat_time + '</span></div>'
    +'<div class="bili_title">投稿了 <a href="http://www.bilibili.com/video/av' + i.aid + '/" target="_blank">' + i.title + '</a></div>'
    +'<div class="bili_bot">'
    +'<div class="bili_img" style="background:url(/code/bilibili.php?img=' + i.pic + '@182w_112h.webp)"></div>'
    +'<div class="bili_desc">' + i.desc + '</div></div></div></div>'
    );
});
}
</script>
<div id="bili"></div>
!!!

php文件代码

<?php
/*
up主主页
老版本接口
http://space.bilibili.com/ajax/member/getSubmitVideos?order=pubdate&pagesize=10&mid=223146252
新版本接口 ps 页内数量
https://api.bilibili.com/x/space/arc/search?mid=223146252&ps=30&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp
*/

date_default_timezone_set('Asia/Shanghai');
//页面展示视频数量
define('SHOW_COUNT', 30);
//json文件名
define('JSON_FILE', './bilibili.json');

//bilibili关注id
$UP = [
//=====游戏=====
[
    //天天卡牌->逗鱼时刻
    //在当前up主的视频中搜索,范围限制在title和介绍
    'keyword'=>'逗鱼时刻',
    'mid'=>10462362,
],
//嘟督不噶油
//2403047,
//罗汉解说
51896064,

//=====杨超越=====
//午夜出动
353407160,
//杨超越视频站
107961740,

//=====科普学习=====
//李永乐老师
9458053,
//妈咪说
223146252,
//张召忠
33683045,
//芳斯塔芙
72270557,
//3Blue1Brown 数学之美
88461692,
//zettaranc
326246517,
//表象的本质
363584502,
//回形针PaperClip
258150656,
//奇闻观察室
454719565,

//=====美食=====
//徐大sao
390461123,
//厨师长农国栋
415479453,
//美食作家王刚
290526283,
//华农兄弟
250858633,

//=====数码=====
//科技美学-大家测
346098914,
//科技美学
3766866,
//钟文泽
25910292,
//远古时代装机猿
35359510,

//=====VLOG生活=====
//吃素的狮子
//808171,
//阿阳 跟西瓜视频签约了
//189999067,
//翔翔大作战
196356191,
//麻雀喳喳野行记
120122306,
//郭杰瑞
176037767,
//机智的党妹
466272,

//=====音乐=====
//bibipiano
419743655,
//郎朗piano
479893558,

//=====搞笑=====
//手工耿
280793434,

//=====影视解说=====
//木鱼水心
927587,
//电影最TOP
17819768,
];

//获取封面图片
function getImage(){
    $path = 'static/cover_' . md5($_GET['img']) . '.jpg';
    $url = 'https:' . $_GET['img'];
    if(file_exists($path)){
        header('Location: ./'.$path);exit;
    }else{
        $img = file_get_contents($url);
        file_put_contents($path, $img);
        //使用图片头输出浏览器
        header("Content-Type: image/jpeg;text/html; charset=utf-8");
        echo $img;
        exit;
    }
}
//更新up主头像,每次添加up主后访问bilibili.php更新头像
function updateImg(){
    global $UP;
    //删除static下所有图片
    if (!file_exists('./static')) {
        mkdir('./static');
    }else{
        exec("rm static/*.jpg");
    }
    $url = 'https://api.bilibili.com/x/space/acc/info?mid=';
    foreach($UP as $item){
        if(is_numeric($item)){
            $mid = $item;
        }else if(is_array($item) && isset($item['mid'])){
            $mid = $item['mid'];
        }else if(is_array($item) && empty($item['mid'])){
            $mid = $item['image'];
        }
        $url = 'https://api.bilibili.com/x/space/acc/info?mid=' . $mid;
        $jsonObj = json_decode(file_get_contents($url));
        //使用参数设置头像为50*50
        $imgUrl = $jsonObj->data->face . '@50w_50h.webp';
        $path = './static/head_'.$mid.'.jpg';
        file_put_contents($path, file_get_contents($imgUrl));
    }
    return true;
}
function updateJson(){
    global $UP;
    $ret = [];
    foreach($UP as $item){
        if(is_numeric($item)){
            $ret = array_merge($ret, up_list(['mid'=>$item]));
        }else if(is_array($item) && isset($item['mid'])){
            $ret = array_merge($ret, up_list($item));
        }else if(is_array($item) && empty($item['mid'])){
            $ret = array_merge($ret, search_list($item));
        }
    }
    $retSort = [];
    foreach($ret as $key=>&$item){
        $retSort[$item['created']] = $item;
    }
    krsort($retSort);
    $ret = json_encode(
        array(
        'last_time_str'=>date('Y-m-d H:i:s'),
        'last_time'=>time(),
        'list'=>array_slice($retSort, 0, SHOW_COUNT)
    ));
    file_put_contents(JSON_FILE, $ret);
    return $ret;
}
function search_list($conf){
    //https://api.bilibili.com/x/web-interface/search/type?context=&page=1&order=pubdate&tids_2=&__refresh__=true&search_type=video&tids=0&highlight=1&single_column=0&keyword=%E6%9D%A8%E8%B6%85%E8%B6%8A&duration=2
    $param = [
        'context'=>'',
        'page'=>'1',
        'order'=>'pubdate',
        'tids_2'=>'',
        '__refresh__'=>'true',
        'search_type'=>'video',
        'tids'=>'0',
        'highlight'=>'1',
        'single_column'=>'0',
        'keyword'=>isset($conf['keyword'])?urlencode($conf['keyword']):'', 
        'duration'=>isset($conf['duration'])?$conf['duration']:'0', 
        ];
    $url = 'https://api.bilibili.com/x/web-interface/search/type?';
    foreach($param as $key=>$item2){
        $url .= $key . '=' . $item2 . '&';
    }
    //echo $url;exit;
    $ret = [];
    $arr = json_decode(curl_get($url,['REFERER'=>'https://search.bilibili.com/']),1);
    foreach($arr['data']['result'] as $item){
        if(count($ret) == $conf['max'])break;
        $item['created'] = $item['pubdate'];
        $item['upimg'] = $conf['image'];
        $ret[$item['pubdate']] = json_format($item);
    }
    
    return $ret;
}
function up_list($conf){
    $param = [
        'tid'=>'0',
        //最多显示5个
        'ps'=>'5',
        //第一页
        'pn'=>'1',
        //在当前up的投稿中搜索
        'keyword'=>isset($conf['keyword'])?urlencode($conf['keyword']):'', 
        //按照时间排序
        'order'=>'pubdate',
        //up主id
        'mid'=>$conf['mid'],
        ];
    $url = 'https://api.bilibili.com/x/space/arc/search?';
    foreach($param as $key=>$item2){
        $url .= $key . '=' . $item2 . '&';
    }
    $ret = [];
    $arr = json_decode(file_get_contents($url),1);
    foreach($arr['data']['list']['vlist'] as $item){
        $ret[$item['created']] = json_format($item);
    }
    return $ret;
}
function json_format($json){
    return [
        'aid'=>$json['aid'],
        'pic'=>$json['pic'],
        'desc'=>$json['description'],
        'title'=>$json['title'],
        'author'=>$json['author'],
        'mid'=>$json['mid'],
        'created'=>$json['created'],
        'upimg'=>isset($json['upimg'])?$json['upimg']:$json['mid'],
        ];
}
function curl_get($url, $conf=[]){
    //初始化
    $ch = curl_init();
    //设置选项,包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    if(!empty($conf['REFERER'])){
        curl_setopt($ch, CURLOPT_REFERER, $conf['REFERER']);
    }
    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    // file_put_contents('./log.txt',$output);
    //释放curl句柄
    curl_close($ch);
    //打印获得的数据
    return $output;
}

if(isset($_GET['action']) && $_GET['action']=='update'){
    //更新json 并展示
    echo updateJson();
}else if(isset($_GET['img'])){
    //获取cover图片
    getImage();
}else if(empty($_SERVER['HTTP_REFERER'])){
    //如果直接访问则清空所有图片 并更新up主头像
    updateImg();
    echo "update head images finish.";
}
最后修改:2020 年 03 月 15 日 05 : 59 PM