Skip to content

Commit 85ba959

Browse files
committed
增加bilibili板块
1 parent 5039d75 commit 85ba959

File tree

12 files changed

+364
-7
lines changed

12 files changed

+364
-7
lines changed

app/Controller/IndexController.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace App\Controller;
66

7+
use App\Foundation\Facades\Log;
78
use App\Foundation\Utils\Mail;
9+
use App\Model\Laboratory\Bilibili\UpUser;
810
use Hyperf\DbConnection\Db;
911
use Hyperf\HttpServer\Annotation\Controller;
1012
use Hyperf\HttpServer\Annotation\RequestMapping;
@@ -26,9 +28,42 @@ public function __construct()
2628
* @RequestMapping(path="/test", methods="get,post")
2729
*/
2830
public function index()
29-
{
31+
{
32+
return config('bilibili.cookie');
33+
// $url = 'https://m.bilibili.com/video/BV1Q624y1q7sj';
3034

31-
}
3235

36+
// $lastString = basename($url);
37+
// $mid = explode('?', $lastString)[0] ?? '';
38+
//
39+
// $upUser = new UpUser();
40+
// $upUser->mid = $mid;
41+
// $upUser->timed_status = 1;
42+
// $upUser->save();
43+
// return $upUser->mid;
44+
// preg_match('/.*?av(\d{0,})\/?/', $url,$m);
45+
// if (!isset($m[1])) {
46+
// return false;
47+
// }
48+
// $vid = $m[1];//获取视频uid
49+
//
50+
// return $vid;
51+
52+
// $infoUrl = 'https://api.bilibili.com/x/web-interface/archive/stat?aid='.$vid;//拼接得到接口地址
53+
// $infoRespose = doCurlGetRequest($infoUrl);
54+
//
55+
// $data = $this->bilibili($infoRespose);//获取视频抓取数据
56+
// $respose = file_get_contents("https://api.bilibili.com/x/web-interface/view?aid=".$vid);
57+
//
58+
// if (empty($respose) || !$respose) return false;
59+
// $resposeData = json_decode($respose,true)['data'];
60+
//
61+
// $data['name'] = $resposeData['title'];
62+
// $data['time'] = date('Y-m-d H:i:s',$resposeData['ctime']);
63+
// $data['author'] = $resposeData['owner']['name'];
64+
//
65+
// return $data;
66+
67+
}
3368

3469
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Controller\Laboratory\Bilibili;
5+
6+
use App\Constants\StatusCode;
7+
use App\Controller\AbstractController;
8+
use App\Foundation\Annotation\Explanation;
9+
use App\Foundation\Utils\Queue;
10+
use App\Job\Bilibili\UpUserInfoRecordJob;
11+
use App\Model\Laboratory\Bilibili\UpUser;
12+
use Hyperf\Di\Annotation\Inject;
13+
use Hyperf\HttpServer\Annotation\Controller;
14+
use Hyperf\HttpServer\Annotation\Middleware;
15+
use Hyperf\HttpServer\Annotation\Middlewares;
16+
use Hyperf\HttpServer\Annotation\RequestMapping;
17+
use App\Middleware\RequestMiddleware;
18+
use App\Middleware\PermissionMiddleware;
19+
20+
/**
21+
* Up主
22+
* Class UpUserController
23+
* @Controller(prefix="laboratory/bilibili_module/up_user")
24+
*/
25+
class UpUserController extends AbstractController
26+
{
27+
/**
28+
* @Inject()
29+
* @var UpUser
30+
*/
31+
private $upUser;
32+
33+
/**
34+
* @Inject()
35+
* @var Queue
36+
*/
37+
private $queue;
38+
39+
/**
40+
* @Explanation(content="录入Up主")
41+
* @RequestMapping(path="up_user_add", methods="post")
42+
* @Middlewares({
43+
* @Middleware(RequestMiddleware::class),
44+
* @Middleware(PermissionMiddleware::class)
45+
* })
46+
* @throws \Exception
47+
* @return \Psr\Http\Message\ResponseInterface
48+
*/
49+
public function upUserAdd()
50+
{
51+
$upUserInfo = $this->request->all()['up_user_info'] ?? [];
52+
if (empty($upUserInfo)) $this->throwExp(StatusCode::ERR_VALIDATION, 'Up主信息不能为空');
53+
54+
//是否存在空的upUserUrl
55+
$isExistEmptyUrl = false;
56+
$addMidArr = [];
57+
foreach ($upUserInfo as $upUser) {
58+
$upUserUrl = $upUser['up_user_url'] ?? '';
59+
$timedStatus = $upUser['timed_status'] ?? '';
60+
if (empty($upUserUrl)) {
61+
$isExistEmptyUrl = true;
62+
continue;
63+
}
64+
$lastString = basename($upUserUrl);
65+
$mid = explode('?', $lastString)[0] ?? '';
66+
if (empty($mid)) continue;
67+
68+
$upUser = new UpUser();
69+
$upUser->mid = $mid;
70+
$upUser->timed_status = $timedStatus;
71+
$upUser->save();
72+
$addMidArr[] = $mid;
73+
}
74+
//推送一个队列,异步获取Up主信息
75+
$this->queue->push(new UpUserInfoRecordJob([
76+
'up_user_mid' => $addMidArr,
77+
]));
78+
79+
if ($isExistEmptyUrl) return $this->successByMessage('录入Up主成功,部分Url条目为空录入失败');
80+
81+
return $this->successByMessage('录入Up主成功');
82+
}
83+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller\User;
6+
7+
use App\Controller\AbstractController;
8+
use Hyperf\HttpServer\Annotation\AutoController;
9+
10+
/**
11+
* 测试控制器,一般用来测试一些代码
12+
* Class IndexController
13+
* @AutoController()
14+
*/
15+
class IndexController extends AbstractController
16+
{
17+
public function __construct()
18+
{
19+
20+
}
21+
22+
/**
23+
* 获取用户数据列表
24+
*/
25+
public function index()
26+
{
27+
return 123;
28+
}
29+
30+
public function user()
31+
{
32+
return 1;
33+
}
34+
}

app/Foundation/Facades/Log.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function sqlLog()
7171
*/
7272
public static function jobLog()
7373
{
74-
return self::channel('jon_log', config('app_env', 'app'));
74+
return self::channel('job_log', config('app_env', 'app'));
7575

7676
}
7777
}

