添加转发动态抽奖逻辑

This commit is contained in:
lulu 2020-12-03 19:57:05 +08:00
parent e0cfec5aaa
commit 7dd84128b6
4 changed files with 485 additions and 0 deletions

View File

@ -79,6 +79,13 @@ COMPET_STAKE=1
# 风纪委员
USE_JUDGE=false
# 自动转发抽奖动态
AUTO_DYNAMIC = false
# 自动取关未中奖动态
CLEAR_DYNAMIC = false
# 强制清除抽奖组关注
CLEAR_GROUP_FOLLOW = false
#######################
# 基础设置 #
#######################

View File

@ -92,6 +92,7 @@ class App
'AnchorRaffle',
'AwardRecord',
'Statistics',
'Forward'
];
foreach ($plugins as $plugin) {
$this->newTask($plugin);

238
src/plugin/Dynamic.php Normal file
View File

@ -0,0 +1,238 @@
<?php
/**
* @blog http://blog.jianxiaodai.com
* @author 菜如狗怎么了
* @date 2020-12
*/
namespace BiliHelper\Plugin;
use BiliHelper\Core\Curl;
class Dynamic
{
// 228584 14027 434405 7019788 3230836
private static $topic_list = [
3230836 => '',
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;
}
}

239
src/plugin/Forward.php Normal file
View File

@ -0,0 +1,239 @@
<?php
/**
* @Blog http://blog.jianxiaodai.com
* @author 菜如狗怎么了
* @date 2020-12
*/
namespace BiliHelper\Plugin;
use BiliHelper\Core\Log;
use BiliHelper\Util\TimeLock;
class Forward
{
use TimeLock;
public static $default_follows = [];
public static $un_follows = [];
public static $del_dynamic = [];
public static $already = [];
private static $group_name = "氪可改命";
private static $group_id = null;
private static $msg = '从未中奖,从未放弃[doge]';
public static function run()
{
if (self::getLock() > 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;
}
}