From 4132b044fba523668856a148f7857099e4e266bf Mon Sep 17 00:00:00 2001 From: RayWang <2271272653@qq.com> Date: Wed, 16 Dec 2020 12:33:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A7=86=E9=A2=91=E9=A2=86?= =?UTF-8?q?=E5=9F=9F=E6=9C=8D=E5=8A=A1=E9=80=89=E5=8F=96=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clean.cmd | 12 ++ .../DonateCoinDomainService.cs | 112 +++++---------- .../Interfaces/IDonateCoinDomainService.cs | 2 + .../Interfaces/IVideoDomainService.cs | 21 ++- .../VideoDomainService.cs | 129 +++++++++--------- test/DailyTaskTest/AddCoinsForVideo.cs | 4 +- 6 files changed, 124 insertions(+), 156 deletions(-) create mode 100644 clean.cmd diff --git a/clean.cmd b/clean.cmd new file mode 100644 index 0000000..4af8558 --- /dev/null +++ b/clean.cmd @@ -0,0 +1,12 @@ +@echo off + +REM start to clean +echo Start to clean all bin and obj folder under Noodle repo +@echo on +@for /d /r %%c in (obj) do @if exist "%%c" (@rd /s /q "%%c" & echo Delete %%c) +@for /d /r %%c in (bin) do @if exist "%%c" (@rd /s /q "%%c" & echo Delete %%c) +@for /d /r %%c in (packages) do @if exist "%%c" (@rd /s /q "%%c" & echo Delete %%c) +@for /d /r %%c in (.vs) do @if exist "%%c" (@rd /s /q "%%c" & echo Delete %%c) +@for /d /r %%c in (temp) do @if exist "%%c" (@rd /s /q "%%c" & echo Delete %%c) +@echo off +pause \ No newline at end of file diff --git a/src/Ray.BiliBiliTool.DomainService/DonateCoinDomainService.cs b/src/Ray.BiliBiliTool.DomainService/DonateCoinDomainService.cs index 60bb0dd..93a8766 100644 --- a/src/Ray.BiliBiliTool.DomainService/DonateCoinDomainService.cs +++ b/src/Ray.BiliBiliTool.DomainService/DonateCoinDomainService.cs @@ -25,11 +25,11 @@ public class DonateCoinDomainService : IDonateCoinDomainService private readonly IVideoDomainService _videoDomainService; private readonly IRelationApi _relationApi; - private Dictionary _alreadyDonatedCoinsCatch = new Dictionary(); + private readonly Dictionary _alreadyDonatedCoinsCatch = new Dictionary(); public DonateCoinDomainService(ILogger logger, IDailyTaskApi dailyTaskApi, - IOptionsMonitor biliBiliCookieOptions, + IOptionsMonitor cookieOptions, IOptionsMonitor dailyTaskOptions, IAccountApi accountApi, ICoinDomainService coinDomainService, @@ -38,7 +38,7 @@ public DonateCoinDomainService(ILogger logger, { _logger = logger; _dailyTaskApi = dailyTaskApi; - _biliBiliCookieOptions = biliBiliCookieOptions.CurrentValue; + _biliBiliCookieOptions = cookieOptions.CurrentValue; _dailyTaskOptions = dailyTaskOptions.CurrentValue; _accountApi = accountApi; _coinDomainService = coinDomainService; @@ -114,6 +114,35 @@ public Tuple TryGetCanDonatedVideo() return result; } + /// + /// 为视频投币 + /// + /// av号 + /// 投币数量 + /// 是否同时点赞 1是0否 + /// 是否投币成功 + public bool DoAddCoinForVideo(string aid, int multiply, bool select_like, string title = "") + { + BiliApiResponse result = _dailyTaskApi.AddCoinForVideo(aid, multiply, select_like ? 1 : 0, _biliBiliCookieOptions.BiliJct).Result; + + if (result.Code == 0) + { + _logger.LogInformation("为“{title}”投币成功", title); + return true; + } + + if (result.Code == -111) + { + string errorMsg = $"投币异常,Cookie配置项[BiliJct]错误或已过期,请检查并更新。接口返回:{result.Message}"; + _logger.LogError(errorMsg); + throw new Exception(errorMsg); + } + else + { + _logger.LogInformation("为“{title}”投币失败,原因:{msg}", title, result.Message); + return false; + } + } #region private @@ -166,9 +195,6 @@ private Tuple TryGetCanDonatedVideoByUp(int tryCount) /// private Tuple TryGetCanDonatedVideoBySpecialUps(int tryCount) { - //缓存每个up的视频总数 - var videoCountDic = new Dictionary(); - //获取特别关注列表 BiliApiResponse> specials = _relationApi.GetSpecialFollowings().Result; if (specials.Data == null || specials.Data.Count == 0) return null; @@ -177,15 +203,12 @@ private Tuple TryGetCanDonatedVideoBySpecialUps(int tryCount) } /// - /// 尝试从特别关注的Up主中随机获取一个可以投币的视频 + /// 尝试从普通关注的Up主中随机获取一个可以投币的视频 /// /// /// private Tuple TryGetCanDonatedVideoByFollowingUps(int tryCount) { - //缓存每个up的视频总数 - Dictionary videoCountDic = new Dictionary(); - //获取特别关注列表 BiliApiResponse result = _relationApi.GetFollowings(_biliBiliCookieOptions.UserId).Result; if (result.Data.Total == 0) return null; @@ -235,12 +258,12 @@ private Tuple TryGetCanDonateVideoByUps(List upIds, int tr //该up的视频总数 if (!videoCountDic.TryGetValue(randomUpId, out int videoCount)) { - videoCount = GetVidoeCountOfUp(randomUpId); + videoCount = _videoDomainService.GetVideoCountOfUp(randomUpId); videoCountDic.Add(randomUpId, videoCount); } if (videoCount == 0 | videoCount < i) continue; - UpVideoInfo videoInfo = GetRandomVideoOfUp(randomUpId, videoCount); + UpVideoInfo videoInfo = _videoDomainService.GetRandomVideoOfUp(randomUpId, videoCount); if (!CanDonatedCoinsForVideo(videoInfo.Aid.ToString())) continue; return Tuple.Create(videoInfo.Aid.ToString(), videoInfo.Title); @@ -273,71 +296,6 @@ private bool CanDonatedCoinsForVideo(string aid) } } - /// - /// 获取UP主的视频总数量 - /// - /// - /// - private int GetVidoeCountOfUp(long upId) - { - BiliApiResponse re = _dailyTaskApi.SearchVideosByUpId(upId, 1, 1).Result; - if (re.Code != 0) - { - throw new Exception(re.Message); - } - - return re.Data.Page.Count; - } - - /// - /// 获取某up的一个随机视频 - /// - /// - /// - /// - private UpVideoInfo GetRandomVideoOfUp(long upId, int total) - { - int pageNum = new Random().Next(1, total + 1); - BiliApiResponse re = _dailyTaskApi.SearchVideosByUpId(upId, 1, pageNum).Result; - - if (re.Code != 0) - { - throw new Exception(re.Message); - } - - return re.Data.List.Vlist.First(); - } - - /// - /// 为视频投币 - /// - /// av号 - /// 投币数量 - /// 是否同时点赞 1是0否 - /// 是否投币成功 - private bool DoAddCoinForVideo(string aid, int multiply, bool select_like, string title = "") - { - BiliApiResponse result = _dailyTaskApi.AddCoinForVideo(aid, multiply, select_like ? 1 : 0, _biliBiliCookieOptions.BiliJct).Result; - - if (result.Code == 0) - { - _logger.LogInformation("为“{title}”投币成功", title); - return true; - } - - if (result.Code == -111) - { - string errorMsg = $"投币异常,Cookie配置项[BiliJct]错误或已过期,请检查并更新。接口返回:{result.Message}"; - _logger.LogError(errorMsg); - throw new Exception(errorMsg); - } - else - { - _logger.LogInformation("为“{title}”投币失败,原因:{msg}", title, result.Message); - return false; - } - } - #endregion } } diff --git a/src/Ray.BiliBiliTool.DomainService/Interfaces/IDonateCoinDomainService.cs b/src/Ray.BiliBiliTool.DomainService/Interfaces/IDonateCoinDomainService.cs index 422115f..3c65906 100644 --- a/src/Ray.BiliBiliTool.DomainService/Interfaces/IDonateCoinDomainService.cs +++ b/src/Ray.BiliBiliTool.DomainService/Interfaces/IDonateCoinDomainService.cs @@ -12,5 +12,7 @@ public interface IDonateCoinDomainService : IDomainService void AddCoinsForVideo(); Tuple TryGetCanDonatedVideo(); + + bool DoAddCoinForVideo(string aid, int multiply, bool select_like, string title = ""); } } diff --git a/src/Ray.BiliBiliTool.DomainService/Interfaces/IVideoDomainService.cs b/src/Ray.BiliBiliTool.DomainService/Interfaces/IVideoDomainService.cs index e0e7b00..9f79ceb 100644 --- a/src/Ray.BiliBiliTool.DomainService/Interfaces/IVideoDomainService.cs +++ b/src/Ray.BiliBiliTool.DomainService/Interfaces/IVideoDomainService.cs @@ -16,6 +16,16 @@ public interface IVideoDomainService : IDomainService /// Tuple GetRandomVideoOfRegion(); + /// + /// 从某个指定UP下获取随机视频 + /// + /// + /// + /// + UpVideoInfo GetRandomVideoOfUp(long upId, int total); + + int GetVideoCountOfUp(long upId); + /// /// 观看并分享视频 /// @@ -35,16 +45,5 @@ public interface IVideoDomainService : IDomainService /// /// void ShareVideo(string aid, string title = ""); - - /// - /// 投币 - /// - /// - /// - /// - /// - bool AddCoinsForVideo(string aid, int multiply, bool select_like, string title = ""); - - List GetRandomVideosOfUps(); } } diff --git a/src/Ray.BiliBiliTool.DomainService/VideoDomainService.cs b/src/Ray.BiliBiliTool.DomainService/VideoDomainService.cs index b5f8e70..ecadea7 100644 --- a/src/Ray.BiliBiliTool.DomainService/VideoDomainService.cs +++ b/src/Ray.BiliBiliTool.DomainService/VideoDomainService.cs @@ -20,14 +20,17 @@ public class VideoDomainService : IVideoDomainService private readonly IDailyTaskApi _dailyTaskApi; private readonly BiliBiliCookieOptions _biliBiliCookieOptions; private readonly DailyTaskOptions _dailyTaskOptions; + private readonly IRelationApi _relationApi; public VideoDomainService(ILogger logger, IDailyTaskApi dailyTaskApi, IOptionsMonitor biliBiliCookieOptions, - IOptionsMonitor dailyTaskOptions) + IOptionsMonitor dailyTaskOptions, + IRelationApi relationApi) { _logger = logger; _dailyTaskApi = dailyTaskApi; + _relationApi = relationApi; _biliBiliCookieOptions = biliBiliCookieOptions.CurrentValue; _dailyTaskOptions = dailyTaskOptions.CurrentValue; } @@ -48,6 +51,36 @@ public Tuple GetRandomVideoOfRegion() return Tuple.Create(data.Aid, data.Title); } + public UpVideoInfo GetRandomVideoOfUp(long upId, int total) + { + int pageNum = new Random().Next(1, total + 1); + BiliApiResponse re = _dailyTaskApi.SearchVideosByUpId(upId, 1, pageNum).Result; + + if (re.Code != 0) + { + throw new Exception(re.Message); + } + + return re.Data.List.Vlist.First(); + } + + /// + /// 获取UP主的视频总数量 + /// + /// + /// + public int GetVideoCountOfUp(long upId) + { + //todo:通过获取分页实现的,有待改善 + BiliApiResponse re = _dailyTaskApi.SearchVideosByUpId(upId, 1, 1).Result; + if (re.Code != 0) + { + throw new Exception(re.Message); + } + + return re.Data.Page.Count; + } + public void WatchAndShareVideo(DailyTaskInfo dailyTaskStatus) { Tuple targetVideo = null; @@ -105,94 +138,58 @@ public void ShareVideo(string aid, string title = "") } } + #region private /// - /// 为视频投币 + /// 获取一个视频用来观看并分享 /// - /// av号 - /// 投币数量 - /// 是否同时点赞 1是0否 - /// 是否投币成功 - public bool AddCoinsForVideo(string aid, int multiply, bool select_like, string title = "") + /// + private Tuple GetRandomVideoForWatch() { - BiliApiResponse result = _dailyTaskApi.AddCoinForVideo(aid, multiply, select_like ? 1 : 0, _biliBiliCookieOptions.BiliJct).Result; - - if (result.Code == 0) - { - _logger.LogInformation("为“{title}”投币成功", title); - return true; - } + Tuple video = GetRandomVideoOfFollowingUps(); + if (video != null) return video; - if (result.Code == -111) - { - string errorMsg = $"投币异常,Cookie配置项[BiliJct]错误或已过期,请检查并更新。接口返回:{result.Message}"; - _logger.LogError(errorMsg); - throw new Exception(errorMsg); - } - else - { - _logger.LogInformation("为“{title}”投币失败,原因:{msg}", title, result.Message); - return false; - } + return GetRandomVideoOfRegion(); } - public List GetRandomVideosOfUps() + private Tuple GetRandomVideoOfFollowingUps() { - List re = new List(); - + //配置的UpId int configUpsCount = _dailyTaskOptions.SupportUpIdList.Count; - if (configUpsCount == 0) return re; - - long upId = _dailyTaskOptions.SupportUpIdList[new Random().Next(0, configUpsCount)]; - int count = GetVidoeCountOfUp(upId); - - int targetNum = 10; - if (count < 10) targetNum = count; - for (int i = 0; i < targetNum; i++) + if (configUpsCount > 0) { - UpVideoInfo videoInfo = GetRandomVideoOfUp(upId, count); - if (re.Count(x => x.Aid == videoInfo.Aid) == 0) re.Add(videoInfo); + Tuple video = GetRandomVideoOfUps(_dailyTaskOptions.SupportUpIdList); + if (video != null) return video; } - return re; - } - - private UpVideoInfo GetRandomVideoOfUp(long upId, int total) - { - int pageNum = new Random().Next(1, total + 1); - BiliApiResponse re = _dailyTaskApi.SearchVideosByUpId(upId, 1, pageNum).Result; - - if (re.Code != 0) + //关注列表 + BiliApiResponse result = _relationApi.GetFollowings(_biliBiliCookieOptions.UserId).Result; + if (result.Data.Total > 0) { - throw new Exception(re.Message); + Tuple video = GetRandomVideoOfUps(result.Data.List.Select(x => x.Mid).ToList()); + if (video != null) return video; } - return re.Data.List.Vlist.First(); + return null; } /// - /// 获取UP主的视频总数量 + /// 从up集合中获取一个随机视频 /// - /// + /// /// - private int GetVidoeCountOfUp(long upId) + private Tuple GetRandomVideoOfUps(List upIds) { - BiliApiResponse re = _dailyTaskApi.SearchVideosByUpId(upId, 1, 1).Result; - if (re.Code != 0) - { - throw new Exception(re.Message); - } + long upId = upIds[new Random().Next(0, upIds.Count)]; - return re.Data.Page.Count; - } + int count = GetVideoCountOfUp(upId); - #region private - private Tuple GetRandomVideoForWatch() - { - List list = GetRandomVideosOfUps(); - if (list.Count > 0) - return Tuple.Create(list.First().Aid.ToString(), list.First().Title); + if (count > 0) + { + UpVideoInfo video = GetRandomVideoOfUp(upId, count); + return Tuple.Create(video.Aid.ToString(), video.Title); + } - return GetRandomVideoOfRegion(); + return null; } #endregion private } diff --git a/test/DailyTaskTest/AddCoinsForVideo.cs b/test/DailyTaskTest/AddCoinsForVideo.cs index 9fe40ba..c8a350b 100644 --- a/test/DailyTaskTest/AddCoinsForVideo.cs +++ b/test/DailyTaskTest/AddCoinsForVideo.cs @@ -49,9 +49,9 @@ public void Test2() using (var scope = RayContainer.Root.CreateScope()) { - var dailyTaskAppService = scope.ServiceProvider.GetRequiredService(); + var service = scope.ServiceProvider.GetRequiredService(); - dailyTaskAppService.AddCoinsForVideo("627549610", 1, true); + service.DoAddCoinForVideo("627549610", 1, true); } Assert.True(true);