Skip to content

Commit

Permalink
Merge pull request #752 from Polaris-cn10086/develop
Browse files Browse the repository at this point in the history
直播亲密度获取规则修改
  • Loading branch information
RayWangQvQ authored Jul 28, 2024
2 parents 6feacf7 + 6161d06 commit 7836737
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class GetLiveRoomInfoResponse

public long Parent_area_id { get; set; }

public int Live_Status { get; set; }

public long Uid { get; set; }
}
}
}
19 changes: 19 additions & 0 deletions src/Ray.BiliBiliTool.Config/Options/LiveFansMedalTaskOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,24 @@ public class LiveFansMedalTaskOptions
//public const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36";

public const int HeartBeatInterval = 60;

/// <summary>
/// 点赞次数
/// </summary>
public int LikeNumber { get; set; } = 55;

/// <summary>
/// 点赞发送失败多少次时放弃
/// </summary>
public int LikeGiveUpThreshold { get; set; } = 5;

/// <summary>
/// 发送弹幕次数
/// </summary>
public int SendDanmakuNumber { get; set; } = 15;
/// <summary>
/// 弹幕发送失败多少次时放弃
/// </summary>
public int SendDanmakugiveUpThreshold { get; set; } = 5;
}
}
133 changes: 82 additions & 51 deletions src/Ray.BiliBiliTool.DomainService/LiveDomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public LiveDomainService(ILogger<LiveDomainService> logger,
_securityOptions = securityOptions.CurrentValue;
_wbiService = wbiService;
_biliCookie = biliCookie;

}

/// <summary>
Expand Down Expand Up @@ -146,6 +145,7 @@ public async Task<bool> ExchangeSilver2Coin()
}

#region 天选时刻抽奖

/// <summary>
/// 天选抽奖
/// </summary>
Expand Down Expand Up @@ -183,9 +183,11 @@ public async Task TianXuan()

await TryJoinTianXuan(item);
}

if (reData.Has_more != 1) break;
defaultSort = reData.New_tags.FirstOrDefault()?.Sort_type ?? "";
}

defaultSort = "";
}

Expand Down Expand Up @@ -223,7 +225,8 @@ public async Task TryJoinTianXuan(ListItemDto target)
}

//根据配置过滤
if (!check.AwardNameIsSatisfied(_liveLotteryTaskOptions.IncludeAwardNameList, _liveLotteryTaskOptions.ExcludeAwardNameList))
if (!check.AwardNameIsSatisfied(_liveLotteryTaskOptions.IncludeAwardNameList,
_liveLotteryTaskOptions.ExcludeAwardNameList))
{
_logger.LogDebug("不满足配置的筛选条件,跳过{newLine}", Environment.NewLine);
return;
Expand Down Expand Up @@ -251,10 +254,7 @@ public async Task TryJoinTianXuan(ListItemDto target)

var request = new JoinTianXuanRequest
{
Id = check.Id,
Gift_id = check.Gift_id,
Gift_num = check.Gift_num,
Csrf = _biliCookie.BiliJct
Id = check.Id, Gift_id = check.Gift_id, Gift_num = check.Gift_num, Csrf = _biliCookie.BiliJct
};
var re = await _liveApi.Join(request);
if (re.Code == 0)
Expand Down Expand Up @@ -285,6 +285,7 @@ public async Task GroupFollowing()
_logger.LogInformation("未关注主播");
return;
}

_logger.LogInformation("【抽奖的主播】{ups}",
string.Join(",", _tianXuanFollowed.Select(x => x.Uname)));

Expand Down Expand Up @@ -321,7 +322,6 @@ public async Task GroupFollowing()
}



/// <summary>
/// 获取抽奖前最后一个关注的up
/// </summary>
Expand Down Expand Up @@ -381,7 +381,8 @@ private async Task<long> GetOrCreateTianXuanGroupId()
{
_logger.LogInformation("“天选时刻”分组不存在,尝试创建...");
//创建一个
var createRe = await _relationApi.CreateTag(new CreateTagRequest { Tag = "天选时刻", Csrf = _biliCookie.BiliJct });
var createRe =
await _relationApi.CreateTag(new CreateTagRequest { Tag = "天选时刻", Csrf = _biliCookie.BiliJct });
groupId = createRe.Data.Tagid;
_logger.LogInformation("创建成功");
}
Expand All @@ -393,27 +394,26 @@ private async Task<long> GetOrCreateTianXuanGroupId()

