Skip to content

Commit

Permalink
优化视频领域服务选取视频逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
RayWangQvQ committed Dec 16, 2020
1 parent 71f89f6 commit 4132b04
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 156 deletions.
12 changes: 12 additions & 0 deletions clean.cmd
Original file line number Diff line number Diff line change
@@ -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
112 changes: 35 additions & 77 deletions src/Ray.BiliBiliTool.DomainService/DonateCoinDomainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public class DonateCoinDomainService : IDonateCoinDomainService
private readonly IVideoDomainService _videoDomainService;
private readonly IRelationApi _relationApi;

private Dictionary<string, int> _alreadyDonatedCoinsCatch = new Dictionary<string, int>();
private readonly Dictionary<string, int> _alreadyDonatedCoinsCatch = new Dictionary<string, int>();

public DonateCoinDomainService(ILogger<DonateCoinDomainService> logger,
IDailyTaskApi dailyTaskApi,
IOptionsMonitor<BiliBiliCookieOptions> biliBiliCookieOptions,
IOptionsMonitor<BiliBiliCookieOptions> cookieOptions,
IOptionsMonitor<DailyTaskOptions> dailyTaskOptions,
IAccountApi accountApi,
ICoinDomainService coinDomainService,
Expand All @@ -38,7 +38,7 @@ public DonateCoinDomainService(ILogger<DonateCoinDomainService> logger,
{
_logger = logger;
_dailyTaskApi = dailyTaskApi;
_biliBiliCookieOptions = biliBiliCookieOptions.CurrentValue;
_biliBiliCookieOptions = cookieOptions.CurrentValue;
_dailyTaskOptions = dailyTaskOptions.CurrentValue;
_accountApi = accountApi;
_coinDomainService = coinDomainService;
Expand Down Expand Up @@ -114,6 +114,35 @@ public Tuple<string, string> TryGetCanDonatedVideo()
return result;
}

/// <summary>
/// 为视频投币
/// </summary>
/// <param name="aid">av号</param>
/// <param name="multiply">投币数量</param>
/// <param name="select_like">是否同时点赞 1是0否</param>
/// <returns>是否投币成功</returns>
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

Expand Down Expand Up @@ -166,9 +195,6 @@ private Tuple<string, string> TryGetCanDonatedVideoByUp(int tryCount)
/// <returns></returns>
private Tuple<string, string> TryGetCanDonatedVideoBySpecialUps(int tryCount)
{
//缓存每个up的视频总数
var videoCountDic = new Dictionary<long, int>();

//获取特别关注列表
BiliApiResponse<List<UpInfo>> specials = _relationApi.GetSpecialFollowings().Result;
if (specials.Data == null || specials.Data.Count == 0) return null;
Expand All @@ -177,15 +203,12 @@ private Tuple<string, string> TryGetCanDonatedVideoBySpecialUps(int tryCount)
}

/// <summary>
/// 尝试从特别关注的Up主中随机获取一个可以投币的视频
/// 尝试从普通关注的Up主中随机获取一个可以投币的视频
/// </summary>
/// <param name="tryCount"></param>
/// <returns></returns>
private Tuple<string, string> TryGetCanDonatedVideoByFollowingUps(int tryCount)
{
//缓存每个up的视频总数
Dictionary<long, int> videoCountDic = new Dictionary<long, int>();

//获取特别关注列表
BiliApiResponse<GetFollowingsResponse> result = _relationApi.GetFollowings(_biliBiliCookieOptions.UserId).Result;
if (result.Data.Total == 0) return null;
Expand Down Expand Up @@ -235,12 +258,12 @@ private Tuple<string, string> TryGetCanDonateVideoByUps(List<long> 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);
Expand Down Expand Up @@ -273,71 +296,6 @@ private bool CanDonatedCoinsForVideo(string aid)
}
}

/// <summary>
/// 获取UP主的视频总数量
/// </summary>
/// <param name="upId"></param>
/// <returns></returns>
private int GetVidoeCountOfUp(long upId)
{
BiliApiResponse<SearchUpVideosResponse> re = _dailyTaskApi.SearchVideosByUpId(upId, 1, 1).Result;
if (re.Code != 0)
{
throw new Exception(re.Message);
}

return re.Data.Page.Count;
}

/// <summary>
/// 获取某up的一个随机视频
/// </summary>
/// <param name="upId"></param>
/// <param name="total"></param>
/// <returns></returns>
private UpVideoInfo GetRandomVideoOfUp(long upId, int total)
{
int pageNum = new Random().Next(1, total + 1);
BiliApiResponse<SearchUpVideosResponse> re = _dailyTaskApi.SearchVideosByUpId(upId, 1, pageNum).Result;

if (re.Code != 0)
{
throw new Exception(re.Message);
}

return re.Data.List.Vlist.First();
}

/// <summary>
/// 为视频投币
/// </summary>
/// <param name="aid">av号</param>
/// <param name="multiply">投币数量</param>
/// <param name="select_like">是否同时点赞 1是0否</param>
/// <returns>是否投币成功</returns>
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface IDonateCoinDomainService : IDomainService
void AddCoinsForVideo();

Tuple<string, string> TryGetCanDonatedVideo();

bool DoAddCoinForVideo(string aid, int multiply, bool select_like, string title = "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public interface IVideoDomainService : IDomainService
/// <returns></returns>
Tuple<string, string> GetRandomVideoOfRegion();

/// <summary>
/// 从某个指定UP下获取随机视频
/// </summary>
/// <param name="upId"></param>
/// <param name="total"></param>
/// <returns></returns>
UpVideoInfo GetRandomVideoOfUp(long upId, int total);

int GetVideoCountOfUp(long upId);

/// <summary>
/// 观看并分享视频
/// </summary>
Expand All @@ -35,16 +45,5 @@ public interface IVideoDomainService : IDomainService
/// <param name="aid"></param>
/// <param name="dailyTaskStatus"></param>
void ShareVideo(string aid, string title = "");

/// <summary>
/// 投币
/// </summary>
/// <param name="aid"></param>
/// <param name="multiply"></param>
/// <param name="select_like"></param>
/// <returns></returns>
bool AddCoinsForVideo(string aid, int multiply, bool select_like, string title = "");

List<UpVideoInfo> GetRandomVideosOfUps();
}
}
Loading

0 comments on commit 4132b04

Please sign in to comment.