Skip to content

Commit

Permalink
继续增加代码和结构;增加测试项目;完成登录和同步规则部分
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedei committed Jun 22, 2020
1 parent 6c30831 commit 9a0e27b
Show file tree
Hide file tree
Showing 18 changed files with 403 additions and 67 deletions.
30 changes: 28 additions & 2 deletions ResourceMonitor/.idea/.idea.ResourceMonitor/.idea/contentModel.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ResourceMonitor/.idea/.idea.ResourceMonitor/riderModule.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ResourceMonitor/ResourceMonitor.Test/ResourceMonitor.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions ResourceMonitor/ResourceMonitor.Test/RulesContainerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ResourceMonitor.Test
{
[TestClass]
public class RulesContainerTests
{
[TestMethod]
public void TestMethod1()
{
}
}
}
6 changes: 6 additions & 0 deletions ResourceMonitor/ResourceMonitor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceMonitor", "ResourceMonitor\ResourceMonitor.csproj", "{6803E3E6-61BA-4DB0-BD0C-1A810D493E9C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceMonitor.Test", "ResourceMonitor.Test\ResourceMonitor.Test.csproj", "{6C8A0394-AA65-4E45-8E11-16067A94EE99}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{6803E3E6-61BA-4DB0-BD0C-1A810D493E9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6803E3E6-61BA-4DB0-BD0C-1A810D493E9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6803E3E6-61BA-4DB0-BD0C-1A810D493E9C}.Release|Any CPU.Build.0 = Release|Any CPU
{6C8A0394-AA65-4E45-8E11-16067A94EE99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C8A0394-AA65-4E45-8E11-16067A94EE99}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C8A0394-AA65-4E45-8E11-16067A94EE99}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C8A0394-AA65-4E45-8E11-16067A94EE99}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ public class AutoDownloadRule
public bool chooseNewerIfDuplicate { get; set; }
public int limitFileSize { get; set; }
public int version { get; set; }

public override string ToString()
{
return $"{nameof(id)}: {id}, {nameof(description)}: {description}, {nameof(keyword)}: {keyword}, {nameof(subgroupId)}: {subgroupId}, {nameof(typeId)}: {typeId}, {nameof(maxCount)}: {maxCount}, {nameof(autoStart)}: {autoStart}, {nameof(createdTime)}: {createdTime}, {nameof(subgroupName)}: {subgroupName}, {nameof(typeName)}: {typeName}, {nameof(chooseNewerIfDuplicate)}: {chooseNewerIfDuplicate}, {nameof(limitFileSize)}: {limitFileSize}, {nameof(version)}: {version}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public class LoginRequest
public string appId { get; set; }
public long unixTimestamp { get; set; }
public string hash { get; set; }

}
}
4 changes: 2 additions & 2 deletions ResourceMonitor/ResourceMonitor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public static int Main(string[] args)