return groupId;
}

#endregion

public async Task SendDanmakuToFansMedalLive()
{
if (!await CheckLiveCookie()) return;

(await GetFansMedalInfoList()).ForEach(async info =>
var infoList = await GetFansMedalInfoList();

foreach (var info in infoList)
{
var medal = info.MedalInfo;

_logger.LogInformation("【直播间】{liveRoomName}", medal.Target_name);
_logger.LogInformation("【粉丝牌】{medalName}", medal.Medal_info.Medal_name);

_logger.LogInformation("正在发送弹幕...");

// 通过空间主页信息获取直播间 id
var liveHostUserId = medal.Medal_info.Target_id;
var req = new GetSpaceInfoDto()
{
mid = liveHostUserId
};
var req = new GetSpaceInfoDto() { mid = liveHostUserId };
await _wbiService.SetWridAsync(req);

var spaceInfo = await _userInfoApi.GetSpaceInfo(req);
Expand All @@ -424,21 +424,35 @@ public async Task SendDanmakuToFansMedalLive()
return;
}

var successCount = 0;
var failedCount = 0;

// 发送弹幕
var sendResult = await _liveApi.SendLiveDanmuku(new SendLiveDanmukuRequest(
_biliCookie.BiliJct,
spaceInfo.Data.Live_room.Roomid,
_liveFansMedalTaskOptions.DanmakuContent));

if (sendResult.Code != 0)
while (successCount < _liveFansMedalTaskOptions.SendDanmakuNumber &&
failedCount < _liveFansMedalTaskOptions.SendDanmakugiveUpThreshold)
{
_logger.LogError("【弹幕发送】失败");
_logger.LogError("【原因】{message}", sendResult.Message);
return;
await Task.Delay(2000);

var sendResult = await _liveApi.SendLiveDanmuku(new SendLiveDanmukuRequest(
_biliCookie.BiliJct,
spaceInfo.Data.Live_room.Roomid,
_liveFansMedalTaskOptions.DanmakuContent));

if (sendResult.Code != 0)
{
_logger.LogError("【弹幕发送】失败");
_logger.LogError("【原因】{message}", sendResult.Message);
failedCount++;
}
else
successCount++;
}

_logger.LogInformation("【弹幕发送】成功~,你和主播 {name} 的亲密值增加了100!", spaceInfo.Data.Name);
});

_logger.LogInformation("【弹幕发送】发送情况:你向主播 {name} 发送弹幕{success}/{total}", spaceInfo.Data.Name,
successCount, successCount + failedCount);
}
}

public async Task SendHeartBeatToFansMedalLive()
Expand All @@ -448,7 +462,7 @@ public async Task SendHeartBeatToFansMedalLive()
var infoList = new List<HeartBeatIterationInfoDto>();
(await GetFansMedalInfoList()).ForEach(medal =>
infoList.Add(new(medal.RoomId, medal.LiveRoomInfo, new(), 0, 0))
);
);

if (infoList.Count == 0)
{
Expand All @@ -460,8 +474,8 @@ public async Task SendHeartBeatToFansMedalLive()

while (infoList.Min(
info => info.FailedTimes >= _liveFansMedalTaskOptions.HeartBeatSendGiveUpThreshold
? int.MaxValue :
info.HeartBeatCount)
? int.MaxValue
: info.HeartBeatCount)
< _liveFansMedalTaskOptions.HeartBeatNumber)
{
foreach (var info in infoList)
Expand All @@ -473,7 +487,8 @@ public async Task SendHeartBeatToFansMedalLive()
var current = Now();
if (current - info.LastBeatTime <= (LiveFansMedalTaskOptions.HeartBeatInterval + 5) * 1000)
{
int sleepTime = (int)((LiveFansMedalTaskOptions.HeartBeatInterval + 5) * 1000 - (current - info.LastBeatTime));
int sleepTime = (int)((LiveFansMedalTaskOptions.HeartBeatInterval + 5) * 1000 -
(current - info.LastBeatTime));
_logger.LogDebug("【休眠】{time} 毫秒", sleepTime);
Thread.Sleep(sleepTime);
}
Expand All @@ -494,7 +509,7 @@ public async Task SendHeartBeatToFansMedalLive()
_biliCookie.BiliJct,
info.RoomInfo.Uid,
$"[\"{_biliCookie.LiveBuvid}\",\"{uuid}\"]")
);
);
}
else
{
Expand All @@ -513,7 +528,7 @@ public async Task SendHeartBeatToFansMedalLive()
_biliCookie.BiliJct,
uuid,
$"[\"{_biliCookie.LiveBuvid}\",\"{uuid}\"]")
);
);
}