app/Foundation/Helpers/curl.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?PHP
22
if (!function_exists('curl_get')) {
33
// 传递数据以易于阅读的样式格式化后输出
4-
function curl_get($apiUrl = '', $sendData = [], $header = [])
4+
function curl_get($apiUrl = '', $sendData = [], $header = [], $cookie = '')
55
{
66
if (!empty($sendData)) $apiUrl .= '?' . http_build_query($sendData);
77

@@ -21,10 +21,12 @@ function curl_get($apiUrl = '', $sendData = [], $header = [])
2121
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
2222
curl_setopt($curl, CURLOPT_HEADER, 1);
2323
}
24+
if (!empty($cookie)) curl_setopt($curl, CURLOPT_COOKIE, $cookie);
2425
$response = curl_exec($curl);
2526
if (empty($response)) Throw new Exception(curl_error($curl), 400);
2627
curl_close($curl);
27-
return $response;
28+
29+
return json_decode($response, true);
2830
}
2931
}
3032

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Job\Bilibili;
6+
7+
use App\Foundation\Facades\Log;
8+
use App\Foundation\Utils\Mail;
9+
use App\Model\Auth\User;
10+
use App\Service\Laboratory\Bilibili\UpUserService;
11+
use Hyperf\AsyncQueue\Job;
12+
13+
/**
14+
* 新录入Up主信息录入
15+
* Class UpUserInfoRecordJob
16+
* @package App\Job\Bilibili
17+
* @Author YiYuan-Lin
18+
* @Date: 2021/8/20
19+
*/
20+
class UpUserInfoRecordJob extends Job
21+
{
22+
public $params;
23+
/**
24+
* 任务执行失败后的重试次数,即最大执行次数为 $maxAttempts+1 次
25+
* @var int
26+
*/
27+
protected $maxAttempts = 2;
28+
29+
public function __construct($params)
30+
{
31+
$this->params = $params;
32+
}
33+
34+
public function handle()
35+
{
36+
try {
37+
UpUserService::getInstance()->recordUpUserInfoFromBilibili($this->params['up_user_mid']);
38+
} catch (\Exception $e) {
39+
Log::jobLog()->error($e->getMessage());
40+
}
41+
}
42+
43+
}

