casa-bot/src/CasaBot/CasaBotApp/ShinobiConnector.cs
2025-02-13 19:10:25 -03:00

61 lines
2.1 KiB
C#

using Microsoft.Extensions.Logging;
namespace CasaBotApp;
public class ShinobiConnector
{
private readonly ILogger<ShinobiConnector> _logger;
private readonly string _shinobivUrl = "https://shinobi.francelsoft.com";
private readonly string _apikey = "OGD6nsGGzA1NL48M5Tg7Wbzto62oPl";
private readonly string _groupId = "aInxuWCYLI";
private readonly string _monitorId = "mQ3kQ5qjKK";
private readonly HttpClient _httpClient;
public ShinobiConnector(ILogger<ShinobiConnector> logger, HttpClient httpClient)
{
_logger = logger;
_httpClient = httpClient;
}
public async Task FetchLastVideo(string filename = "2025-02-12T08-00-01.mp4")
{
//fetch video from shinobi endpoint using example file "2025-02-12T08-00-01.mp4"
const string fetchVideoEndpoint = "/{0}/videos/{1}/{2}/{3}";
var endpoint = string.Format(_shinobivUrl+fetchVideoEndpoint, _apikey, _groupId, _monitorId, filename);
_logger.LogInformation("Fetching video from endpoint: {Endpoint}", endpoint);
//fetch video
const string mediaPath = @".\media\"; // Replace with your desired file path
var videoPath = mediaPath + filename;
try
{
//make sure the directory exists
Directory.CreateDirectory(Path.GetDirectoryName(mediaPath)!);
_logger.LogDebug("Cleaning media folder");
CleanDirectory(mediaPath);
_logger.LogDebug("Downloading video...");
var videoData = await _httpClient.GetByteArrayAsync(endpoint);
//Write the video to the file
await File.WriteAllBytesAsync(videoPath, videoData);
_logger.LogInformation("Video downloaded successfully!");
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while downloading the video");
}
}
private void CleanDirectory(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
foreach (var file in di.GetFiles())
{
file.Delete();
}
}
}