refactor: extract to handler
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
namespace StevesBot.Webhook.Handlers;
|
||||
|
||||
// TODO: Implement logic to do the following:
|
||||
// - if is stream create discord message
|
||||
internal static class NotificationHandler
|
||||
{
|
||||
public static async Task<IResult> HandleAsync(
|
||||
HttpContext context,
|
||||
[FromServices] ILogger<Program> logger,
|
||||
[FromServices] IYouTubeDataApiClient youTubeDataApiClient
|
||||
)
|
||||
{
|
||||
using StreamReader stream = new(context.Request.Body);
|
||||
var body = await stream.ReadToEndAsync();
|
||||
|
||||
var videoIdRegex = VideoIdRegex.Regex();
|
||||
var match = videoIdRegex.Match(body);
|
||||
|
||||
if (match.Success is false)
|
||||
{
|
||||
logger.LogWarning("No video ID found in the request body.");
|
||||
return Results.BadRequest("No video ID found in the request body.");
|
||||
}
|
||||
|
||||
var videoId = match.Groups[1].Value;
|
||||
var parts = new string[] { "liveStreamingDetails", "snippet" };
|
||||
var video = await youTubeDataApiClient.GetVideoByIdAsync(videoId, parts);
|
||||
|
||||
if (video is null)
|
||||
{
|
||||
logger.LogWarning("Video with ID {VideoId} not found.", videoId);
|
||||
return Results.NotFound();
|
||||
}
|
||||
|
||||
if (video.IsLiveStream is false)
|
||||
{
|
||||
logger.LogInformation("Video ID {VideoId} is not a live stream.", videoId);
|
||||
return Results.Ok();
|
||||
}
|
||||
|
||||
logger.LogInformation("Video ID {VideoId} is a live stream.", videoId);
|
||||
|
||||
return Results.Ok();
|
||||
}
|
||||
}
|
||||
|
||||
internal static partial class VideoIdRegex
|
||||
{
|
||||
[GeneratedRegex(@"<yt:videoId>(.*?)</yt:videoId>")]
|
||||
public static partial Regex Regex();
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace StevesBot.Webhook.YouTube;
|
||||
namespace StevesBot.Webhook.Handlers;
|
||||
|
||||
internal static class VerifySubscriptionHandler
|
||||
{
|
||||
@@ -1,11 +1,5 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.Configure<HostOptions>(
|
||||
static options => options.ShutdownTimeout = TimeSpan.FromSeconds(30)
|
||||
);
|
||||
|
||||
builder.Services
|
||||
.AddOptionsWithValidateOnStart<SubscriptionOptions>()
|
||||
.BindConfiguration(nameof(SubscriptionOptions))
|
||||
@@ -54,52 +48,8 @@ if (app.Environment.IsDevelopment())
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
// app.UseHttpsRedirection();
|
||||
|
||||
const string ytCallback = "yt-callback";
|
||||
|
||||
app.MapGet(ytCallback, VerifySubscriptionHandler.HandleAsync);
|
||||
app.MapPost(ytCallback, NotificationHandler.HandleAsync);
|
||||
|
||||
// TODO: Implement logic to do the following:
|
||||
// - if is stream create discord message
|
||||
app.MapPost(ytCallback, static async (HttpContext context, [FromServices] ILogger<Program> logger, [FromServices] IYouTubeDataApiClient youTubeDataApiClient) =>
|
||||
{
|
||||
var body = "";
|
||||
using StreamReader stream = new(context.Request.Body);
|
||||
body = await stream.ReadToEndAsync();
|
||||
|
||||
var videoIdRegex = VideoIdRegex();
|
||||
var match = videoIdRegex.Match(body);
|
||||
|
||||
if (match.Success is false)
|
||||
{
|
||||
logger.LogWarning("No video ID found in the request body.");
|
||||
}
|
||||
|
||||
var videoId = match.Groups[1].Value;
|
||||
var video = await youTubeDataApiClient.GetVideoByIdAsync(videoId, ["liveStreamingDetails"]);
|
||||
|
||||
if (video is null)
|
||||
{
|
||||
logger.LogWarning("Video with ID {VideoId} not found.", videoId);
|
||||
return Results.NotFound();
|
||||
}
|
||||
|
||||
if (video.IsLiveStream is false)
|
||||
{
|
||||
logger.LogInformation("Video ID {VideoId} is not a live stream.", videoId);
|
||||
return Results.Ok();
|
||||
}
|
||||
|
||||
logger.LogInformation("Video ID {VideoId} is a live stream.", videoId);
|
||||
|
||||
return Results.Ok();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
||||
internal partial class Program
|
||||
{
|
||||
[GeneratedRegex(@"<yt:videoId>(.*?)</yt:videoId>")]
|
||||
private static partial Regex VideoIdRegex();
|
||||
}
|
||||
app.Run();
|
||||
@@ -1,10 +1,12 @@
|
||||
global using System.Collections.Concurrent;
|
||||
global using System.ComponentModel.DataAnnotations;
|
||||
global using System.Text.Json.Serialization;
|
||||
global using System.Text.RegularExpressions;
|
||||
|
||||
global using Microsoft.AspNetCore.Mvc;
|
||||
global using Microsoft.Extensions.Options;
|
||||
|
||||
global using StevesBot.Webhook.Handlers;
|
||||
global using StevesBot.Webhook.YouTube;
|
||||
global using StevesBot.Webhook.YouTube.Data;
|
||||
global using StevesBot.Webhook.YouTube.Tasks;
|
||||
Reference in New Issue
Block a user