app/Middleware/CheckMaintainMiddleware.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CheckMaintainMiddleware implements MiddlewareInterface
2626
{
2727
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
2828
{
29-
$writeRoute = ['/common/sys_config', '/common/auth/verification_code', '/auth/login', '/auth/register', '/test'];
29+
$writeRoute = ['/common/sys_config', '/common/auth/verification_code', '/auth/login', '/auth/register', '/test', ];
3030
if (in_array($request->getUri()->getPath(), $writeRoute)) return $handler->handle($request);
3131

3232
//获取当前用户
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Model\Laboratory\Bilibili;
6+
7+
use App\Model\Model;
8+
9+
/**
10+
* UP主信息表
11+
* Class UpUser
12+
* @package App\Model\Laboratory\Bilibili
13+
* @Author YiYuan-Lin
14+
* @Date: 2021/8/20
15+
*/
16+
class UpUser extends Model
17+
{
18+
/**
19+
* The table associated with the model.
20+
*
21+
* @var string
22+
*/
23+
protected $table = 'bili_up_user';
24+
25+
/**
26+
* The connection name for the model.
27+
*
28+
* @var string
29+
*/
30+
protected $connection = 'default';
31+
32+
/**
33+
* The attributes that are mass assignable.
34+
*
35+
* @var array
36+
*/
37+
protected $fillable = [];
38+
39+
/**
40+
* The attributes that should be cast to native types.
41+
*
42+
* @var array
43+
*/
44+
protected $casts = [];
45+
46+
/**
47+
* 定时任务开关状态
48+
*/
49+
const TIMED_STATUS_ON = 1;
50+
const TIMED_STATUS_OFF = 0;
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
namespace App\Service\Laboratory\Bilibili;
3+
4+
use App\Foundation\Traits\Singleton;
5+
use App\Model\Laboratory\Bilibili\UpUser;
6+
use App\Service\BaseService;
7+
8+
class UpUserService extends BaseService
9+
{
10+
use Singleton;
11+
12+
/**
13+
* UP主信息(名称、性别、头像、描述、个人认证信息、大会员状态、直播间地址、预览图、标题、房间号、观看人数、直播间状态[开启/关闭]等)
14+
* @var string
15+
*/
16+
private $upUserInfoApi = 'https://api.bilibili.com/x/space/acc/info?mid=';
17+
18+
/**
19+
* UP主粉丝数、关注数
20+
* @var string
21+
*/
22+
private $upUserStatApi = 'https://api.bilibili.com/x/relation/stat?vmid=';
23+
24+
/**
25+
* UP主总播放数、总专栏浏览数
26+
* @var string
27+
*/
28+
private $upUserUpStatApi = 'https://api.bilibili.com/x/space/upstat?mid=';
29+
30+
/**
31+
* UP主充电信息(月充电人数、月充电用户、总充电人数)
32+
* @var string
33+
*/
34+
private $upUserElecApi = 'https://api.bilibili.com/x/ugcpay-rank/elec/month/up?up_mid=';
35+
36+
/**
37+
* 根据Up主mid获取相关数据信息
38+
* @param array $upUserMid
39+
* @return bool
40+
* @throws \Exception
41+
*/
42+
public function recordUpUserInfoFromBilibili(array $upUserMid) : bool
43+
{
44+
if (empty($upUserMid)) return false;
45+
46+
foreach ($upUserMid as $mid) {
47+
$upUserInfo = curl_get($this->upUserInfoApi . $mid);
48+
$upUserStat = curl_get($this->upUserStatApi . $mid);
49+
//这个接口比较特殊,需要用到cookie
50+
$upUserUpStat = curl_get($this->upUserUpStatApi . $mid, [], [], config('bilibili.cookie'));
51+
$upUserElec = curl_get($this->upUserElecApi . $mid);
52+
53+
$updateData['name'] = $upUserInfo['data']['name'] ?? '';
54+
$updateData['sex'] = $upUserInfo['data']['sex'] ?? '';
55+
$updateData['sign'] = $upUserInfo['data']['sign'] ?? '';
56+
$updateData['face'] = $upUserInfo['data']['face'] ?? '';
57+
$updateData['level'] = $upUserInfo['data']['level'] ?? '';
58+
$updateData['top_photo'] = $upUserInfo['data']['top_photo'] ?? '';
59+
$updateData['birthday'] = $upUserInfo['data']['birthday'] ?? '';
60+
$updateData['following'] = $upUserStat['data']['following'] ?? 0;
61+
$updateData['follower'] = $upUserStat['data']['follower'] ?? 0;
62+
$updateData['video_play'] = $upUserUpStat['data']['archive']['view'] ?? 0;
63+
$updateData['readling'] = $upUserUpStat['data']['article']['view'] ?? 0;
64+
$updateData['likes'] = $upUserUpStat['data']['likes'] ?? 0;
65+
$updateData['recharge_month'] = $upUserElec['data']['count'] ?? 0;
66+
$updateData['recharge_total'] = $upUserElec['data']['total'] ?? 0;
67+
$updateData['live_room_info'] = empty($upUserInfo['data']['live_room']) ? '' : json_encode($upUserInfo['data']['live_room']);
68+
$updateData['updated_at'] = date('Y-m-d H:i:s');
69+
UpUser::where('mid', $mid)->update($updateData);
70+
}
71+
72+
return true;
73+
}
74+
}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"easyswoole/verifycode": "3.x",
3838
"hyperf/filesystem": "2.1.*",
3939
"xxtime/flysystem-aliyun-oss": "^1.5",
40-
"dragonmantank/cron-expression": "^3.1"
40+
"dragonmantank/cron-expression": "^3.1",
41+
"doctrine/dbal": "^3.0"
4142
},
4243
"require-dev": {
4344
"swoole/ide-helper": "^4.5",

0 commit comments

Comments
 (0)