From d3b6a1272d2132f01013fe7ebc9dde39b107e797 Mon Sep 17 00:00:00 2001 From: METO Date: Wed, 9 May 2018 11:58:16 +0800 Subject: [PATCH] =?UTF-8?q?:taco:=200.9.1=20=E6=B7=BB=E5=8A=A0=E6=89=AD?= =?UTF-8?q?=E8=9B=8B=E6=9C=BA=E3=80=81=E5=BA=94=E6=8F=B4=E5=9B=A2=E7=AD=BE?= =?UTF-8?q?=E5=88=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 19 +++++ CHANGELOG.md | 17 ++++- README.md | 6 +- index.php | 32 +++++--- src/{plugins => libs}/Config.php | 2 +- src/libs/Helper.php | 13 ++-- src/libs/Log.php | 3 + src/plugins/Base.php | 13 ++-- src/plugins/Capsule.php | 83 +++++++++++++++++++++ src/plugins/{Websocket.php => Danmaku.php} | 4 +- src/plugins/Group.php | 87 ++++++++++++++++++++++ src/plugins/SmallTV.php | 2 +- 12 files changed, 252 insertions(+), 29 deletions(-) create mode 100644 .travis.yml rename src/{plugins => libs}/Config.php (98%) create mode 100644 src/plugins/Capsule.php rename src/plugins/{Websocket.php => Danmaku.php} (98%) create mode 100644 src/plugins/Group.php diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..92fb4dc --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ +language: php + +matrix: + fast_finish: true + + include: + - php: '5.6' + - php: '7.0' + - php: '7.1' + - php: '7.2' + - php: 'nightly' + + allow_failures: + - php: 'nightly' + +sudo: false + +script: + - find -L . -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l diff --git a/CHANGELOG.md b/CHANGELOG.md index 730dc68..3fff05b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,21 @@ # Release Notes -## v0.9.0 (2018-05-07) +## v0.9.1 (2018-05-09) + +### Added +- 添加应援团签到功能 +- 添加扭蛋机功能(暂时支持普通扭蛋币) + +### Changed +- [dev] 当配置文件为空时跳过写入 +- 当回调地址为空时跳过 + +### Fixed +- 兼容 PHP 5.6 版本测试 ([#37](https://github.com/metowolf/BilibiliHelper/issues/37)) +- 修复部分接口逻辑问题 + + +## v0.9.0-pre (2018-05-07) **该版本为非兼容更新,从 0.8.x 升级需要重新覆盖配置文件** diff --git a/README.md b/README.md index a0c4c01..19a6950 100644 --- a/README.md +++ b/README.md @@ -15,20 +15,20 @@ B 站直播实用脚本 |plugin |version |description | |---------|---------|--------------| |Auth |18.05.07 |帐号登录组件 | +|Capsule |18.05.09 |扭蛋机(普通) | |DailyBag |18.05.07 |每日礼包领取 | +|Group |18.05.09 |应援团签到 | |Heart |18.05.07 |双端直播间心跳 | |Silver |18.05.07 |免费宝箱领取 | -|SmallTV |18.05.07 |小电视抽奖 | +|SmallTV |18.05.08 |小电视抽奖 | |Task |18.05.07 |每日任务 | ## 未完成功能 |待续| |-------| -|扭蛋机| |总督检测| |节奏风暴| -|应援团签到| ## 环境依赖 diff --git a/index.php b/index.php index 873878a..ce3d339 100644 --- a/index.php +++ b/index.php @@ -3,7 +3,7 @@ /*! * metowolf BilibiliHelper * https://i-meto.com/ - * Version 0.9.0 + * Version 0.9.1 * * Copyright 2018, metowolf * Released under the MIT license @@ -11,17 +11,29 @@ require 'vendor/autoload.php'; +$plugins = [ + 'websocket', + 'auth', + 'capsule', + 'dailyBag', + 'giftSend', + 'group', + 'heart', + 'silver', + 'smallTV', + 'task', +]; + +$filename = isset($argv[1]) ? $argv[1] : 'config'; + $app = new BilibiliHelper\Lib\Helper(); +$t = $app->get('config'); +$config = $t::parse($filename); -$config = $app->get('config')::parse('config'); while (true) { - $app->get('websocket')::run($config); - $app->get('auth')::run($config); - $app->get('heart')::run($config); - $app->get('dailyBag')::run($config); - $app->get('task')::run($config); - $app->get('giftSend')::run($config); - $app->get('silver')::run($config); - $app->get('smallTV')::run($config); + foreach ($plugins as $plugin) { + $t = $app->get($plugin); + $t::run($config); + } sleep(10); } diff --git a/src/plugins/Config.php b/src/libs/Config.php similarity index 98% rename from src/plugins/Config.php rename to src/libs/Config.php index 99007ea..9ee60c5 100644 --- a/src/plugins/Config.php +++ b/src/libs/Config.php @@ -9,7 +9,7 @@ * Released under the MIT license */ -namespace BilibiliHelper\Plugin; +namespace BilibiliHelper\Lib; use Dotenv\Dotenv; diff --git a/src/libs/Helper.php b/src/libs/Helper.php index 32e9b67..fbc4885 100644 --- a/src/libs/Helper.php +++ b/src/libs/Helper.php @@ -11,14 +11,15 @@ namespace BilibiliHelper\Lib; use BilibiliHelper\Plugin\Auth; -use BilibiliHelper\Plugin\Config; +use BilibiliHelper\Plugin\Capsule; use BilibiliHelper\Plugin\DailyBag; +use BilibiliHelper\Plugin\Danmaku; use BilibiliHelper\Plugin\GiftSend; +use BilibiliHelper\Plugin\Group; use BilibiliHelper\Plugin\Heart; use BilibiliHelper\Plugin\Silver; use BilibiliHelper\Plugin\SmallTV; use BilibiliHelper\Plugin\Task; -use BilibiliHelper\Plugin\Websocket; class Helper { @@ -33,14 +34,16 @@ public function __construct() private function registerAll() { $this->set('auth', Auth::getInstance()); + $this->set('capsule', Capsule::getInstance()); $this->set('config', Config::getInstance()); - $this->set('heart', Heart::getInstance()); $this->set('dailyBag', DailyBag::getInstance()); - $this->set('task', Task::getInstance()); $this->set('giftSend', GiftSend::getInstance()); + $this->set('group', Group::getInstance()); + $this->set('heart', Heart::getInstance()); $this->set('silver', Silver::getInstance()); $this->set('smallTV', SmallTV::getInstance()); - $this->set('websocket', Websocket::getInstance()); + $this->set('task', Task::getInstance()); + $this->set('websocket', Danmaku::getInstance()); } public function get($name) diff --git a/src/libs/Log.php b/src/libs/Log.php index 9596306..52b03bd 100644 --- a/src/libs/Log.php +++ b/src/libs/Log.php @@ -88,6 +88,9 @@ public static function error($message, array $context = []) public static function callback($levelId, $level, $message) { + if (empty(static::$config['config']['CALLBACK_URL'])) { + return; + } $callback_level = intval(static::$config['config']['CALLBACK_LEVEL']); if ($levelId >= $callback_level) { $url = str_replace('{account}', self::prefix(), static::$config['config']['CALLBACK_URL']); diff --git a/src/plugins/Base.php b/src/plugins/Base.php index b25c65a..6574d3e 100644 --- a/src/plugins/Base.php +++ b/src/plugins/Base.php @@ -40,11 +40,13 @@ public static function run(&$config) protected static function config($key, $value = null) { if (!is_null($value)) { - file_put_contents(static::$config['path'], preg_replace( - '/^'.$key.'=\S*/m', - $key.'='.$value, - file_get_contents(static::$config['path']) - )); + if (!empty(static::$config['path'])) { + file_put_contents(static::$config['path'], preg_replace( + '/^'.$key.'=\S*/m', + $key.'='.$value, + file_get_contents(static::$config['path']) + )); + } static::$config['config'][$key] = $value; } return static::$config['config'][$key]; @@ -82,7 +84,6 @@ protected static function sign($payload) 'mobi_app' => 'iphone', 'platform' => 'ios', 'ts' => time(), - 'type' => 'json', ]; $payload = array_merge($payload, $default); if (isset($payload['sign'])) { diff --git a/src/plugins/Capsule.php b/src/plugins/Capsule.php new file mode 100644 index 0000000..aafd144 --- /dev/null +++ b/src/plugins/Capsule.php @@ -0,0 +1,83 @@ + time()) { + return; + } + + $count = static::info(); + $step = 100; + while ($count && $step) { + while ($count >= $step) { + $count = static::open($step); + sleep(mt_rand(0, 5)); + } + $step = intval($step / 10); + } + + static::data('lock', time() + 86400); + } + + public static function info() + { + $payload = []; + $data = Curl::post('https://api.live.bilibili.com/AppUser/capsuleInfo', static::sign($payload)); + $data = json_decode($data, true); + + if (isset($data['code']) && $data['code']) { + Log::warning("扭蛋币余额查询异常"); + return 0; + } + Log::info("当前还有 {$data['data']['normal']['coin']} 枚扭蛋币"); + + return $data['data']['normal']['coin']; + } + + public static function open($num) + { + $payload = [ + 'count' => $num, + 'type' => 'normal', + ]; + $data = Curl::get('https://api.live.bilibili.com/AppUser/capsuleInfoOpen', static::sign($payload)); + $data = json_decode($data, true); + + if (isset($data['code']) && $data['code']) { + Log::warning("扭蛋失败,稍后重试"); + return 0; + } + + if (isset($data['data']['text'])) { + foreach ($data['data']['text'] as $vo) { + Log::notice("扭蛋成功,获得 {$vo['num']} 个{$vo['name']}"); + } + } + + return isset($data['data']['coin']) ? $data['data']['coin'] : 0; + } +} diff --git a/src/plugins/Websocket.php b/src/plugins/Danmaku.php similarity index 98% rename from src/plugins/Websocket.php rename to src/plugins/Danmaku.php index 7a378dc..c59c5d3 100644 --- a/src/plugins/Websocket.php +++ b/src/plugins/Danmaku.php @@ -14,9 +14,9 @@ use BilibiliHelper\Lib\Curl; use Wrench\Client; -class Websocket extends Base +class Danmaku extends Base { - const PLUGIN_NAME = 'websocket'; + const PLUGIN_NAME = 'danmaku'; protected static function init() { diff --git a/src/plugins/Group.php b/src/plugins/Group.php new file mode 100644 index 0000000..8431c59 --- /dev/null +++ b/src/plugins/Group.php @@ -0,0 +1,87 @@ + time()) { + return; + } + + $groups = static::list(); + $count = count($groups); + foreach ($groups as $group) { + $count -= static::signIn($group); + } + + if ($count == 0) { + static::data('lock', strtotime(date('Y-m-d 23:59:59')) + 600); + } else { + static::data('lock', time() + 3600); + } + } + + public static function list() + { + $payload = []; + $data = Curl::post('https://api.vc.bilibili.com/link_group/v1/member/my_groups', static::sign($payload)); + $data = json_decode($data, true); + + if (isset($data['code']) && $data['code']) { + Log::warning("查询应援团名单异常"); + return []; + } + + if (empty($data['data']['list'])) { + Log::notice('没有需要签到的应援团'); + return []; + } + + return $data['data']['list']; + } + + public static function signIn($value) + { + $payload = [ + 'group_id' => $value['group_id'], + 'owner_id' => $value['owner_uid'], + ]; + $data = Curl::post('https://api.vc.bilibili.com/link_setting/v1/link_setting/sign_in', static::sign($payload)); + $data = json_decode($data, true); + + if (isset($data['code']) && $data['code']) { + Log::warning("应援团 {$value['group_name']} 签到异常"); + return false; + } + + if ($data['data']['status']) { + Log::notice("应援团 {$value['group_name']} 已经签到过了"); + } else { + Log::notice("应援团 {$value['group_name']} 签到成功,增加 {$de_raw['data']['add_num']} 点亲密度"); + } + + return true; + } +} diff --git a/src/plugins/SmallTV.php b/src/plugins/SmallTV.php index 255aacb..3f5087e 100644 --- a/src/plugins/SmallTV.php +++ b/src/plugins/SmallTV.php @@ -26,7 +26,7 @@ protected static function init() protected static function work() { - foreach (static::$config['data']['websocket']['smalltv'] as $tvid => $roomid) { + foreach (static::$config['data']['danmaku']['smalltv'] as $tvid => $roomid) { static::$config['data'][static::PLUGIN_NAME]['smallTV'][$tvid] = [ 'roomid' => $roomid, 'tvid' => $tvid,