Skip to content

Commit

Permalink
feat(plugins/itodaysplus): 0.1.2: 调整 settings.json 结构
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyungent committed Jan 3, 2024
1 parent bcc83ac commit 9034e4c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 80 deletions.
144 changes: 80 additions & 64 deletions plugins/ITodaysPlus/ITodaysPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public long SecondsPeriod
{
get
{
var settings = PluginSettingsModelFactory.Create<SettingsModel>(nameof(ITodaysPlus));

return settings.SecondsPeriod;
return 1;
}
}

public DateTime ITodaysBackupLastExecute { get; set; }
#endregion

public override (bool IsSuccess, string Message) AfterEnable()
Expand All @@ -49,84 +49,100 @@ public async Task ExecuteAsync()
{
var settings = PluginSettingsModelFactory.Create<SettingsModel>(nameof(ITodaysPlus));

#region 备份到 本地
// 备份到 本地
if (settings.Backup.EnabledBackupToLocal)
#region 爱今天-备份记录
try
{
ITodaysUtil iTodaysUtil = new ITodaysUtil();
var jsonOptions = new JsonSerializerOptions
if (DateTime.Now - ITodaysBackupLastExecute > TimeSpan.FromSeconds(settings.Backup.SecondsPeriod))
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true
};
try
{
var loginResModel = iTodaysUtil.Login(settings.ITodays.UserName, settings.ITodays.Password);
if (loginResModel.status == "1")
ITodaysBackupLastExecute = DateTime.Now;

#region 备份到 本地
// 备份到 本地
if (settings.Backup.EnabledBackupToLocal)
{
// 登录成功
string folderPath = Path.Combine(PluginPathProvider.PluginsRootPath(), nameof(ITodaysPlus), "Backups");
if (!Directory.Exists(folderPath))
ITodaysUtil iTodaysUtil = new ITodaysUtil();
var jsonOptions = new JsonSerializerOptions
{
Directory.CreateDirectory(folderPath);
}
string loginResFilePath = Path.Combine(folderPath, "login.json");
await File.WriteAllTextAsync(path: loginResFilePath, System.Text.Json.JsonSerializer.Serialize(loginResModel, jsonOptions), Encoding.UTF8);

#region 备份日期记录
if (!string.IsNullOrEmpty(settings.Backup?.DateRecordStartTime)
&&
DateTime.TryParse(settings.Backup.DateRecordStartTime, out DateTime startTime)
&&
!string.IsNullOrEmpty(settings.Backup?.DateRecordEndTime)
&&
DateTime.TryParse(settings.Backup.DateRecordEndTime, out DateTime endTime))
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true
};
try
{
var folderDir = new DirectoryInfo(folderPath);
while (startTime <= endTime)
var loginResModel = iTodaysUtil.Login(settings.UserName, settings.Password);
if (loginResModel.status == "1")
{
string dateRecordFileName = $"DateRecord-{startTime.ToString("yyyy-MM-dd")}.json";
string dateRecordFilePath = Path.Combine(folderPath, dateRecordFileName);
Console.WriteLine($"{dateRecordFileName} - 尝试");
// 登录成功
string folderPath = Path.Combine(PluginPathProvider.PluginsRootPath(), nameof(ITodaysPlus), "Backups");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string loginResFilePath = Path.Combine(folderPath, "login.json");
await File.WriteAllTextAsync(path: loginResFilePath, System.Text.Json.JsonSerializer.Serialize(loginResModel, jsonOptions), Encoding.UTF8);

var dateRecordFileNames = folderDir.GetFiles("DateRecord-*.json")?.Select(m => m.Name)?.ToArray();
if (dateRecordFileNames != null && !dateRecordFileNames.Contains(dateRecordFileName))
#region 备份日期记录
if (!string.IsNullOrEmpty(settings.Backup?.DateRecordStartTime)
&&
DateTime.TryParse(settings.Backup.DateRecordStartTime, out DateTime startTime)
&&
!string.IsNullOrEmpty(settings.Backup?.DateRecordEndTime)
&&
DateTime.TryParse(settings.Backup.DateRecordEndTime, out DateTime endTime))
{
// 不存在此记录才 新增保存
try
var folderDir = new DirectoryInfo(folderPath);
while (startTime <= endTime)
{
var dateRecordResModel = iTodaysUtil.GetDateRecord(loginResModel.token, startTime);
if (dateRecordResModel.ResponseModel != null
&& dateRecordResModel.ResponseModel.status == 1
&& dateRecordResModel.DataModel != null
&& dateRecordResModel.DataModel.items.Length >= 1)
string dateRecordFileName = $"DateRecord-{startTime.ToString("yyyy-MM-dd")}.json";
string dateRecordFilePath = Path.Combine(folderPath, dateRecordFileName);
Console.WriteLine($"{dateRecordFileName} - 尝试");

var dateRecordFileNames = folderDir.GetFiles("DateRecord-*.json")?.Select(m => m.Name)?.ToArray();
if (dateRecordFileNames != null && !dateRecordFileNames.Contains(dateRecordFileName))
{
// 按日期/每一天 保存, 而不按每一条记录保存
await File.WriteAllTextAsync(path: dateRecordFilePath,
System.Text.Json.JsonSerializer.Serialize(dateRecordResModel.DataModel, jsonOptions),
Encoding.UTF8);
Console.WriteLine($"{dateRecordFileName} - 新增保存");
// 不存在此记录才 新增保存
try
{
var dateRecordResModel = iTodaysUtil.GetDateRecord(loginResModel.token, startTime);
if (dateRecordResModel.ResponseModel != null
&& dateRecordResModel.ResponseModel.status == 1
&& dateRecordResModel.DataModel != null
&& dateRecordResModel.DataModel.items.Length >= 1)
{
// 按日期/每一天 保存, 而不按每一条记录保存
await File.WriteAllTextAsync(path: dateRecordFilePath,
System.Text.Json.JsonSerializer.Serialize(dateRecordResModel.DataModel, jsonOptions),
Encoding.UTF8);
Console.WriteLine($"{dateRecordFileName} - 新增保存");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

// 避免频繁
Thread.Sleep(2000);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

// 避免频繁
Thread.Sleep(2000);
startTime = startTime.AddDays(1);
}
}

startTime = startTime.AddDays(1);
#endregion
}
}
#endregion
catch (Exception ex)
{
Console.WriteLine($"爱今天-备份记录: {ex.ToString()}");
}
}
#endregion

Console.WriteLine($"执行定时任务成功: 爱今天-备份记录");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine($"执行定时任务失败: 爱今天-备份记录: {ex.ToString()}");
}
#endregion

Expand Down
2 changes: 1 addition & 1 deletion plugins/ITodaysPlus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


<!-- Matomo Image Tracker-->
<img referrerpolicy="no-referrer-when-downgrade" src="https://matomo.moeci.com/matomo.php?idsite=2&amp;rec=1&amp;action_name=Plugins.ITodaysPlus-v0.1.1.README" style="border:0" alt="" />
<img referrerpolicy="no-referrer-when-downgrade" src="https://matomo.moeci.com/matomo.php?idsite=2&amp;rec=1&amp;action_name=Plugins.ITodaysPlus-v0.1.2.README" style="border:0" alt="" />
<!-- End Matomo -->


Expand Down
13 changes: 4 additions & 9 deletions plugins/ITodaysPlus/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ namespace ITodaysPlus
{
public class SettingsModel : PluginSettingsModel
{
public long SecondsPeriod { get; set; }
public string UserName { get; set; }

public ITodaysModel ITodays { get; set; }

public class ITodaysModel
{
public string UserName { get; set; }

public string Password { get; set; }
}
public string Password { get; set; }

public BackupModel Backup { get; set; }

public class BackupModel
{
public long SecondsPeriod { get; set; }

public bool EnabledBackupToLocal { get; set; }

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion plugins/ITodaysPlus/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"DisplayName": "iTodayss+",
"Description": "提升 爱今天/时间朋友 体验",
"Author": "yiyun",
"Version": "0.1.1",
"Version": "0.1.2",
"SupportedVersions": [ "0.0.1" ]
}
8 changes: 3 additions & 5 deletions plugins/ITodaysPlus/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"SecondsPeriod": 3600,
"ITodays": {
"UserName": "",
"Password": ""
},
"UserName": "",
"Password": "",
"Backup": {
"SecondsPeriod": 3600,
"EnabledBackupToLocal": false,
"DateRecordStartTime": "2018-01-01",
"DateRecordEndTime": "2023-01-01"
Expand Down

0 comments on commit 9034e4c

Please sign in to comment.