BililiveRecorder/BililiveRecorder.ToolBox/Tool/DanmakuStartTime/DanmakuStartTimeHandler.cs

70 lines
2.6 KiB
C#
Raw Normal View History

2021-11-21 00:08:31 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace BililiveRecorder.ToolBox.Tool.DanmakuStartTime
{
public class DanmakuStartTimeHandler : ICommandHandler<DanmakuStartTimeRequest, DanmakuStartTimeResponse>
{
public string Name => "Read Danmaku start_time";
public async Task<CommandResponse<DanmakuStartTimeResponse>> Handle(DanmakuStartTimeRequest request, CancellationToken cancellationToken, ProgressCallback? progress)
{
List<DanmakuStartTimeResponse.DanmakuStartTime> result = new();
try
{
progress?.Invoke(0);
var finished = 0;
double total = request.Inputs.Length;
Parallel.ForEach(request.Inputs, input =>
{
try
{
using var file = File.Open(input, FileMode.Open, FileAccess.Read, FileShare.Read);
var r = XmlReader.Create(file, null);
r.ReadStartElement("i");
while (r.Name != "i")
{
if (r.Name == "BililiveRecorderRecordInfo")
{
var el = (XNode.ReadFrom(r) as XElement)!;
var time = (DateTimeOffset)el.Attribute("start_time");
lock (result)
result.Add(new DanmakuStartTimeResponse.DanmakuStartTime { Path = input, StartTime = time });
2021-11-21 00:08:31 +08:00
Interlocked.Increment(ref finished);
2021-11-21 00:08:31 +08:00
progress?.Invoke(finished / total);
break;
}
else
{
r.Skip();
}
}
}
catch (Exception) { }
});
}
catch (Exception ex)
{
return new CommandResponse<DanmakuStartTimeResponse>
{
Status = ResponseStatus.Error,
Exception = ex,
ErrorMessage = ex.Message
};
}
return new CommandResponse<DanmakuStartTimeResponse> { Status = ResponseStatus.OK, Data = new DanmakuStartTimeResponse { StartTimes = result.ToArray() } };
}
}
}