feat: final connection to send photos

This commit is contained in:
Guillermo Marcel 2025-02-19 12:54:09 -03:00
parent a6ac0187f1
commit cfc41907ef
5 changed files with 58 additions and 17 deletions

View File

@ -35,8 +35,8 @@ public class AutoScanApp
_logger.LogInformation("AutoScanApp is running...");
var at = DateTime.Now.AddSeconds(10);
var cron = $"{at.Second} {at.Minute} {at.Hour} * * ?";
//var cron = CronFromAt(_options.At);
// var cron = $"{at.Second} {at.Minute} {at.Hour} * * ?";
var cron = CronFromAt(_options.At);
_logger.LogInformation("Waiting for next scan at {At} [{cron}].", at, cron);
await _scheduler.Start(cancellationToken);

View File

@ -60,13 +60,13 @@ public class ShinobiConnector : IDVRConnector
try
{
_logger.LogDebug("Downloading video...");
var videoData = await _httpClient.GetByteArrayAsync(endpoint, cancellationToken);
if(runDry)
{
_logger.LogInformation("RunDry is enabled, skipping video download");
return;
}
var videoData = await _httpClient.GetByteArrayAsync(endpoint, cancellationToken);
//Write the video to the file
await File.WriteAllBytesAsync(videoPath, videoData, cancellationToken);
_logger.LogInformation("Video downloaded successfully!");

View File

@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Xml.Schema;
using Telegram.Bots;
using Telegram.Bots.Extensions.Polling;
using Telegram.Bots.Requests.Usernames;
@ -92,20 +93,53 @@ public class BotHandler : IUpdateHandler
public async Task UpdatePhotos(string[] paths)
{
if (_subscribers.Count == 0)
{
_logger.LogWarning("No subscribers to send message to");
return;
}
//separate into groups of 10 and send them
var groups = paths.Select((x, i) => (x, i)).GroupBy(x => x.i / 10).Select(x => x.Select(y => y.x).ToArray()).ToArray();
foreach (var group in groups)
{
await UpdatePhotosInt(group);
}
}
private async Task UpdatePhotosInt(string[] paths)
{
var streams = paths.Select(File.OpenRead).ToList();
var photos = streams.Select(stream => new PhotoFile(stream)).Cast<IGroupableMedia>().ToList();
foreach (var subscriber in _subscribers)
var s0 = _subscribers.FirstOrDefault()?.Id.ToString();
if(s0 is null)
{
var request = new SendMediaGroup(subscriber.Id.ToString(), photos);
await _bot.HandleAsync(request);
_logger.LogWarning("No subscribers to send message to");
return;
}
var request = new SendMediaGroup(s0, photos);
var response = await _bot.HandleAsync(request);
if (!response.Ok)
{
_logger.LogError("Error sending photos.");
}
if (_subscribers.Count < 1) return;
var caches = response.Result.Select(x => (x as PhotoMessage).PhotoSet.LastOrDefault()?.Id).ToList();
var media = caches.Select(x => new CachedPhoto(x)).ToList();
//send to the rest of the subscribers
foreach (var subscriber in _subscribers.Skip(1).ToList())
{
var request2 = new SendMediaGroup(subscriber.Id.ToString(), media);
var response2 = await _bot.HandleAsync(request2);
if (!response2.Ok)
{
_logger.LogError("Error sending photos to {Subscriber}", subscriber.Id);
}
}
foreach (var stream in streams)

View File

@ -64,12 +64,19 @@ botHandler.Start(cts.Token);
autoScanApp.OnScanCompleted = async options =>
{
logger.LogInformation("Scan completed at {At}", DateTime.Now);
try
{
//list all the images in the detection folder
if (options.Scanner?.DetectionFolder is null)
return;
var images = Directory.GetFiles(options.Scanner.DetectionFolder , "*.jpg");
botHandler.Update($"Scan completed, found {images.Length} images");
botHandler.UpdatePhotos(images);
}catch(Exception ex)
{
logger.LogError(ex, "Error while sending message");
}
//list all the images in the detection folder
if (options.Scanner?.DetectionFolder is null)
return;
var images = Directory.GetFiles(options.Scanner.DetectionFolder , "*.jpg");
botHandler.UpdatePhotos(images);
};

View File

@ -17,16 +17,16 @@
"URL": "http://localhost:8080",
"APIKey": "APIKEY",
"GroupId": "Group",
"MonitorId": "Monitor",
"MonitorId": "Monitor"
},
"AutoScan": {
"Enabled": true,
"RunDry": true,
"At": "07:00",
"FromDayBefore": true,
"From": "23:00",
"FromDayBefore": false,
"From": "00:45",
"To": "05:00",
"MaxAmount": 2,
"MaxAmount": 0,
"MediaFolder": "./media/originals/",
"Scanner": {
"Exe": "./dvr-scanner/dvr-scan.exe",