info.LastBeatTime = Now();
Expand Down Expand Up @@ -548,19 +563,35 @@ public async Task LikeFansMedalLive()
{
if (!await CheckLiveCookie()) return;

(await GetFansMedalInfoList()).ForEach(async info =>
var infoList = await GetFansMedalInfoList();
infoList = infoList.FindAll(info => info.LiveRoomInfo.Live_Status != 1);

foreach (var info in infoList)
{
var result = await _liveApi.LikeLiveRoom(new LikeLiveRoomRequest(info.RoomId, _biliCookie.BiliJct));
if (result.Code == 0)
var successCount = 0;
var failedCount = 0;
while (successCount < _liveFansMedalTaskOptions.LikeNumber &&
failedCount < _liveFansMedalTaskOptions.LikeGiveUpThreshold)
{
_logger.LogInformation("【点赞直播间】{roomId} 完成", info.RoomId);
}
else
{
_logger.LogError("【点赞直播间】{roomId} 时候出现错误", info.RoomId);
_logger.LogError("【原因】{message}", result.Message);
var result = await _liveApi.LikeLiveRoom(new LikeLiveRoomRequest(info.RoomId, _biliCookie.BiliJct));
if (result.Code == 0)
{
// _logger.LogInformation("【点赞直播间】{roomId} 完成", info.RoomId);
successCount++;
}
else
{
_logger.LogError("【点赞直播间】{roomId} 时候出现错误", info.RoomId);
_logger.LogError("【原因】{message}", result.Message);
failedCount++;
}

await Task.Delay(500);
}
});

_logger.LogInformation("【点赞直播间】{romeId}完成情况:{success}/{total}", info.RoomId, successCount,
failedCount + successCount);
}
}

private async Task<List<FansMedalInfoDto>> GetFansMedalInfoList()
Expand All @@ -587,11 +618,8 @@ private async Task<List<FansMedalInfoDto>> GetFansMedalInfoList()

// 通过空间主页信息获取直播间 id
var liveHostUserId = medal.Medal_info.Target_id;
var req = new GetSpaceInfoDto()
{
mid = liveHostUserId
};
await _wbiService.SetWridAsync(req);
var req = new GetSpaceInfoDto() { mid = liveHostUserId };
await _wbiService.SetWridAsync(req);

var spaceInfo = await _userInfoApi.GetSpaceInfo(req);
if (spaceInfo.Code != 0)
Expand All @@ -604,11 +632,11 @@ private async Task<List<FansMedalInfoDto>> GetFansMedalInfoList()
// 用以排除有牌子无直播间的up主
if (spaceInfo.Data.Live_room is null)
{
_logger.LogInformation("【主播】{name} 直播间id获取失败,已跳过",medal.Target_name);
_logger.LogInformation("【主播】{name} 直播间id获取失败,已跳过", medal.Target_name);
continue;
}


var roomId = spaceInfo.Data.Live_room.Roomid;

// 获取直播间详细信息
Expand Down Expand Up @@ -643,13 +671,15 @@ private async Task<bool> CheckLiveCookie()

// 请求主播主页来正确配置 cookie
var liveHome = await _liveApi.GetLiveHome();
var liveHomeContent = JsonConvert.DeserializeObject<BiliApiResponse>(await liveHome.Content.ReadAsStringAsync());
var liveHomeContent =
JsonConvert.DeserializeObject<BiliApiResponse>(await liveHome.Content.ReadAsStringAsync());
if (liveHomeContent.Code != 0)
{
throw new Exception(liveHomeContent.Message);
}

List<string> liveCookies = liveHome.Headers.SingleOrDefault(header => header.Key == "Set-Cookie").Value.ToList();
List<string> liveCookies = liveHome.Headers.SingleOrDefault(header => header.Key == "Set-Cookie").Value
.ToList();
_biliCookie.MergeCurrentCookie(liveCookies);

_logger.LogDebug("LiveBuvid {value}", _biliCookie.LiveBuvid);
Expand All @@ -661,7 +691,8 @@ private async Task<bool> CheckLiveCookie()
_logger.LogError("【原因】{message}", exception.Message);
return false;
}

return true;
}
}
}
}

0 comments on commit 7836737

Please sign in to comment.