diff --git a/src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/Live/GetLiveRoomInfoResponse.cs b/src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/Live/GetLiveRoomInfoResponse.cs index 9a2a42e1a..c69e5f308 100644 --- a/src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/Live/GetLiveRoomInfoResponse.cs +++ b/src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/Live/GetLiveRoomInfoResponse.cs @@ -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; } } -} +} \ No newline at end of file diff --git a/src/Ray.BiliBiliTool.Config/Options/LiveFansMedalTaskOptions.cs b/src/Ray.BiliBiliTool.Config/Options/LiveFansMedalTaskOptions.cs index 88abea4c3..0e60cc608 100644 --- a/src/Ray.BiliBiliTool.Config/Options/LiveFansMedalTaskOptions.cs +++ b/src/Ray.BiliBiliTool.Config/Options/LiveFansMedalTaskOptions.cs @@ -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; + + /// + /// 点赞次数 + /// + public int LikeNumber { get; set; } = 55; + + /// + /// 点赞发送失败多少次时放弃 + /// + public int LikeGiveUpThreshold { get; set; } = 5; + + /// + /// 发送弹幕次数 + /// + public int SendDanmakuNumber { get; set; } = 15; + /// + /// 弹幕发送失败多少次时放弃 + /// + public int SendDanmakugiveUpThreshold { get; set; } = 5; } } diff --git a/src/Ray.BiliBiliTool.DomainService/LiveDomainService.cs b/src/Ray.BiliBiliTool.DomainService/LiveDomainService.cs index 7acdc3ec3..91cc26e68 100644 --- a/src/Ray.BiliBiliTool.DomainService/LiveDomainService.cs +++ b/src/Ray.BiliBiliTool.DomainService/LiveDomainService.cs @@ -59,7 +59,6 @@ public LiveDomainService(ILogger logger, _securityOptions = securityOptions.CurrentValue; _wbiService = wbiService; _biliCookie = biliCookie; - } /// @@ -146,6 +145,7 @@ public async Task ExchangeSilver2Coin() } #region 天选时刻抽奖 + /// /// 天选抽奖 /// @@ -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 = ""; } @@ -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; @@ -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) @@ -285,6 +285,7 @@ public async Task GroupFollowing() _logger.LogInformation("未关注主播"); return; } + _logger.LogInformation("【抽奖的主播】{ups}", string.Join(",", _tianXuanFollowed.Select(x => x.Uname))); @@ -321,7 +322,6 @@ public async Task GroupFollowing() } - /// /// 获取抽奖前最后一个关注的up /// @@ -381,7 +381,8 @@ private async Task 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("创建成功"); } @@ -393,27 +394,26 @@ private async Task 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); @@ -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() @@ -448,7 +462,7 @@ public async Task SendHeartBeatToFansMedalLive() var infoList = new List(); (await GetFansMedalInfoList()).ForEach(medal => infoList.Add(new(medal.RoomId, medal.LiveRoomInfo, new(), 0, 0)) - ); + ); if (infoList.Count == 0) { @@ -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) @@ -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); } @@ -494,7 +509,7 @@ public async Task SendHeartBeatToFansMedalLive() _biliCookie.BiliJct, info.RoomInfo.Uid, $"[\"{_biliCookie.LiveBuvid}\",\"{uuid}\"]") - ); + ); } else { @@ -513,7 +528,7 @@ public async Task SendHeartBeatToFansMedalLive() _biliCookie.BiliJct, uuid, $"[\"{_biliCookie.LiveBuvid}\",\"{uuid}\"]") - ); + ); } info.LastBeatTime = Now(); @@ -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> GetFansMedalInfoList() @@ -587,11 +618,8 @@ private async Task> 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) @@ -604,11 +632,11 @@ private async Task> 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; // 获取直播间详细信息 @@ -643,13 +671,15 @@ private async Task CheckLiveCookie() // 请求主播主页来正确配置 cookie var liveHome = await _liveApi.GetLiveHome(); - var liveHomeContent = JsonConvert.DeserializeObject(await liveHome.Content.ReadAsStringAsync()); + var liveHomeContent = + JsonConvert.DeserializeObject(await liveHome.Content.ReadAsStringAsync()); if (liveHomeContent.Code != 0) { throw new Exception(liveHomeContent.Message); } - List liveCookies = liveHome.Headers.SingleOrDefault(header => header.Key == "Set-Cookie").Value.ToList(); + List liveCookies = liveHome.Headers.SingleOrDefault(header => header.Key == "Set-Cookie").Value + .ToList(); _biliCookie.MergeCurrentCookie(liveCookies); _logger.LogDebug("LiveBuvid {value}", _biliCookie.LiveBuvid); @@ -661,7 +691,8 @@ private async Task CheckLiveCookie() _logger.LogError("【原因】{message}", exception.Message); return false; } + return true; } } -} +} \ No newline at end of file