-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 添加条件排除无法进行直播挂机的up主 * 为跳过的主播添加跳过提示 * 专栏投币基本框架已完成 * 专栏签到全流程基本完成,将开发环境硬编码调整为专栏投币模式开始进行挂机测试。 * 修复了一下Bug,目前专栏投币功能已经能正常运行 * 显式添加个性化配置IsDonateCoinForArticle * 添加对专栏投币同时点赞的功能,与视频点赞共享一个参数 * 修复硬币数量多的时候可投硬币数返回值错误 * 添加了一些log,方便调试 * 修改单词拼写错误 * 临时的windows环境构建脚本 * 删除未使用的参数 * 添加卡券状态查询相关api * 完成每日领取大会员经验任务,同时修复在大积分任务中的一些错误 * 修改调用错误
1 parent
0f21718
commit aa73789
Showing
22 changed files
with
818 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
dotnet.exe publish ../src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj --runtime win-x86 --no-self-contained -c Release -p:PublishSingleFile=true -o ./bin/Publish/win-x86 | ||
dotnet.exe publish ../src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj --runtime win-x64 --no-self-contained -c Release -p:PublishSingleFile=true -o ./bin/Publish/win-x64 | ||
dotnet.exe publish ../src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj --runtime win-arm64 --no-self-contained -c Release -p:PublishSingleFile=true -o ./bin/Publish/win-arm64 | ||
dotnet.exe publish ../src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj --runtime linux-x64 --no-self-contained -c Release -p:PublishSingleFile=true -o ./bin/Publish/linux-x64 | ||
dotnet.exe publish ../src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj --runtime linux-musl-x64 --no-self-contained -c Release -p:PublishSingleFile=true -o ./bin/Publish/linux-musl-x64 | ||
dotnet.exe publish ../src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj --runtime linux-arm64 --no-self-contained -c Release -p:PublishSingleFile=true -o ./bin/Publish/linux-arm64 | ||
dotnet.exe publish ../src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj --runtime linux-arm --no-self-contained -c Release -p:PublishSingleFile=true -o ./bin/Publish/linux-arm | ||
dotnet.exe publish ../src/Ray.BiliBiliTool.Console/Ray.BiliBiliTool.Console.csproj --runtime osx-x64 --no-self-contained -c Release -p:PublishSingleFile=true -o ./bin/Publish/osx-x64 | ||
Remove-Item ./bin/Publish/win-x86/*.pdb | ||
Remove-Item ./bin/Publish/win-x64/*.pdb | ||
Remove-Item ./bin/Publish/win-arm64/*.pdb | ||
Remove-Item ./bin/Publish/linux-x64/*.pdb | ||
Remove-Item ./bin/Publish/linux-musl-x64/*.pdb | ||
Remove-Item ./bin/Publish/linux-arm64/*.pdb | ||
Remove-Item ./bin/Publish/linux-arm/*.pdb | ||
Remove-Item ./bin/Publish/osx-x64/*.pdb |
22 changes: 22 additions & 0 deletions
22
src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/Article/AddCoinForArticleRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.Article; | ||
|
||
public class AddCoinForArticleRequest | ||
{ | ||
public AddCoinForArticleRequest(long cvid,long mid,string csrf) | ||
{ | ||
Aid = cvid; | ||
Upid = mid; | ||
Csrf = csrf; | ||
} | ||
|
||
public long Aid { get; set; } | ||
|
||
public long Upid { get; set; } | ||
|
||
public int Multiply { get; set; } = 1; | ||
|
||
// 必须为2 | ||
public int Avtype { get; private set; } = 2; | ||
|
||
public string Csrf { get; set; } | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/Article/SearchArticleInfoResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.Article; | ||
|
||
public class SearchArticleInfoResponse | ||
{ | ||
public int Like { get; set; } | ||
|
||
public int Coin { get; set; } | ||
|
||
public long Mid { get; set; } | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/Article/SearchArticlesByUpIdFullFto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.Article; | ||
|
||
public class SearchArticlesByUpIdDto | ||
{ | ||
public long Mid { get; set; } | ||
|
||
public int Pn { get; set; } = 1; | ||
|
||
public int Ps { get; set; } = 30; | ||
|
||
public string Sort { get; set; } = "publish_time"; | ||
} | ||
|
||
public class SearchArticlesByUpIdFullDto : SearchArticlesByUpIdDto | ||
{ | ||
public string w_rid { get; set; } | ||
|
||
public long wts { get; set; } | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/Article/SearchUpArticlesResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.Article; | ||
|
||
public class SearchUpArticlesResponse | ||
{ | ||
public List<ArticleInfo> Articles { get; set; } | ||
public int Count { get; set; } | ||
|
||
} | ||
|
||
public class ArticleInfo | ||
{ | ||
public long Id { get; set; } | ||
|
||
public string Title { get; set; } | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/VipTask/VipExperienceRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.VipTask; | ||
|
||
public class VipExperienceRequest | ||
{ | ||
public string csrf { get; set; } | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Dtos/VipTask/VouchersInfoResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.VipTask; | ||
|
||
public class VouchersInfoResponse | ||
{ | ||
public List<List> List { get; set; } | ||
public bool IsShortVip { get; set; } | ||
public bool IsFreightOpen { get; set; } | ||
public int Level { get; set; } | ||
public int CurExp { get; set; } | ||
public int NextExp { get; set; } | ||
public bool IsVip { get; set; } | ||
public int IsSeniorMember { get; set; } | ||
public int Format060102 { get; set; } | ||
} | ||
|
||
|
||
public class List | ||
{ | ||
public int Type { get; set; } | ||
public int State { get; set; } | ||
public int ExpireTime { get; set; } | ||
public int VipType { get; set; } | ||
public int NextReceiveDays { get; set; } | ||
public int PeriodEndUnix { get; set; } | ||
} | ||
|
45 changes: 45 additions & 0 deletions
45
src/Ray.BiliBiliTool.Agent/BiliBiliAgent/Interfaces/IArticleApi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos; | ||
using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.Article; | ||
using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.Video; | ||
using WebApiClientCore.Attributes; | ||
|
||
namespace Ray.BiliBiliTool.Agent.BiliBiliAgent.Interfaces | ||
{ | ||
|
||
[Header("Host", "api.bilibili.com")] | ||
public interface IArticleApi : IBiliBiliApi | ||
{ | ||
[Header("Content-Type", "application/x-www-form-urlencoded")] | ||
[Header("Origin", "https://www.bilibili.com")] | ||
[HttpPost("/x/web-interface/coin/add")] | ||
Task<BiliApiResponse> AddCoinForArticle([FormContent] AddCoinForArticleRequest request, [Header("referer")] string refer = "https://www.bilibili.com/read/cv5806746/?from=search&spm_id_from=333.337.0.0"); | ||
|
||
|
||
[Header("Referer", "https://www.bilibili.com/")] | ||
[Header("Origin", "https://space.bilibili.com")] | ||
[HttpGet("/x/space/wbi/article")] | ||
Task<BiliApiResponse<SearchUpArticlesResponse>> SearchUpArticlesByUpId( | ||
[PathQuery] SearchArticlesByUpIdFullDto request); | ||
|
||
/// <summary> | ||
/// 获取专栏详情 | ||
/// </summary> | ||
/// <param name="cvid"></param> | ||
/// <returns></returns> | ||
[HttpGet("/x/article/viewinfo?id={cvid}")] | ||
Task<BiliApiResponse<SearchArticleInfoResponse>> SearchArticleInfo(long cvid); | ||
|
||
|
||
[Header("Content-Type", "application/x-www-form-urlencoded")] | ||
[Header("Referer", "https://www.bilibili.com/read/cv{cvid}/?from=search&spm_id_from=333.337.0.0")] | ||
[Header("Origin", "https://www.bilibili.com")] | ||
[HttpPost("/x/article/like?id={cvid}&type=1&csrf={csrf}")] | ||
Task<BiliApiResponse> Like(long cvid, string csrf); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
395 changes: 395 additions & 0 deletions
395
src/Ray.BiliBiliTool.DomainService/ArticleDomainService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,395 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Polly; | ||
using Ray.BiliBiliTool.Agent; | ||
using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos; | ||
using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.Article; | ||
using Ray.BiliBiliTool.Agent.BiliBiliAgent.Interfaces; | ||
using Ray.BiliBiliTool.Config.Options; | ||
using Ray.BiliBiliTool.DomainService.Interfaces; | ||
|
||
namespace Ray.BiliBiliTool.DomainService; | ||
|
||
public class ArticleDomainService : IArticleDomainService | ||
{ | ||
private readonly IArticleApi _articleApi; | ||
private readonly BiliCookie _biliCookie; | ||
private readonly ILogger<ArticleDomainService> _logger; | ||
private readonly DailyTaskOptions _dailyTaskOptions; | ||
private readonly ICoinDomainService _coinDomainService; | ||
private readonly IAccountApi _accountApi; | ||
private readonly IWbiDomainService _wbiDomainService; | ||
|
||
|
||
/// <summary> | ||
/// up的专栏总数缓存 | ||
/// </summary> | ||
private readonly Dictionary<long, int> _upArticleCountDicCatch = new(); | ||
|
||
/// <summary> | ||
/// 已对投币数量缓存 | ||
/// </summary> | ||
private readonly Dictionary<string, int> _alreadyDonatedCoinCountCatch = new(); | ||
|
||
public ArticleDomainService( | ||
IArticleApi articleApi, | ||
BiliCookie biliCookie, | ||
ILogger<ArticleDomainService> logger, | ||
IOptionsMonitor<DailyTaskOptions> dailyTaskOptions, | ||
ICoinDomainService coinDomainService, | ||
IAccountApi accountApi, IWbiDomainService wbiDomainService) | ||
{ | ||
_articleApi = articleApi; | ||
_biliCookie = biliCookie; | ||
_logger = logger; | ||
_coinDomainService = coinDomainService; | ||
_accountApi = accountApi; | ||
_wbiDomainService = wbiDomainService; | ||
_dailyTaskOptions = dailyTaskOptions.CurrentValue; | ||
} | ||
|
||
|
||
|
||
public async Task LikeArticle(long cvid) | ||
{ | ||
await _articleApi.Like(cvid, _biliCookie.BiliJct); | ||
} | ||
|
||
/// <summary> | ||
/// 投币专栏任务 | ||
/// </summary> | ||
/// <returns></returns> | ||
public async Task<bool> AddCoinForArticles() | ||
{ | ||
|
||
var donateCoinsCounts = await CalculateDonateCoinsCounts(); | ||
|
||
if (donateCoinsCounts == 0) | ||
{ | ||
// 没有可投的币相当于投币任务全部完成 | ||
return true; | ||
} | ||
|
||
|
||
int success = 0; | ||
int tryCount = 10; | ||
|
||
for (int i = 0; i <= tryCount && success < donateCoinsCounts; i++) | ||
{ | ||
_logger.LogDebug("开始尝试第{num}次", i); | ||
|
||
var upId = GetUpFromConfigUps(); | ||
var cvid = await GetRandomArticleFromUp(upId); | ||
if (upId == 0 || cvid == 0) | ||
{ | ||
_logger.LogInformation("未添加支持的Up主,任务跳过"); | ||
return false; | ||
} | ||
|
||
if (await AddCoinForArticle(cvid, upId)) | ||
{ | ||
// 点赞 | ||
if (_dailyTaskOptions.SelectLike) | ||
{ | ||
await LikeArticle(cvid); | ||
_logger.LogInformation("文章点赞成功"); | ||
} | ||
success++; | ||
} | ||
|
||
|
||
} | ||
|
||
if (success == donateCoinsCounts) | ||
_logger.LogInformation("专栏投币任务完成"); | ||
else | ||
{ | ||
_logger.LogInformation("投币尝试超过10次,已终止"); | ||
return false; | ||
} | ||
|
||
|
||
_logger.LogInformation("【硬币余额】{coin}", (await _accountApi.GetCoinBalance()).Data.Money ?? 0); | ||
|
||
return true; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 给某一篇专栏投币 | ||
/// </summary> | ||
/// <param name="cvid">文章cvid</param> | ||
/// <param name="mid">文章作者mid</param> | ||
/// <returns>投币是否成功(false 投币失败,true 投币成功)</returns> | ||
public async Task<bool> AddCoinForArticle(long cvid, long mid) | ||
{ | ||
BiliApiResponse result; | ||
try | ||
{ | ||
var refer = $"https://www.bilibili.com/read/cv{cvid}/?from=search&spm_id_from=333.337.0.0"; | ||
result = await _articleApi.AddCoinForArticle(new AddCoinForArticleRequest(cvid, mid, _biliCookie.BiliJct), | ||
refer); | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
|
||
if (result.Code == 0) | ||
{ | ||
_logger.LogInformation("投币成功,经验+10 √"); | ||
return true; | ||
} | ||
else | ||
{ | ||
_logger.LogError("投币错误 {message}", result.Message); | ||
return false; | ||
} | ||
} | ||
|
||
|
||
#region private | ||
|
||
/// <summary> | ||
/// 从某个up主中随机挑选一个专栏 | ||
/// </summary> | ||
/// <param name="mid"></param> | ||
/// <returns>专栏的cvid</returns> | ||
private async Task<long> GetRandomArticleFromUp(long mid) | ||
{ | ||
if (!_upArticleCountDicCatch.TryGetValue(mid, out int articleCount)) | ||
{ | ||
articleCount = await GetArticleCountOfUp(mid); | ||
_upArticleCountDicCatch.Add(mid, articleCount); | ||
} | ||
|
||
// 专栏数为0时 | ||
if (articleCount == 0) | ||
{ | ||
return 0; | ||
} | ||
|
||
var req = new SearchArticlesByUpIdDto() | ||
{ | ||
Mid = mid, | ||
Ps = 1, | ||
Pn = new Random().Next(1, articleCount + 1) | ||
}; | ||
var w_ridDto = await _wbiDomainService.GetWridAsync(req); | ||
|
||
var fullDto = new SearchArticlesByUpIdFullDto() | ||
{ | ||
Mid = mid, | ||
Ps = req.Ps, | ||
Pn = req.Pn, | ||
w_rid = w_ridDto.w_rid, | ||
wts = w_ridDto.wts | ||
}; | ||
|
||
BiliApiResponse<SearchUpArticlesResponse> re = await _articleApi.SearchUpArticlesByUpId(fullDto); | ||
|
||
if (re.Code != 0) | ||
{ | ||
throw new Exception(re.Message); | ||
} | ||
|
||
ArticleInfo articleInfo = re.Data.Articles.FirstOrDefault(); | ||
|
||
_logger.LogDebug("获取到的专栏{cvid}({title})", articleInfo.Id, articleInfo.Title); | ||
|
||
// 检查是否可投 | ||
if (!await IsCanDonate(articleInfo.Id)) | ||
{ | ||
return 0; | ||
} | ||
|
||
return articleInfo.Id; | ||
} | ||
|
||
|
||
// TODO 转变为异步代码 | ||
/// <summary> | ||
/// 从支持UP主列表中随机挑选一位 | ||
/// </summary> | ||
/// <returns>被挑选up主的mid</returns> | ||
private long GetUpFromConfigUps() | ||
{ | ||
if (_dailyTaskOptions.SupportUpIdList == null || _dailyTaskOptions.SupportUpIdList.Count == 0) | ||
{ | ||
return 0; | ||
} | ||
|
||
try | ||
{ | ||
long randomUpId = | ||
_dailyTaskOptions.SupportUpIdList[new Random().Next(0, _dailyTaskOptions.SupportUpIdList.Count)]; | ||
|
||
if (randomUpId is 0 or long.MinValue) return 0; | ||
|
||
if (randomUpId.ToString() == _biliCookie.UserId) | ||
{ | ||
_logger.LogDebug("不能为自己投币"); | ||
return 0; | ||
} | ||
_logger.LogDebug("挑选出的up主为{UpId}",randomUpId); | ||
return randomUpId; | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogWarning("异常:{msg}", e); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/// <summary> | ||
/// 获取Up主专栏总数 | ||
/// </summary> | ||
/// <param name="mid">up主mid</param> | ||
/// <returns>专栏总数</returns> | ||
/// <exception cref="Exception"></exception> | ||
private async Task<int> GetArticleCountOfUp(long mid) | ||
{ | ||
var req = new SearchArticlesByUpIdDto() | ||
{ | ||
Mid = mid | ||
}; | ||
|
||
var w_ridDto = await _wbiDomainService.GetWridAsync(req); | ||
|
||
var fullDto = new SearchArticlesByUpIdFullDto() | ||
{ | ||
Mid = mid, | ||
w_rid = w_ridDto.w_rid, | ||
wts = w_ridDto.wts | ||
}; | ||
|
||
BiliApiResponse<SearchUpArticlesResponse> re = await _articleApi.SearchUpArticlesByUpId(fullDto); | ||
|
||
if (re.Code != 0) | ||
{ | ||
throw new Exception(re.Message); | ||
} | ||
|
||
return re.Data.Count; | ||
} | ||
|
||
/// <summary> | ||
/// 计算所需要投的硬币数量 | ||
/// </summary> | ||
/// <returns>硬币数量</returns> | ||
private async Task<int> CalculateDonateCoinsCounts() | ||
{ | ||
int needCoins = await GetNeedDonateCoinCounts(); | ||
|
||
int protectedCoins = _dailyTaskOptions.NumberOfProtectedCoins; | ||
if (needCoins <= 0) return 0; | ||
|
||
//投币前硬币余额 | ||
decimal coinBalance = await _coinDomainService.GetCoinBalance(); | ||
_logger.LogInformation("【投币前余额】 : {coinBalance}", coinBalance); | ||
_ = int.TryParse(decimal.Truncate(coinBalance - protectedCoins).ToString(), out int unprotectedCoins); | ||
|
||
if (coinBalance <= 0) | ||
{ | ||
_logger.LogInformation("因硬币余额不足,今日暂不执行投币任务"); | ||
return 0; | ||
} | ||
|
||
if (coinBalance <= protectedCoins) | ||
{ | ||
_logger.LogInformation("因硬币余额达到或低于保留值,今日暂不执行投币任务"); | ||
return 0; | ||
} | ||
|
||
//余额小于目标投币数,按余额投 | ||
if (coinBalance < needCoins) | ||
{ | ||
_ = int.TryParse(decimal.Truncate(coinBalance).ToString(), out needCoins); | ||
_logger.LogInformation("因硬币余额不足,目标投币数调整为: {needCoins}", needCoins); | ||
return needCoins; | ||
} | ||
|
||
//投币后余额小于等于保护值,按保护值允许投 | ||
if (coinBalance - needCoins <= protectedCoins) | ||
{ | ||
//排除需投等于保护后可投数量相等时的情况 | ||
if (unprotectedCoins != needCoins) | ||
{ | ||
needCoins = unprotectedCoins; | ||
_logger.LogInformation("因硬币余额投币后将达到或低于保留值,目标投币数调整为: {needCoins}", needCoins); | ||
return needCoins; | ||
} | ||
} | ||
|
||
return needCoins; | ||
} | ||
|
||
private async Task<int> GetNeedDonateCoinCounts() | ||
{ | ||
int configCoins = _dailyTaskOptions.NumberOfCoins; | ||
|
||
if (configCoins <= 0) | ||
{ | ||
_logger.LogInformation("已配置为跳过投币任务"); | ||
return configCoins; | ||
} | ||
|
||
//已投的硬币 | ||
int alreadyCoins = await _coinDomainService.GetDonatedCoins(); | ||
|
||
int targetCoins = configCoins; | ||
|
||
_logger.LogInformation("【今日已投】{already}枚", alreadyCoins); | ||
_logger.LogInformation("【目标欲投】{already}枚", targetCoins); | ||
|
||
if (targetCoins > alreadyCoins) | ||
{ | ||
int needCoins = targetCoins - alreadyCoins; | ||
_logger.LogInformation("【还需再投】{need}枚", needCoins); | ||
return needCoins; | ||
} | ||
|
||
_logger.LogInformation("已完成投币任务,不需要再投啦~"); | ||
return 0; | ||
} | ||
|
||
|
||
private async Task<bool> IsCanDonate(long cvid) | ||
{ | ||
try | ||
{ | ||
if (_alreadyDonatedCoinCountCatch.Any(x => x.Key == cvid.ToString())) | ||
{ | ||
_logger.LogDebug("重复专栏,丢弃处理"); | ||
return false; | ||
} | ||
|
||
if (!_alreadyDonatedCoinCountCatch.TryGetValue(cvid.ToString(), out int multiply)) | ||
{ | ||
multiply = (await _articleApi.SearchArticleInfo(cvid)).Data.Coin; | ||
_alreadyDonatedCoinCountCatch.TryAdd(cvid.ToString(), multiply); | ||
} | ||
|
||
// 在网页端我测试时只能投一枚硬币,暂时设置最多投一枚 | ||
if (multiply >= 1) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogWarning("异常:{mag}", e); | ||
return false; | ||
} | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Ray.BiliBiliTool.DomainService/Interfaces/IArticleDomainService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Ray.BiliBiliTool.DomainService.Interfaces; | ||
|
||
public interface IArticleDomainService : IDomainService | ||
{ | ||
Task<bool> AddCoinForArticle(long cvid, long mid); | ||
|
||
Task<bool> AddCoinForArticles(); | ||
|
||
Task LikeArticle(long cvid); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Ray.BiliBiliTool.Application.Contracts; | ||
using Ray.BiliBiliTool.Infrastructure; | ||
|
||
namespace AppServiceTest; | ||
|
||
public class VipServiceTest | ||
{ | ||
public VipServiceTest() | ||
{ | ||
Program.CreateHost(new[] { "--ENVIRONMENT=Development" }); | ||
} | ||
|
||
[Fact] | ||
public async Task VipExpressTest() | ||
{ | ||
using var scope = Global.ServiceProviderRoot.CreateScope(); | ||
var appService = scope.ServiceProvider.GetRequiredService<IVipBigPointAppService>(); | ||
await appService.VipExpress(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Ray.BiliBiliTool.Agent; | ||
using Ray.BiliBiliTool.Agent.BiliBiliAgent.Dtos.VipTask; | ||
using Ray.BiliBiliTool.Console; | ||
using Ray.BiliBiliTool.Infrastructure; | ||
using Ray.BiliBiliTool.Agent.BiliBiliAgent.Interfaces; | ||
using Xunit.Abstractions; | ||
namespace BiliAgentTest; | ||
|
||
public class VipApiTest | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
public VipApiTest(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
Program.CreateHost(new[] { "--ENVIRONMENT=Development" }); | ||
} | ||
|
||
[Fact] | ||
public async Task VipInfoTest() | ||
{ | ||
using var scope = Global.ServiceProviderRoot.CreateScope(); | ||
|
||
var ck = scope.ServiceProvider.GetRequiredService<BiliCookie>(); | ||
var api = scope.ServiceProvider.GetRequiredService<IVipBigPointApi>(); | ||
|
||
var re = await api.GetVouchersInfo(); | ||
if (re.Code == 0) | ||
{ | ||
var info = re.Data.List.Find(x => x.Type == 9); | ||
if (info != null) | ||
{ | ||
_output.WriteLine(info.State.ToString()); | ||
} | ||
else | ||
{ | ||
_output.WriteLine("error"); | ||
} | ||
} | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task GetVipExperienceTest() | ||
{ | ||
using var scope = Global.ServiceProviderRoot.CreateScope(); | ||
|
||
var ck = scope.ServiceProvider.GetRequiredService<BiliCookie>(); | ||
var api = scope.ServiceProvider.GetRequiredService<IVipBigPointApi>(); | ||
var re = await api.GetVipExperience(new VipExperienceRequest() | ||
{ | ||
csrf = ck.BiliJct | ||
}); | ||
|
||
_output.WriteLine(re.Message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Xunit.Abstractions; | ||
|
||
namespace DomainServiceTest; | ||
|
||
public class ArticleDomainServiceTest | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
public ArticleDomainServiceTest(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
Program.CreateHost(new[] { "--ENVIRONMENT=Development" }); | ||
} | ||
|
||
[Fact] | ||
public async Task LikeArticleTest() | ||
{ | ||
using var scope = Global.ServiceProviderRoot.CreateScope(); | ||
var config = Global.ConfigurationRoot; | ||
var domainService = scope.ServiceProvider.GetRequiredService<IArticleDomainService>(); | ||
await domainService.LikeArticle(5806746); | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task AddCoinForArticleTest() | ||
{ | ||
using var scope = Global.ServiceProviderRoot.CreateScope(); | ||
var config = Global.ConfigurationRoot; | ||
var domainService = scope.ServiceProvider.GetRequiredService<IArticleDomainService>(); | ||
|
||
// 测试用的专栏:https://www.bilibili.com/read/cv5806746/?from=search&spm_id_from=333.337.0.0 | ||
|
||
await domainService.AddCoinForArticle(5806746, 486980924); | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task AddCoinForArticlesTest() | ||
{ | ||
using var scope = Global.ServiceProviderRoot.CreateScope(); | ||
var config = Global.ConfigurationRoot; | ||
var domainService = scope.ServiceProvider.GetRequiredService<IArticleDomainService>(); | ||
await domainService.AddCoinForArticles(); | ||
} | ||
|
||
|
||
} |