feat: final connection to send photos
This commit is contained in:
parent
a6ac0187f1
commit
cfc41907ef
@ -35,8 +35,8 @@ public class AutoScanApp
|
|||||||
_logger.LogInformation("AutoScanApp is running...");
|
_logger.LogInformation("AutoScanApp is running...");
|
||||||
|
|
||||||
var at = DateTime.Now.AddSeconds(10);
|
var at = DateTime.Now.AddSeconds(10);
|
||||||
var cron = $"{at.Second} {at.Minute} {at.Hour} * * ?";
|
// var cron = $"{at.Second} {at.Minute} {at.Hour} * * ?";
|
||||||
//var cron = CronFromAt(_options.At);
|
var cron = CronFromAt(_options.At);
|
||||||
_logger.LogInformation("Waiting for next scan at {At} [{cron}].", at, cron);
|
_logger.LogInformation("Waiting for next scan at {At} [{cron}].", at, cron);
|
||||||
|
|
||||||
await _scheduler.Start(cancellationToken);
|
await _scheduler.Start(cancellationToken);
|
||||||
|
@ -60,13 +60,13 @@ public class ShinobiConnector : IDVRConnector
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Downloading video...");
|
_logger.LogDebug("Downloading video...");
|
||||||
var videoData = await _httpClient.GetByteArrayAsync(endpoint, cancellationToken);
|
|
||||||
|
|
||||||
if(runDry)
|
if(runDry)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("RunDry is enabled, skipping video download");
|
_logger.LogInformation("RunDry is enabled, skipping video download");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
var videoData = await _httpClient.GetByteArrayAsync(endpoint, cancellationToken);
|
||||||
|
|
||||||
//Write the video to the file
|
//Write the video to the file
|
||||||
await File.WriteAllBytesAsync(videoPath, videoData, cancellationToken);
|
await File.WriteAllBytesAsync(videoPath, videoData, cancellationToken);
|
||||||
_logger.LogInformation("Video downloaded successfully!");
|
_logger.LogInformation("Video downloaded successfully!");
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using System.Xml.Schema;
|
||||||
using Telegram.Bots;
|
using Telegram.Bots;
|
||||||
using Telegram.Bots.Extensions.Polling;
|
using Telegram.Bots.Extensions.Polling;
|
||||||
using Telegram.Bots.Requests.Usernames;
|
using Telegram.Bots.Requests.Usernames;
|
||||||
@ -92,20 +93,53 @@ public class BotHandler : IUpdateHandler
|
|||||||
|
|
||||||
public async Task UpdatePhotos(string[] paths)
|
public async Task UpdatePhotos(string[] paths)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (_subscribers.Count == 0)
|
if (_subscribers.Count == 0)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("No subscribers to send message to");
|
_logger.LogWarning("No subscribers to send message to");
|
||||||
return;
|
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 streams = paths.Select(File.OpenRead).ToList();
|
||||||
var photos = streams.Select(stream => new PhotoFile(stream)).Cast<IGroupableMedia>().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);
|
_logger.LogWarning("No subscribers to send message to");
|
||||||
await _bot.HandleAsync(request);
|
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)
|
foreach (var stream in streams)
|
||||||
|
@ -64,12 +64,19 @@ botHandler.Start(cts.Token);
|
|||||||
autoScanApp.OnScanCompleted = async options =>
|
autoScanApp.OnScanCompleted = async options =>
|
||||||
{
|
{
|
||||||
logger.LogInformation("Scan completed at {At}", DateTime.Now);
|
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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,16 +17,16 @@
|
|||||||
"URL": "http://localhost:8080",
|
"URL": "http://localhost:8080",
|
||||||
"APIKey": "APIKEY",
|
"APIKey": "APIKEY",
|
||||||
"GroupId": "Group",
|
"GroupId": "Group",
|
||||||
"MonitorId": "Monitor",
|
"MonitorId": "Monitor"
|
||||||
},
|
},
|
||||||
"AutoScan": {
|
"AutoScan": {
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"RunDry": true,
|
"RunDry": true,
|
||||||
"At": "07:00",
|
"At": "07:00",
|
||||||
"FromDayBefore": true,
|
"FromDayBefore": false,
|
||||||
"From": "23:00",
|
"From": "00:45",
|
||||||
"To": "05:00",
|
"To": "05:00",
|
||||||
"MaxAmount": 2,
|
"MaxAmount": 0,
|
||||||
"MediaFolder": "./media/originals/",
|
"MediaFolder": "./media/originals/",
|
||||||
"Scanner": {
|
"Scanner": {
|
||||||
"Exe": "./dvr-scanner/dvr-scan.exe",
|
"Exe": "./dvr-scanner/dvr-scan.exe",
|
||||||
|
Loading…
Reference in New Issue
Block a user