casa-bot/src/CasaBot/AutoScan/Jobs/DownloaderJob.cs

109 lines
4.1 KiB
C#
Raw Normal View History

using AutoScan.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Quartz;
namespace AutoScan.Jobs;
public class DownloaderJob : IJob
{
private readonly ILogger<DownloaderJob> _logger;
private readonly AutoScanOptions _options;
private readonly ShinobiConnector _shinobiConnector;
public DownloaderJob(ILogger<DownloaderJob> logger, IOptionsSnapshot<AutoScanOptions> options, ShinobiConnector shinobiConnector)
{
_logger = logger;
_options = options.Value;
_shinobiConnector = shinobiConnector;
}
public async Task Execute(IJobExecutionContext context)
{
_logger.LogInformation("Scheduled scan executed at {At}", DateTime.Now);
if (_options.MediaFolder is null)
{
_logger.LogError("MediaFolder is not set in options!");
context.Result = new JobResult()
{
Status = JobResultStatus.JobFailed,
Result = "MediaFolder is not set in options!"
};
return;
}
//create from variable with the datetime of last night with the variables in options.From (23:00) and options.FromDayBefore (true) [yesterday]
//for example, if options.From is 23:00 and options.FromDayBefore is true, from should be yesterday at 23:00
var now = DateTime.Now;
var minutes = _options.From.Split(":")[1];
var hours = _options.From.Split(":")[0];
var from = new DateTime(now.Year, now.Month, now.Day, int.Parse(hours), int.Parse(minutes), 0);
if(_options.FromDayBefore)
from = from.AddDays(-1);
//create to variable with the datetime of last night with the variables in options.To (1:00) [today]
//for example, if options.To is 1:00, to should be today at 1:00
minutes = _options.To.Split(":")[1];
hours = _options.To.Split(":")[0];
var to = new DateTime(now.Year, now.Month, now.Day, int.Parse(hours), int.Parse(minutes), 0);
_logger.LogInformation("Fetching videos from {From} to {To}", from, to);
var videos = await _shinobiConnector.FetchMonitorVideosBetween(from, to);
//if the amount of videos is greater than the max amount in options, log a warning
if (_options.MaxAmount > 0 && videos.Count > _options.MaxAmount)
{
_logger.LogWarning("Amount of videos fetched is greater than the max amount in options ({MaxAmount})", _options.MaxAmount);
videos = videos.Take(_options.MaxAmount).ToList();
}
CleanMediaFolder();
//download each video to the media folder
foreach (var video in videos)
{
_logger.LogDebug("Downloading video {Filename}", video.filename);
await _shinobiConnector.DownloadMonitorVideo(video, _options.MediaFolder);
}
context.Result = new JobResult()
{
Status = JobResultStatus.JobSucceeded,
Result = $"Downloaded {videos.Count} videos to {_options.MediaFolder}"
};
}
private void CleanMediaFolder()
{
if (_options.MediaFolder is not null)
{
Directory.CreateDirectory(Path.GetDirectoryName(_options.MediaFolder)!);
foreach (var file in Directory.GetFiles(_options.MediaFolder))
{
File.Delete(file);
}
}
if(_options.Scanner?.DetectionFolder is not null)
{
Directory.CreateDirectory(Path.GetDirectoryName(_options.Scanner.DetectionFolder)!);
foreach (var file in Directory.GetFiles(_options.Scanner.DetectionFolder))
{
File.Delete(file);
}
}
if(_options.Screenshot?.Folder is not null)
{
Directory.CreateDirectory(Path.GetDirectoryName(_options.Screenshot.Folder)!);
foreach (var file in Directory.GetFiles(_options.Screenshot.Folder))
{
File.Delete(file);
}
}
}
}