try
{
Log.Information("Starting resource monitor");
Log.Information("启动 ResMonitor...");
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
Log.Fatal(ex, "ResMonitor 因意外情况终止");
return 1;
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"ResourceMonitor": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"launchUrl": "index.html",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
3 changes: 3 additions & 0 deletions ResourceMonitor/ResourceMonitor/ResourceMonitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Authors>kaedei</Authors>
<Copyright>Copyright kaedei 2020</Copyright>
<PackageProjectUrl>https://github.com/kaedei/dandanplay-resmonitor</PackageProjectUrl>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
using Refit;
using ResourceMonitor.Models.DandanplayApi;

namespace ResourceMonitor.Services
namespace ResourceMonitor.Services.Declaration
{
public interface IDandanplayApi
{
[Post("api/v2/login")]
[Post("/api/v2/login")]
Task<LoginResponse> Login(LoginRequest request);

[Get("api/v2/login/renew")]
[Get("/api/v2/login/renew")]
Task<LoginResponse> Renew([Header("Authorization")] string authorization);

[Post("api/v2/sync/autodownload")]
[Post("/api/v2/sync/autodownload")]
Task<AutoDownloadRuleListResponse> SyncAutoDownloadRules(AutoDownloadRuleSyncRequest request,
[Header("Authorization")] string authorization);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Immutable;
using ResourceMonitor.Models.DandanplayApi;

namespace ResourceMonitor.Services.Declaration
{
public interface IRulesContainer
{
bool IsUpdating { get; set; }
ImmutableList<AutoDownloadRule> LocalRules { get; }
void SyncWithServerRules(AutoDownloadRuleListResponse serverRules);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ResourceMonitor.Models.DandanplayApi;
using ResourceMonitor.Services.Declaration;

namespace ResourceMonitor.Services.Implementation
{
/// <summary>
/// 检查新的资源
/// </summary>
public class CheckNewResourcesBackgroundService : BackgroundService
{
private readonly IRulesContainer _rulesContainer;
private readonly IConfiguration _configuration;
private readonly ILogger<CheckNewResourcesBackgroundService> _logger;

public CheckNewResourcesBackgroundService(IRulesContainer rulesContainer,
IConfiguration configuration,
ILogger<CheckNewResourcesBackgroundService> logger)
{
_rulesContainer = rulesContainer;
_configuration = configuration;
_logger = logger;
}


protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation($"{nameof(CheckNewResourcesBackgroundService)} 开始在后台运行");

while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("开始解析所有规则");

//等待规则同步完毕
while (_rulesContainer.IsUpdating)
{
_logger.LogInformation("自动下载规则正在同步中,等待同步完毕...");
await Task.Delay(TimeSpan.FromSeconds(15), stoppingToken);
}

//按顺序解析每个规则
var ruleList = _rulesContainer.LocalRules;
foreach (var rule in ruleList)
{
await DownloadRule(rule);
}

//默认每 15 分钟检查一次
_logger.LogInformation("全部规则解析完毕,等待 15 分钟后再次执行");
await Task.Delay(TimeSpan.FromMinutes(15), stoppingToken);
}
}

private async Task DownloadRule(AutoDownloadRule rule)
{
_logger.LogInformation($"正在解析规则 {rule}");
//获得需要下载的新资源

//对比当前已有的下载任务,只下载新的资源

//解析磁力链至种子文件

//连接下载器,添加远程任务
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Collections.Concurrent;
using System.Collections.Immutable;
using Microsoft.Extensions.Logging;
using ResourceMonitor.Models.DandanplayApi;
using ResourceMonitor.Services.Declaration;

namespace ResourceMonitor.Services.Implementation
{
public class RulesContainer : IRulesContainer
{
private readonly ILogger<RulesContainer> _logger;
private readonly ConcurrentDictionary<string, AutoDownloadRule> _localRules = new ConcurrentDictionary<string, AutoDownloadRule>();
private bool _isUpdating = true;

public RulesContainer(ILogger<RulesContainer> logger)
{
_logger = logger;
}

public bool IsUpdating
{
get => _isUpdating;
set
{
_logger.LogDebug($"RulesContainer.IsUpdating 设置为 {value}");
_isUpdating = value;
}
}

public ImmutableList<AutoDownloadRule> LocalRules => _localRules.Values.ToImmutableList();

public void SyncWithServerRules(AutoDownloadRuleListResponse serverRules)
{
_logger.LogInformation("开始同步服务器规则");
//移除服务器端已经删除的规则
foreach (var removedRuleId in serverRules.removedRuleIds ?? new string[0])
{
_localRules.TryRemove(removedRuleId, out var removedRule);
_logger.LogInformation($"删除规则 {removedRule}");
}

//添加或更新规则
foreach (var serverRule in serverRules.rules)
{
//新增规则
if (!_localRules.ContainsKey(serverRule.id))
{
_localRules.TryAdd(serverRule.id, serverRule);
_logger.LogInformation($"新增规则 {serverRule}");
}
else //更新规则
{
var localRule = _localRules[serverRule.id];
if (serverRule.version > localRule.version)
{
_localRules[serverRule.id] = serverRule;
_logger.LogInformation($"更新规则-原规则 {localRule}");
_logger.LogInformation($"更新规则-替换为 {serverRule}");
}
}
}

_logger.LogInformation($"规则同步完毕,当前本地规则共有 {_localRules.Count}");
}

}
}
Loading

0 comments on commit 9a0e27b

Please sign in to comment.