From 8aef3e95a2f39b173d50da3bbd3df47b59291351 Mon Sep 17 00:00:00 2001 From: lulu <794329728@qq.com> Date: Thu, 3 Dec 2020 19:57:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BD=AC=E5=8F=91?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E6=8A=BD=E5=A5=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/user.conf.example | 7 ++ src/core/App.php | 1 + src/plugin/Dynamic.php | 238 ++++++++++++++++++++++++++++++++++++++++ src/plugin/Forward.php | 239 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 485 insertions(+) create mode 100644 src/plugin/Dynamic.php create mode 100644 src/plugin/Forward.php diff --git a/conf/user.conf.example b/conf/user.conf.example index df262f3..18b0697 100644 --- a/conf/user.conf.example +++ b/conf/user.conf.example @@ -79,6 +79,13 @@ COMPET_STAKE=1 # 风纪委员 USE_JUDGE=false +# 自动转发抽奖动态 +AUTO_DYNAMIC = false +# 自动取关未中奖动态 +CLEAR_DYNAMIC = false +# 强制清除抽奖组关注 +CLEAR_GROUP_FOLLOW = false + ####################### # 基础设置 # ####################### diff --git a/src/core/App.php b/src/core/App.php index 93ada16..cb1df0c 100644 --- a/src/core/App.php +++ b/src/core/App.php @@ -92,6 +92,7 @@ class App 'AnchorRaffle', 'AwardRecord', 'Statistics', + 'Forward' ]; foreach ($plugins as $plugin) { $this->newTask($plugin); diff --git a/src/plugin/Dynamic.php b/src/plugin/Dynamic.php new file mode 100644 index 0000000..c2ddd58 --- /dev/null +++ b/src/plugin/Dynamic.php @@ -0,0 +1,238 @@ + '', + 434405 => '', + 7019788 => '' + ]; + + + private static $article_list = []; + + /** + * 获取抽奖话题下的帖子 + */ + public static function getAwardTopic() + { + + foreach (self::$topic_list as $t_id => $t_name) { + $url = 'https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_new?topic_id=' . $t_id; + $data = Curl::request('get', $url); + $data = json_decode($data, true); + + // new + foreach ($data['data']['cards'] as $article) { + $article_id = $article['desc']['dynamic_id']; + $item = [ + 'uid' => $article['desc']['uid'], + 'rid' => $article['desc']['rid'], + 'did' => $article_id, + 'tm' => $article['desc']['timestamp'], + ]; + + self::$article_list[$article_id] = $item; + } +// $has_more = 0; + // more ?? + // https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_history?topic_name=转发抽奖&offset_dynamic_id=454347930068783808 + } + return self::$article_list; + } + + + /** + * 动态转发 + * @param $rid + * @param string $content + * @param int $type + * @param int $repost_code + * @param string $from + * @param string $extension + * @return bool + */ + public static function dynamicRepost($rid, $content = "", $type = 1, $repost_code = 3000, $from = "create.comment", $extension = '{"emoji_type":1}') + { + $user_info = User::parseCookies(); + $url = "https://api.vc.bilibili.com/dynamic_repost/v1/dynamic_repost/reply"; + $payload = [ + "uid" => $user_info['uid'], + "rid" => $rid, + "type" => $type, + "content" => $content, + "extension" => $extension, + "repost_code" => $repost_code, + "from" => $from, + ]; + $raw = Curl::post('app', $url, $payload); + $de_raw = json_decode($raw, true); + if ($de_raw['code'] == 0 && isset($de_raw['data'])) { + return true; + } + return false; + } + + + /** + * 发表评论 + * @param int $rid + * @param string $message + * @param int $type + * @param int $plat + * @return bool + */ + public static function dynamicReplyAdd($rid, $message = "", $type = 11, $plat = 1) + { + $user_info = User::parseCookies(); + $url = "https://api.bilibili.com/x/v2/reply/add"; + $payload = [ + "oid" => $rid, + "plat" => $plat, + "type" => $type, + "message" => $message, + "csrf" => $user_info['token'], + ]; + $raw = Curl::post('app', $url, $payload); + $de_raw = json_decode($raw, true); + if ($de_raw['code'] == 0 && isset($de_raw['data'])) { + return true; + } + return false; + } + + + /** + * 删除指定动态 + * @param $did + * @return bool + */ + public static function removeDynamic($did) + { + $user_info = User::parseCookies(); + $url = 'https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/rm_dynamic'; + $payload = [ + "dynamic_id" => $did, + "csrf_token" => $user_info['token'], + ]; + $raw = Curl::post('app', $url, $payload); + $de_raw = json_decode($raw, true); + if ($de_raw['code'] == 0 && isset($de_raw['data'])) { + return true; + } + return false; + } + + + /** + * 获取个人发布的动态 + * @param int $uid + * @return mixed + */ + public static function getMyDynamic($uid = 0) + { + $user_info = User::parseCookies(); + $uid = $uid == 0 ? $user_info['uid'] : $uid; + $url = "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history"; + $offset = ''; + $has_more = true; + $card_list = []; + while ($has_more) { + $payload = [ + "host_uid" => $uid, + "need_top" => 1, + "offset_dynamic_id" => $offset, + ]; + $raw = Curl::get('app', $url, $payload); + $de_raw = json_decode($raw, true); + $has_more = $de_raw['data']['has_more'] == 1; + if (!isset($de_raw['data']['cards'])) { + continue; + } + $card_list = array_merge($card_list, $de_raw['data']['cards']); + foreach ($de_raw['data']['cards'] as $card) { + $offset = $card['desc']['dynamic_id_str']; + } + } + return $card_list; + } + + /** + * 获取动态详情 + * @param $did + * @return mixed + */ + public static function getDynamicDetail($did) + { + $url = "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/get_dynamic_detail"; + $payload = [ + "dynamic_id" => $did, + ]; + $raw = Curl::get('app', $url, $payload); + return json_decode($raw, true); + + } + + + /** + * 获取抽奖动态信息 + * @param $did + * @return mixed + */ + public static function getLotteryNotice($did) + { + $url = 'https://api.vc.bilibili.com/lottery_svr/v1/lottery_svr/lottery_notice'; + $payload = [ + "dynamic_id" => $did, + ]; + $raw = Curl::get('app', $url, $payload); + return json_decode($raw, true); + } + + /** + * 获取个人动态TAB列表 + * @param int $uid + * @param int $type_list + * @return array|mixed + */ + public static function getDynamicTab($uid = 0, $type_list = 268435455) + { + $user_info = User::parseCookies(); + $uid = $uid == 0 ? $user_info['uid'] : $uid; + $url = "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/dynamic_new"; + $offset = ''; + $has_more = true; + $card_list = []; + while ($has_more) { + $payload = [ + "uid" => $uid, + "type_list" => $type_list, + "offset_dynamic_id" => $offset, + ]; + $raw = Curl::get('app', $url, $payload); + $de_raw = json_decode($raw, true); + if (!isset($de_raw['data']['cards'])) { + continue; + } + $card_list = $de_raw['data']['cards']; + $has_more = $de_raw['data']['has_more'] == 1; + foreach ($de_raw['data']['cards'] as $card) { + $offset = $card['desc']['dynamic_id_str']; + } + } + return $card_list; + + } +} \ No newline at end of file diff --git a/src/plugin/Forward.php b/src/plugin/Forward.php new file mode 100644 index 0000000..fc12b7e --- /dev/null +++ b/src/plugin/Forward.php @@ -0,0 +1,239 @@ + time()) { + return; + } + self::setPauseStatus(); + if (!self::start()) { + self::setLock(60 * 60); + return; + } + self::setLock(5 * 60); + } + + + public static function start() + { + + // 取关未中奖 + if (getenv('CLEAR_DYNAMIC') == 'true') { + self::clearDynamic(); + } + // 自动转发关注评论 + if (getenv('AUTO_DYNAMIC') == 'true') { + self::autoRepost(); + } + // 强制清除抽奖关注组 + if (getenv('CLEAR_GROUP_FOLLOW') == 'true') { + self::clearAllDynamic(); + self::clearFollowGroup(); + } + return true; + } + + + /** + * 自动转发抽奖 + */ + public static function autoRepost() + { + $article_list = Dynamic::getAwardTopic(); + foreach ($article_list as $did => $article) { + + if (isset(self::$already[$did])) { + //重复 + Log::info("[动态抽奖]-已转发 跳过: {$did} {$article['uid']}"); + continue; + } + // 评论 + Log::info("[动态抽奖]-评论: {$did} {$article['rid']}"); + if (Dynamic::dynamicReplyAdd($article['rid'], self::$msg)) { + // 转发 + Log::info("[动态抽奖]-转发: {$did}"); + if (Dynamic::dynamicRepost($did, self::$msg)) { + // 关注 + Log::info("[动态抽奖]-关注: {$did} {$article['uid']}"); + self::addToGroup($article['uid']); // + self::$already[$did] = 1; + } + } + sleep(1); + } + } + + + /** + * 清理无效的动态 + */ + private static function clearDynamic() + { + $dynamicList = Dynamic::getMyDynamic(); + + Log::info("[动态抽奖]-检测中奖 动态数: " . count($dynamicList)); + foreach ($dynamicList as $dynamic) { + $flag = false; + $msg = ''; + $did = $dynamic['desc']['dynamic_id']; + $card = json_decode($dynamic['card'], true); + + + if (isset($card["item"]["miss"]) && $card["item"]["miss"] == 1) { + $flag = true; + $msg = "[动态抽奖]-删除动态 源动态已删除 {$did}"; + } + if (isset($card["origin_extension"]['lott'])) { + $lott = json_decode($card["origin_extension"]["lott"], true); + if (isset($lott["lottery_time"]) && $lott["lottery_time"] <= time()) { + $flag = true; + $msg = "[动态抽奖]-删除动态 抽奖已过期 {$did}"; + } + } + if (isset($card["item"]["orig_dy_id"])) { + $ret = Dynamic::getLotteryNotice($card["item"]["orig_dy_id"]); + if (isset($ret['data']['lottery_time']) && $ret['data']['lottery_time'] <= time()) { + $flag = true; + $msg = "[动态抽奖]-删除动态 抽奖已过期 {$did}"; + } + } + +// if (isset($card['origin'])) { +// $origin = json_decode($card["origin"], true); +// if (isset($origin['item']['description'])) { +// if (isset($card["item"]['description'])) { +// $text = $origin["item"]["description"]; +// } elseif (isset($card["item"]['content'])) { +// $text = $card["item"]["content"]; +// } else { +// $text = null; +// } +// if ($text) { +// continue; +// // 关键字过滤 +// } +// } +// } + + + if ($flag) { + self::$del_dynamic[$did] = $msg; + if (isset($card['origin_user']['info']['uid'])) { + array_push(self::$un_follows, $card['origin_user']['info']['uid']); + } + } + } + + // 取关 + foreach (self::$del_dynamic as $did => $msg) { + Dynamic::removeDynamic($did); + Log::info($msg); + unset(self::$del_dynamic[$did]); + } + // 取关 + foreach (self::$un_follows as $uid) { + Log::info("[动态抽奖]-未中奖-取关 {$uid}"); + User::setUserFollow($uid, true); + } + } + + private static function clearFollowGroup() + { + $tags = User::fetchTags(); + foreach ($tags as $gid => $name) { + if (!in_array($name, ['玄不改非', '氪可改命'])) { + continue; + } + $r = User::fetchTagFollowings($gid); + foreach ($r as $uid) { + Log::info("[清除抽奖组关注] : {$uid}"); + User::setUserFollow($uid, true); + } + } + + } + + private static function clearAllDynamic() + { + $dynamicList = Dynamic::getMyDynamic(); + foreach ($dynamicList as $dynamic) { + $did = $dynamic['desc']['dynamic_id']; + $card = json_decode($dynamic['card'], true); + if (strpos($card['item']['content'], self::$msg) !== false) { + Log::info("[删除所有动态] 删除动态 {$did}"); + Dynamic::removeDynamic($did); + } + } + } + + + /** + * @use 添加分组 + * @param int $need_follow_uid + * @param int $anchor_id + * @param int $time + */ + private static function addToGroup(int $need_follow_uid, int $anchor_id = 0, int $time = 0) + { + // 获取分组id + if (is_null(self::$group_id)) { + $tags = User::fetchTags(); + $tag_id = array_search(self::$group_name, $tags); + // 如果不存在则调用创建 + self::$group_id = $tag_id ? $tag_id : User::createRelationTag(self::$group_name); + } + // 是否在关注里 + $default_follows = self::getDefaultFollows(); + if (!in_array($need_follow_uid, $default_follows)) { + User::setUserFollow($need_follow_uid); // 关注 + User::tagAddUsers($need_follow_uid, self::$group_id); // 转到分组中 + } + } + + /** + * @use 获取默认关注 + * @return array + */ + private static function getDefaultFollows() + { + if (!empty(self::$default_follows)) { + return self::$default_follows; + } + // 如果获取默认关注错误 或者 为空则补全一个 + self::$default_follows = User::fetchTagFollowings(); + if (empty(self::$default_follows)) { + array_push(self::$default_follows, 1); + } + return self::$default_follows; + } +} \ No newline at end of file From 60b79faa4f35050c3256f94b29f4c6fde63280f3 Mon Sep 17 00:00:00 2001 From: lulu <794329728@qq.com> Date: Fri, 4 Dec 2020 10:32:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B1=8F=E8=94=BD=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6ffb99d..da64cdc 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ gen /configs/ /tests/ config +/conf/user*.conf *.log Traits/ README1.md