From dbe91b5914dcf5de20002c9ef5b7115698035791 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 25 Dec 2024 00:42:57 +0800 Subject: [PATCH] fix[#791]: fix VipBigPoint interupt when exception --- CHANGELOG.md | 1 + .../VipBigPointAppService.cs | 271 ++++++++++-------- 2 files changed, 146 insertions(+), 126 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc29e47c1..c0b04a622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 2.1.3 - Code refactory +- Fix[#791]:修复VipBigPoint任务异常导致终止的问题 ## 2.1.2 - Feature: enhancement CICD scripts - Fix[#728]: compatible with qinglong history versions diff --git a/src/Ray.BiliBiliTool.Application/VipBigPointAppService.cs b/src/Ray.BiliBiliTool.Application/VipBigPointAppService.cs index bd8276b31..73979d46c 100644 --- a/src/Ray.BiliBiliTool.Application/VipBigPointAppService.cs +++ b/src/Ray.BiliBiliTool.Application/VipBigPointAppService.cs @@ -2,12 +2,10 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Ray.BiliBiliTool.Agent; using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos; -using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.Video; using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.ViewMall; using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.VipTask; using Ray.BiliBiliTool.Agent.BiliBiliAgent.Interfaces; @@ -22,7 +20,6 @@ namespace Ray.BiliBiliTool.Application; public class VipBigPointAppService : AppService, IVipBigPointAppService { private readonly ILogger _logger; - private readonly IConfiguration _configuration; private readonly IVipBigPointApi _vipApi; private readonly IAccountDomainService _loginDomainService; private readonly IVideoDomainService _videoDomainService; @@ -33,7 +30,6 @@ public class VipBigPointAppService : AppService, IVipBigPointAppService private readonly VipBigPointOptions _vipBigPointOptions; public VipBigPointAppService( - IConfiguration configuration, ILogger logger, IVipBigPointApi vipApi, IAccountDomainService loginDomainService, @@ -44,7 +40,6 @@ public VipBigPointAppService( IVideoApi videoApi, IOptionsMonitor vipBigPointOptions) { - _configuration = configuration; _logger = logger; _vipApi = vipApi; _loginDomainService = loginDomainService; @@ -56,58 +51,8 @@ public VipBigPointAppService( _vipBigPointOptions = vipBigPointOptions.CurrentValue; } - /// - /// 领取大会员专属经验包 - /// - public async Task VipExpress() - { - _logger.LogInformation("大会员经验领取任务开始"); - var re = await _vipApi.GetVouchersInfoAsync(); - if (re.Code == 0) - { - var state = re.Data.List.Find(x => x.Type == 9).State; - - switch (state) - { - case 2: - _logger.LogInformation("大会员经验观看任务未完成"); - _logger.LogInformation("开始观看视频"); - // 观看视频,暂时没有好办法解决,先这样使着 - DailyTaskInfo dailyTaskInfo = await _accountDomainService.GetDailyTaskStatus(); - await _videoDomainService.WatchAndShareVideo(dailyTaskInfo); - // 跳转到未兑换,执行兑换任务 - goto case 0; - - case 1: - _logger.LogInformation("大会员经验已兑换"); - break; - - case 0: - _logger.LogInformation("大会员经验未兑换"); - //兑换api - var response = await _vipApi.ObtainVipExperienceAsync(new VipExperienceRequest() - { - csrf = _biliCookie.BiliJct - }); - if (response.Code != 0) - { - _logger.LogInformation("大会员经验领取失败,错误信息:{message}", response.Message); - break; - } - - _logger.LogInformation("领取成功,经验+10 √"); - break; - - default: - _logger.LogDebug("大会员经验领取失败,未知错误"); - break; - } - } - } - - [TaskInterceptor("大会员大积分", TaskLevel.One)] - public override async Task DoTaskAsync(CancellationToken cancellationToken) + public override async Task DoTaskAsync(CancellationToken cancellationToken = default) { // TODO 解决taskInfo在一个任务出错后,后续的任务均会报空引用错误 var ui = await GetUserInfo(); @@ -157,6 +102,55 @@ public override async Task DoTaskAsync(CancellationToken cancellationToken) taskInfo.LogInfo(_logger); } + /// + /// 领取大会员专属经验包 + /// + public async Task VipExpress() + { + _logger.LogInformation("大会员经验领取任务开始"); + var re = await _vipApi.GetVouchersInfoAsync(); + if (re.Code == 0) + { + var state = re.Data.List.Find(x => x.Type == 9).State; + + switch (state) + { + case 2: + _logger.LogInformation("大会员经验观看任务未完成"); + _logger.LogInformation("开始观看视频"); + // 观看视频,暂时没有好办法解决,先这样使着 + DailyTaskInfo dailyTaskInfo = await _accountDomainService.GetDailyTaskStatus(); + await _videoDomainService.WatchAndShareVideo(dailyTaskInfo); + // 跳转到未兑换,执行兑换任务 + goto case 0; + + case 1: + _logger.LogInformation("大会员经验已兑换"); + break; + + case 0: + _logger.LogInformation("大会员经验未兑换"); + //兑换api + var response = await _vipApi.ObtainVipExperienceAsync(new VipExperienceRequest() + { + csrf = _biliCookie.BiliJct + }); + if (response.Code != 0) + { + _logger.LogInformation("大会员经验领取失败,错误信息:{message}", response.Message); + break; + } + + _logger.LogInformation("领取成功,经验+10 √"); + break; + + default: + _logger.LogDebug("大会员经验领取失败,未知错误"); + break; + } + } + } + [TaskInterceptor("测试Cookie")] private async Task GetUserInfo() { @@ -195,7 +189,16 @@ private async Task Sign(VipTaskInfo info) [TaskInterceptor("福利任务", TaskLevel.Two, false)] private async Task Bonus(VipTaskInfo info) { - var bonusTask = GetTarget(info); + const string moduleCode = "福利任务"; + const string taskCode = "bonus"; + + var bonusTask = GetTarget(info, moduleCode, taskCode); + + if (bonusTask == null) + { + _logger.LogInformation("任务失效"); + return info; + } //如果状态不等于3,则做 if (bonusTask.state == 3) @@ -220,25 +223,27 @@ private async Task Bonus(VipTaskInfo info) var infoResult = await _vipApi.GetTaskListAsync(); if (infoResult.Code != 0) throw new Exception(infoResult.ToJsonStr()); info = infoResult.Data; - bonusTask = GetTarget(info); + bonusTask = GetTarget(info, moduleCode, taskCode); _logger.LogInformation("确认:{re}", bonusTask.state == 3 && bonusTask.complete_times >= 1); } return info; - - CommonTaskItem GetTarget(VipTaskInfo info) - { - return info.Task_info.Modules.First(x => x.module_title == "福利任务") - .common_task_item - .First(x => x.task_code == "bonus"); - } } [TaskInterceptor("体验任务", TaskLevel.Two, false)] private async Task Privilege(VipTaskInfo info) { - var privilegeTask = GetTarget(info); + const string moduleCode = "体验任务"; + const string taskCode = "privilege"; + + var privilegeTask = GetTarget(info, moduleCode, taskCode); + + if (privilegeTask == null) + { + _logger.LogInformation("任务失效"); + return info; + } //如果状态不等于3,则做 if (privilegeTask.state == 3) @@ -263,27 +268,29 @@ private async Task Privilege(VipTaskInfo info) var infoResult = await _vipApi.GetTaskListAsync(); if (infoResult.Code != 0) throw new Exception(infoResult.ToJsonStr()); info = infoResult.Data; - privilegeTask = GetTarget(info); + privilegeTask = GetTarget(info, moduleCode, taskCode); _logger.LogInformation("确认:{re}", privilegeTask.state == 3 && privilegeTask.complete_times >= 1); } - CommonTaskItem GetTarget(VipTaskInfo info) - { - return info.Task_info.Modules.First(x => x.module_title == "体验任务") - .common_task_item - .First(x => x.task_code == "privilege"); - } - return info; } [TaskInterceptor("浏览追番频道页10秒", TaskLevel.Two, false)] private async Task ViewAnimate(VipTaskInfo info) { + const string moduleCode = "日常任务"; + const string taskCode = "animatetab"; + var code = "jp_channel"; - CommonTaskItem targetTask = GetTarget(info); + CommonTaskItem targetTask = GetTarget(info, moduleCode, taskCode); + + if (targetTask == null) + { + _logger.LogInformation("任务失效"); + return info; + } //如果状态不等于3,则做 if (targetTask.state == 3) @@ -308,25 +315,27 @@ private async Task ViewAnimate(VipTaskInfo info) var infoResult = await _vipApi.GetTaskListAsync(); if (infoResult.Code != 0) throw new Exception(infoResult.ToJsonStr()); info = infoResult.Data; - targetTask = GetTarget(info); + targetTask = GetTarget(info, moduleCode, taskCode); _logger.LogInformation("确认:{re}", targetTask.state == 3 && targetTask.complete_times >= 1); } - CommonTaskItem GetTarget(VipTaskInfo info) - { - return info.Task_info.Modules.First(x => x.module_title == "日常任务") - .common_task_item - .First(x => x.task_code == "animatetab"); - } - return info; } [TaskInterceptor("浏览会员购页面10秒", TaskLevel.Two, false)] private async Task ViewVipMall(VipTaskInfo info) { - CommonTaskItem targetTask = GetTarget(info); + const string moduleCode = "日常任务"; + const string taskCode = "vipmallview"; + + CommonTaskItem targetTask = GetTarget(info, moduleCode, taskCode); + + if (targetTask == null) + { + _logger.LogInformation("任务失效"); + return info; + } //如果状态不等于3,则做 if (targetTask.state == 3) @@ -355,25 +364,27 @@ private async Task ViewVipMall(VipTaskInfo info) var infoResult = await _vipApi.GetTaskListAsync(); if (infoResult.Code != 0) throw new Exception(infoResult.ToJsonStr()); info = infoResult.Data; - targetTask = GetTarget(info); + targetTask = GetTarget(info, moduleCode, taskCode); _logger.LogInformation("确认:{re}", targetTask.state == 3 && targetTask.complete_times >= 1); } - CommonTaskItem GetTarget(VipTaskInfo info) - { - return info.Task_info.Modules.First(x => x.module_title == "日常任务") - .common_task_item - .First(x => x.task_code == "vipmallview"); - } - return info; } [TaskInterceptor("观看剧集内容", TaskLevel.Two, false)] private async Task ViewVideo(VipTaskInfo info) { - CommonTaskItem targetTask = GetTarget(info); + const string moduleCode = "日常任务"; + const string taskCode = "ogvwatchnew"; + + CommonTaskItem targetTask = GetTarget(info, moduleCode, taskCode); + + if (targetTask == null) + { + _logger.LogInformation("任务失效"); + return info; + } // 如果状态不等于3,则做 if (targetTask.state == 3) @@ -394,20 +405,22 @@ private async Task ViewVideo(VipTaskInfo info) // 观看剧集内容 _logger.LogInformation("api变更,暂未实现"); - CommonTaskItem GetTarget(VipTaskInfo info) - { - return info.Task_info.Modules.First(x => x.module_title == "日常任务") - .common_task_item - .First(x => x.task_code == "ogvwatchnew"); - } - return info; } [TaskInterceptor("购买单点付费影片(仅领取)", TaskLevel.Two, false)] private async Task BuyVipVideo(VipTaskInfo info) { - CommonTaskItem targetTask = GetTarget(info); + const string moduleCode = "日常任务"; + const string taskCode = "tvodbuy"; + + CommonTaskItem targetTask = GetTarget(info, moduleCode, taskCode); + + if (targetTask == null) + { + _logger.LogInformation("任务失效"); + return info; + } if (targetTask.state is 3 or 1) { @@ -424,19 +437,21 @@ private async Task BuyVipVideo(VipTaskInfo info) } return info; - - CommonTaskItem GetTarget(VipTaskInfo info) - { - return info.Task_info.Modules.First(x => x.module_title == "日常任务") - .common_task_item - .First(x => x.task_code == "tvodbuy"); - } } [TaskInterceptor("购买指定会员购商品(仅领取)", TaskLevel.Two, false)] private async Task BuyVipMall(VipTaskInfo info) { - CommonTaskItem targetTask = GetTarget(info); + const string moduleCode = "日常任务"; + const string taskCode = "vipmallbuy"; + + CommonTaskItem targetTask = GetTarget(info, moduleCode, taskCode); + + if (targetTask == null) + { + _logger.LogInformation("任务失效"); + return info; + } if (targetTask.state is 3 or 1) { @@ -453,22 +468,23 @@ private async Task BuyVipMall(VipTaskInfo info) } return info; - - CommonTaskItem GetTarget(VipTaskInfo info) - { - return info.Task_info.Modules.First(x => x.module_title == "日常任务") - .common_task_item - .First(x => x.task_code == "vipmallbuy"); - } } [TaskInterceptor("浏览装扮商城主页", TaskLevel.Two, false)] private async Task ViewDressMall(VipTaskInfo info) { + const string moduleCode = "日常任务"; + const string taskCode = "dress-view"; + //var code = "dress-view"; - CommonTaskItem targetTask = GetTarget(info); + CommonTaskItem targetTask = GetTarget(info, moduleCode, taskCode); + if (targetTask == null) + { + _logger.LogInformation("任务失效"); + return info; + } //如果状态不等于3,则做 if (targetTask.state == 3) @@ -493,21 +509,23 @@ private async Task ViewDressMall(VipTaskInfo info) var infoResult = await _vipApi.GetTaskListAsync(); if (infoResult.Code != 0) throw new Exception(infoResult.ToJsonStr()); info = infoResult.Data; - targetTask = GetTarget(info); + targetTask = GetTarget(info, moduleCode, taskCode); _logger.LogInformation("确认:{re}", targetTask.state == 3 && targetTask.complete_times >= 1); } - CommonTaskItem GetTarget(VipTaskInfo info) - { - return info.Task_info.Modules.First(x => x.module_title == "日常任务") - .common_task_item - .First(x => x.task_code == "dress-view"); - } - return info; } + #region private + + private static CommonTaskItem GetTarget(VipTaskInfo info, string moduleCode, string taskCode) + { + var module = info.Task_info.Modules.FirstOrDefault(x => x.module_title == moduleCode); + return module? + .common_task_item + .FirstOrDefault(x => x.task_code == taskCode); + } /// /// 领取任务 @@ -548,7 +566,6 @@ private async Task Complete(string taskCode) } } - private async Task CompleteV2(string taskCode) { var request = new ReceiveOrCompleteTaskRequest(taskCode); @@ -665,4 +682,6 @@ public async Task WatchBangumi() return null; } + + #endregion }