From 36ebfcadece7f052d11882d3359841c237503182 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 24 Dec 2024 23:16:05 +0800 Subject: [PATCH] fix[#806]:remove live login which was obsolete --- .../DailyTaskAppService.cs | 435 +++++++++--------- test/AppServiceTest/VipServiceTest.cs | 1 - 2 files changed, 210 insertions(+), 226 deletions(-) diff --git a/src/Ray.BiliBiliTool.Application/DailyTaskAppService.cs b/src/Ray.BiliBiliTool.Application/DailyTaskAppService.cs index f7e6ccc84..191c080f2 100644 --- a/src/Ray.BiliBiliTool.Application/DailyTaskAppService.cs +++ b/src/Ray.BiliBiliTool.Application/DailyTaskAppService.cs @@ -12,278 +12,263 @@ using Ray.BiliBiliTool.Config; using Ray.BiliBiliTool.Config.Options; using Ray.BiliBiliTool.DomainService.Interfaces; -using Ray.BiliBiliTool.Infrastructure; -using Ray.BiliBiliTool.Infrastructure.Cookie; using Ray.BiliBiliTool.Infrastructure.Enums; -namespace Ray.BiliBiliTool.Application +namespace Ray.BiliBiliTool.Application; + +public class DailyTaskAppService : AppService, IDailyTaskAppService { - public class DailyTaskAppService : AppService, IDailyTaskAppService + private readonly ILogger _logger; + private readonly IAccountDomainService _accountDomainService; + private readonly IVideoDomainService _videoDomainService; + private readonly IArticleDomainService _articleDomainService; + private readonly IDonateCoinDomainService _donateCoinDomainService; + private readonly IMangaDomainService _mangaDomainService; + private readonly ILiveDomainService _liveDomainService; + private readonly IVipPrivilegeDomainService _vipPrivilegeDomainService; + private readonly IChargeDomainService _chargeDomainService; + private readonly DailyTaskOptions _dailyTaskOptions; + private readonly ICoinDomainService _coinDomainService; + private readonly Dictionary _expDic; + private readonly ILoginDomainService _loginDomainService; + private readonly IConfiguration _configuration; + private readonly BiliCookie _biliCookie; + + public DailyTaskAppService( + ILogger logger, + IOptionsMonitor> dicOptions, + IAccountDomainService accountDomainService, + IVideoDomainService videoDomainService, + IArticleDomainService articleDomainService, + IDonateCoinDomainService donateCoinDomainService, + IMangaDomainService mangaDomainService, + ILiveDomainService liveDomainService, + IVipPrivilegeDomainService vipPrivilegeDomainService, + IChargeDomainService chargeDomainService, + IOptionsMonitor dailyTaskOptions, + ICoinDomainService coinDomainService, + ILoginDomainService loginDomainService, + IConfiguration configuration, + BiliCookie biliCookie) { - private readonly ILogger _logger; - private readonly IAccountDomainService _accountDomainService; - private readonly IVideoDomainService _videoDomainService; - private readonly IArticleDomainService _articleDomainService; - private readonly IDonateCoinDomainService _donateCoinDomainService; - private readonly IMangaDomainService _mangaDomainService; - private readonly ILiveDomainService _liveDomainService; - private readonly IVipPrivilegeDomainService _vipPrivilegeDomainService; - private readonly IChargeDomainService _chargeDomainService; - private readonly DailyTaskOptions _dailyTaskOptions; - private readonly ICoinDomainService _coinDomainService; - private readonly Dictionary _expDic; - private readonly ILoginDomainService _loginDomainService; - private readonly IConfiguration _configuration; - private readonly CookieStrFactory _cookieStrFactory; - private BiliCookie _biliCookie; - - public DailyTaskAppService( - ILogger logger, - IOptionsMonitor> dicOptions, - IAccountDomainService accountDomainService, - IVideoDomainService videoDomainService, - IArticleDomainService articleDomainService, - IDonateCoinDomainService donateCoinDomainService, - IMangaDomainService mangaDomainService, - ILiveDomainService liveDomainService, - IVipPrivilegeDomainService vipPrivilegeDomainService, - IChargeDomainService chargeDomainService, - IOptionsMonitor dailyTaskOptions, - ICoinDomainService coinDomainService, - ILoginDomainService loginDomainService, IConfiguration configuration, - CookieStrFactory cookieStrFactory, - BiliCookie biliCookie) - { - _logger = logger; - _expDic = dicOptions.Get(Constants.OptionsNames.ExpDictionaryName); - _accountDomainService = accountDomainService; - _videoDomainService = videoDomainService; - _articleDomainService = articleDomainService; - _donateCoinDomainService = donateCoinDomainService; - _mangaDomainService = mangaDomainService; - _liveDomainService = liveDomainService; - _vipPrivilegeDomainService = vipPrivilegeDomainService; - _chargeDomainService = chargeDomainService; - _dailyTaskOptions = dailyTaskOptions.CurrentValue; - _coinDomainService = coinDomainService; - _loginDomainService = loginDomainService; - _configuration = configuration; - _cookieStrFactory = cookieStrFactory; - _biliCookie = biliCookie; - } + _logger = logger; + _expDic = dicOptions.Get(Constants.OptionsNames.ExpDictionaryName); + _accountDomainService = accountDomainService; + _videoDomainService = videoDomainService; + _articleDomainService = articleDomainService; + _donateCoinDomainService = donateCoinDomainService; + _mangaDomainService = mangaDomainService; + _liveDomainService = liveDomainService; + _vipPrivilegeDomainService = vipPrivilegeDomainService; + _chargeDomainService = chargeDomainService; + _dailyTaskOptions = dailyTaskOptions.CurrentValue; + _coinDomainService = coinDomainService; + _loginDomainService = loginDomainService; + _configuration = configuration; + _biliCookie = biliCookie; + } - [TaskInterceptor("每日任务", TaskLevel.One)] - public override async Task DoTaskAsync(CancellationToken cancellationToken) - { - await SetCookiesAsync(_biliCookie, cancellationToken); + [TaskInterceptor("每日任务", TaskLevel.One)] + public override async Task DoTaskAsync(CancellationToken cancellationToken = default) + { + await SetCookiesAsync(_biliCookie, cancellationToken); - //每日任务赚经验: - UserInfo userInfo = await Login(); + //每日任务赚经验: + UserInfo userInfo = await Login(); - DailyTaskInfo dailyTaskInfo = await GetDailyTaskStatus(); - await WatchAndShareVideo(dailyTaskInfo); + DailyTaskInfo dailyTaskInfo = await GetDailyTaskStatus(); + await WatchAndShareVideo(dailyTaskInfo); - await AddCoins(userInfo); + await AddCoins(userInfo); - //签到: - await LiveSign(); - await MangaSign(); - await MangaRead(); - await ExchangeSilver2Coin(); + //签到: + await MangaSign(); + await MangaRead(); + await ExchangeSilver2Coin(); - //领福利: - await ReceiveVipPrivilege(userInfo); - await ReceiveMangaVipReward(userInfo); + //领福利: + await ReceiveVipPrivilege(userInfo); + await ReceiveMangaVipReward(userInfo); - await Charge(userInfo); - } + await Charge(userInfo); + } - [TaskInterceptor("Set Cookie", TaskLevel.Two)] - protected async Task SetCookiesAsync(BiliCookie biliCookie, CancellationToken cancellationToken) + [TaskInterceptor("Set Cookie", TaskLevel.Two)] + protected async Task SetCookiesAsync(BiliCookie biliCookie, CancellationToken cancellationToken) + { + //判断cookie是否完整 + if (biliCookie.Buvid.IsNotNullOrEmpty()) { - //判断cookie是否完整 - if (biliCookie.Buvid.IsNotNullOrEmpty()) - { - _logger.LogInformation("Cookie完整,不需要Set Cookie"); - return biliCookie; - } + _logger.LogInformation("Cookie完整,不需要Set Cookie"); + return biliCookie; + } - //Set - _logger.LogInformation("开始Set Cookie"); - var ck = await _loginDomainService.SetCookieAsync(biliCookie, cancellationToken); + //Set + _logger.LogInformation("开始Set Cookie"); + var ck = await _loginDomainService.SetCookieAsync(biliCookie, cancellationToken); - //持久化 - _logger.LogInformation("持久化Cookie"); - await SaveCookieAsync(ck, cancellationToken); + //持久化 + _logger.LogInformation("持久化Cookie"); + await SaveCookieAsync(ck, cancellationToken); - return ck; - } + return ck; + } - /// - /// 登录 - /// - /// - [TaskInterceptor("登录")] - private async Task Login() - { - UserInfo userInfo = await _accountDomainService.LoginByCookie(); - if (userInfo == null) throw new Exception("登录失败,请检查Cookie"); //终止流程 + /// + /// 登录 + /// + /// + [TaskInterceptor("登录")] + private async Task Login() + { + UserInfo userInfo = await _accountDomainService.LoginByCookie(); + if (userInfo == null) throw new Exception("登录失败,请检查Cookie"); //终止流程 - _expDic.TryGetValue("每日登录", out int exp); - _logger.LogInformation("登录成功,经验+{exp} √", exp); + _expDic.TryGetValue("每日登录", out int exp); + _logger.LogInformation("登录成功,经验+{exp} √", exp); - return userInfo; - } + return userInfo; + } - /// - /// 获取任务完成情况 - /// - /// - [TaskInterceptor(null, rethrowWhenException: false)] - private async Task GetDailyTaskStatus() + /// + /// 获取任务完成情况 + /// + /// + [TaskInterceptor(null, rethrowWhenException: false)] + private async Task GetDailyTaskStatus() + { + return await _accountDomainService.GetDailyTaskStatus(); + } + + /// + /// 观看、分享视频 + /// + [TaskInterceptor("观看、分享视频", rethrowWhenException: false)] + private async Task WatchAndShareVideo(DailyTaskInfo dailyTaskInfo) + { + if (!_dailyTaskOptions.IsWatchVideo && !_dailyTaskOptions.IsShareVideo) { - return await _accountDomainService.GetDailyTaskStatus(); + _logger.LogInformation("已配置为关闭,跳过任务"); + return; } - /// - /// 观看、分享视频 - /// - [TaskInterceptor("观看、分享视频", rethrowWhenException: false)] - private async Task WatchAndShareVideo(DailyTaskInfo dailyTaskInfo) - { - if (!_dailyTaskOptions.IsWatchVideo && !_dailyTaskOptions.IsShareVideo) - { - _logger.LogInformation("已配置为关闭,跳过任务"); - return; - } + await _videoDomainService.WatchAndShareVideo(dailyTaskInfo); + } - await _videoDomainService.WatchAndShareVideo(dailyTaskInfo); + /// + /// 投币任务 + /// + [TaskInterceptor("投币", rethrowWhenException: false)] + private async Task AddCoins(UserInfo userInfo) + { + if (_dailyTaskOptions.SaveCoinsWhenLv6 && userInfo.Level_info.Current_level >= 6) + { + _logger.LogInformation("已经为LV6大佬,开始白嫖"); + return; } - /// - /// 投币任务 - /// - [TaskInterceptor("投币", rethrowWhenException: false)] - private async Task AddCoins(UserInfo userInfo) + if (_dailyTaskOptions.IsDonateCoinForArticle) { - if (_dailyTaskOptions.SaveCoinsWhenLv6 && userInfo.Level_info.Current_level >= 6) - { - _logger.LogInformation("已经为LV6大佬,开始白嫖"); - return; - } + _logger.LogInformation("专栏投币已开启"); - if (_dailyTaskOptions.IsDonateCoinForArticle) - { - _logger.LogInformation("专栏投币已开启"); - - if (!await _articleDomainService.AddCoinForArticles()) - { - _logger.LogInformation("专栏投币结束,转入视频投币"); - await _donateCoinDomainService.AddCoinsForVideos(); - } - } - else + if (!await _articleDomainService.AddCoinForArticles()) { + _logger.LogInformation("专栏投币结束,转入视频投币"); await _donateCoinDomainService.AddCoinsForVideos(); } } - - /// - /// 直播中心签到 - /// - [TaskInterceptor("直播签到", rethrowWhenException: false)] - private async Task LiveSign() + else { - await _liveDomainService.LiveSign(); + await _donateCoinDomainService.AddCoinsForVideos(); } + } - /// - /// 直播中心的银瓜子兑换硬币 - /// - [TaskInterceptor("银瓜子兑换硬币", rethrowWhenException: false)] - private async Task ExchangeSilver2Coin() - { - var success = await _liveDomainService.ExchangeSilver2Coin(); - if (!success) return; + /// + /// 直播中心的银瓜子兑换硬币 + /// + [TaskInterceptor("银瓜子兑换硬币", rethrowWhenException: false)] + private async Task ExchangeSilver2Coin() + { + var success = await _liveDomainService.ExchangeSilver2Coin(); + if (!success) return; - //如果兑换成功,则打印硬币余额 - var coinBalance = _coinDomainService.GetCoinBalance(); - _logger.LogInformation("【硬币余额】 {coin}", coinBalance); - } + //如果兑换成功,则打印硬币余额 + var coinBalance = _coinDomainService.GetCoinBalance(); + _logger.LogInformation("【硬币余额】 {coin}", coinBalance); + } - /// - /// 每月领取大会员福利 - /// - [TaskInterceptor("领取大会员福利", rethrowWhenException: false)] - private async Task ReceiveVipPrivilege(UserInfo userInfo) - { - var suc = await _vipPrivilegeDomainService.ReceiveVipPrivilege(userInfo); + /// + /// 每月领取大会员福利 + /// + [TaskInterceptor("领取大会员福利", rethrowWhenException: false)] + private async Task ReceiveVipPrivilege(UserInfo userInfo) + { + var suc = await _vipPrivilegeDomainService.ReceiveVipPrivilege(userInfo); - //如果领取成功,需要刷新账户信息(比如B币余额) - if (suc) + //如果领取成功,需要刷新账户信息(比如B币余额) + if (suc) + { + try { - try - { - userInfo = await _accountDomainService.LoginByCookie(); - } - catch (Exception ex) - { - _logger.LogError("领取福利成功,但之后刷新用户信息时异常,信息:{msg}", ex.Message); - } + userInfo = await _accountDomainService.LoginByCookie(); + } + catch (Exception ex) + { + _logger.LogError("领取福利成功,但之后刷新用户信息时异常,信息:{msg}", ex.Message); } } + } - /// - /// 每月为自己充电 - /// - [TaskInterceptor("B币券充电", rethrowWhenException: false)] - private async Task Charge(UserInfo userInfo) - { - await _chargeDomainService.Charge(userInfo); - } - - /// - /// 漫画签到 - /// - [TaskInterceptor("漫画签到", rethrowWhenException: false)] - private async Task MangaSign() - { - await _mangaDomainService.MangaSign(); - } + /// + /// 每月为自己充电 + /// + [TaskInterceptor("B币券充电", rethrowWhenException: false)] + private async Task Charge(UserInfo userInfo) + { + await _chargeDomainService.Charge(userInfo); + } - /// - /// 漫画阅读 - /// - [TaskInterceptor("漫画阅读", rethrowWhenException: false)] - private async Task MangaRead() - { - await _mangaDomainService.MangaRead(); - } + /// + /// 漫画签到 + /// + [TaskInterceptor("漫画签到", rethrowWhenException: false)] + private async Task MangaSign() + { + await _mangaDomainService.MangaSign(); + } - /// - /// 每月获取大会员漫画权益 - /// - [TaskInterceptor("领取大会员漫画权益", rethrowWhenException: false)] - private async Task ReceiveMangaVipReward(UserInfo userInfo) - { - await _mangaDomainService.ReceiveMangaVipReward(1, userInfo); - } + /// + /// 漫画阅读 + /// + [TaskInterceptor("漫画阅读", rethrowWhenException: false)] + private async Task MangaRead() + { + await _mangaDomainService.MangaRead(); + } - private async Task SaveCookieAsync(BiliCookie ckInfo, CancellationToken cancellationToken) - { - var platformType = _configuration.GetSection("PlateformType").Get(); - _logger.LogInformation("当前运行平台:{platform}", platformType); + /// + /// 每月获取大会员漫画权益 + /// + [TaskInterceptor("领取大会员漫画权益", rethrowWhenException: false)] + private async Task ReceiveMangaVipReward(UserInfo userInfo) + { + await _mangaDomainService.ReceiveMangaVipReward(1, userInfo); + } - //更新cookie到青龙env - if (platformType == PlatformType.QingLong) - { - await _loginDomainService.SaveCookieToQinLongAsync(ckInfo, cancellationToken); - return; - } + private async Task SaveCookieAsync(BiliCookie ckInfo, CancellationToken cancellationToken) + { + var platformType = _configuration.GetSection("PlateformType").Get(); + _logger.LogInformation("当前运行平台:{platform}", platformType); - //更新cookie到json - await _loginDomainService.SaveCookieToJsonFileAsync(ckInfo, cancellationToken); + //更新cookie到青龙env + if (platformType == PlatformType.QingLong) + { + await _loginDomainService.SaveCookieToQinLongAsync(ckInfo, cancellationToken); + return; } + + //更新cookie到json + await _loginDomainService.SaveCookieToJsonFileAsync(ckInfo, cancellationToken); } -} +} \ No newline at end of file diff --git a/test/AppServiceTest/VipServiceTest.cs b/test/AppServiceTest/VipServiceTest.cs index ca5da50ce..216aea8a9 100644 --- a/test/AppServiceTest/VipServiceTest.cs +++ b/test/AppServiceTest/VipServiceTest.cs @@ -28,7 +28,6 @@ public async Task WatchVideo() using var scope = Global.ServiceProviderRoot.CreateScope(); var appService = scope.ServiceProvider.GetRequiredService(); var res = await appService.WatchBangumi(); - Assert.True(res); }