feat(news): dynamic settings, IFinlyticLogger, live log streaming, and EF migration
This commit is contained in:
@@ -1,21 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FinlyticCore.Dtos.News;
|
||||
using FinlyticCore.Services;
|
||||
using FinlyticNews.Adapters.Discovery;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using FinlyticNews.Util;
|
||||
|
||||
namespace FinlyticNews.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Defines operations for discovering article links from various news feeds and web pages.
|
||||
/// </summary>
|
||||
public interface IArticleDiscoveryService
|
||||
{
|
||||
/// <summary>
|
||||
/// Discovers article links from a given source URL using configured adapters.
|
||||
/// </summary>
|
||||
/// <param name="url">The URL of the source news page or feed.</param>
|
||||
/// <param name="adapterType">The type identifier of the adapter to use (e.g. "rss", "html").</param>
|
||||
/// <param name="ct">The token to monitor for cancellation requests.</param>
|
||||
/// <returns>A list of discovered absolute article URLs (optionally carrying ISINs), or null if the URL was invalid.</returns>
|
||||
Task<List<DiscoveredArticle>?> DiscoverLinksAsync(string url, string adapterType, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
@@ -23,22 +20,16 @@ public interface IArticleDiscoveryService
|
||||
public class ArticleDiscoveryService : IArticleDiscoveryService
|
||||
{
|
||||
private readonly IEnumerable<ArticleDiscoveryAdapter> _adapters;
|
||||
private readonly ILogger<ArticleDiscoveryService> _logger;
|
||||
private readonly IFinlyticLogger<ArticleDiscoveryService> _finlyticLogger;
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ArticleDiscoveryService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="adapters">Registered list of specialized adapters.</param>
|
||||
/// <param name="logger">The application logging channel.</param>
|
||||
/// <param name="httpClient">The HTTP client to fetch feeds.</param>
|
||||
public ArticleDiscoveryService(
|
||||
IEnumerable<ArticleDiscoveryAdapter> adapters,
|
||||
ILogger<ArticleDiscoveryService> logger,
|
||||
IFinlyticLogger<ArticleDiscoveryService> finlyticLogger,
|
||||
HttpClient httpClient)
|
||||
{
|
||||
_adapters = adapters;
|
||||
_logger = logger;
|
||||
_finlyticLogger = finlyticLogger;
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
@@ -47,37 +38,35 @@ public class ArticleDiscoveryService : IArticleDiscoveryService
|
||||
{
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] Invalid source URL passed for discovery: {Url}", "NewsChannel", url);
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[ArticleDiscoveryService] Invalid source URL passed for discovery: {Url}", url);
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("Fetching content from discovery source: {Url}", uri);
|
||||
await _finlyticLogger.LogDebugAsync(SettingKeys.NewsChannel, "[ArticleDiscoveryService] Fetching content from discovery source: {Url}", uri);
|
||||
var content = await _httpClient.GetStringAsync(uri, ct);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(content)) return [];
|
||||
|
||||
var trimmedContent = content.TrimStart();
|
||||
|
||||
// 1. Zuerst gezielt nach registriertem Adapter suchen (z. B. finanznachrichten_rss)
|
||||
var adapter = _adapters.FirstOrDefault(a =>
|
||||
a.Name.Equals(adapterType, StringComparison.OrdinalIgnoreCase) ||
|
||||
uri.Host.Contains(a.Name, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (adapter != null)
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Using specialized adapter {AdapterName} for source: {Url}", "NewsChannel", adapter.Name, url);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[ArticleDiscoveryService] Using specialized adapter {AdapterName} for source: {Url}", adapter.Name, url);
|
||||
return adapter.ExtractUrls(content, url);
|
||||
}
|
||||
|
||||
// 2. Fallback: Automatische Erkennung für generische RSS/Atom-Feeds
|
||||
if (adapterType.Equals("rss", StringComparison.OrdinalIgnoreCase) ||
|
||||
trimmedContent.StartsWith("<?xml", StringComparison.OrdinalIgnoreCase) ||
|
||||
trimmedContent.StartsWith("<rss", StringComparison.OrdinalIgnoreCase) ||
|
||||
trimmedContent.StartsWith("<feed", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Syndication (RSS) format detected for source: {Url}", "NewsChannel", url);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[ArticleDiscoveryService] Syndication (RSS) format detected for source: {Url}", url);
|
||||
|
||||
var rssAdapter = _adapters.FirstOrDefault(a => a.Name.Equals("rss", StringComparison.OrdinalIgnoreCase))
|
||||
?? new RssDiscoveryAdapter();
|
||||
@@ -85,12 +74,12 @@ public class ArticleDiscoveryService : IArticleDiscoveryService
|
||||
return rssAdapter.ExtractUrls(content, url);
|
||||
}
|
||||
|
||||
_logger.LogWarning("[{Channel}] No suitable discovery adapter found for type '{Type}' and URL: {Url}", "NewsChannel", adapterType, url);
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[ArticleDiscoveryService] No suitable discovery adapter found for type '{Type}' and URL: {Url}", adapterType, url);
|
||||
return [];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Failed to run article discovery on URL: {Url}", "NewsChannel", url);
|
||||
await _finlyticLogger.LogErrorAsync(SettingKeys.NewsChannel, ex, "[ArticleDiscoveryService] Failed to run article discovery on URL: {Url}", url);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FinlyticCore.Dtos.News;
|
||||
using FinlyticCore.Services;
|
||||
using FinlyticCore.Util;
|
||||
using FinlyticNews.Util;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FinlyticNews.Services;
|
||||
|
||||
using FinlyticCore.Dtos.News;
|
||||
|
||||
/// <summary>
|
||||
/// Defines integration operations with the external n8n AI workflow webhook.
|
||||
/// </summary>
|
||||
@@ -26,21 +30,18 @@ public class N8nService : IN8nService
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<N8nService> _logger;
|
||||
private readonly IFinlyticLogger<N8nService> _finlyticLogger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="N8nService"/> class.
|
||||
/// </summary>
|
||||
public N8nService(
|
||||
HttpClient httpClient,
|
||||
IServiceScopeFactory scopeFactory,
|
||||
IConfiguration configuration,
|
||||
ILogger<N8nService> logger)
|
||||
IFinlyticLogger<N8nService> finlyticLogger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_scopeFactory = scopeFactory;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
_finlyticLogger = finlyticLogger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -48,7 +49,6 @@ public class N8nService : IN8nService
|
||||
{
|
||||
string? targetUrl = null;
|
||||
|
||||
// 1. Dynamic Settings Resolution (DB Scope -> AppSettings Fallback)
|
||||
using (var scope = _scopeFactory.CreateScope())
|
||||
{
|
||||
var settingsDb = scope.ServiceProvider.GetService<ISettingsDbService>();
|
||||
@@ -67,17 +67,16 @@ public class N8nService : IN8nService
|
||||
|
||||
if (string.IsNullOrWhiteSpace(targetUrl))
|
||||
{
|
||||
_logger.LogError("[{Channel}] N8nWebhookUrl is not configured in DB or application settings.", "NewsChannel");
|
||||
await _finlyticLogger.LogErrorAsync(SettingKeys.NewsChannel, "[N8nService] N8nWebhookUrl is not configured in DB or application settings.");
|
||||
return null;
|
||||
}
|
||||
|
||||
_logger.LogInformation("[{Channel}] Posting article to n8n webhook pipeline at: {Url}", "NewsChannel", targetUrl);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[N8nService] Posting article to n8n webhook pipeline at: {Url}", targetUrl);
|
||||
|
||||
var payload = new N8nRequestPayload(content, filteredAssets ?? []);
|
||||
|
||||
try
|
||||
{
|
||||
// Zero-Allocation / Source-Generated Request Serialization
|
||||
var jsonContent = JsonSerializer.Serialize(payload, FinlyticJsonSerializerContext.Default.N8nRequestPayload);
|
||||
using var requestContent = new StringContent(jsonContent, System.Text.Encoding.UTF8, "application/json");
|
||||
|
||||
@@ -86,7 +85,7 @@ public class N8nService : IN8nService
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorMsg = await response.Content.ReadAsStringAsync(ct);
|
||||
_logger.LogError("[{Channel}] n8n webhook returned status code {StatusCode}. Error payload: {Error}", "NewsChannel", response.StatusCode, errorMsg);
|
||||
await _finlyticLogger.LogErrorAsync(SettingKeys.NewsChannel, "[N8nService] n8n webhook returned status code {StatusCode}. Error payload: {Error}", response.StatusCode, errorMsg);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -95,18 +94,16 @@ public class N8nService : IN8nService
|
||||
|
||||
var root = doc.RootElement;
|
||||
|
||||
// 2. Robust n8n Array-Unwrapping (Handles [{ "json": { ... } }])
|
||||
if (root.ValueKind == JsonValueKind.Array)
|
||||
{
|
||||
if (root.GetArrayLength() == 0)
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] n8n webhook returned an empty array.", "NewsChannel");
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[N8nService] n8n webhook returned an empty array.");
|
||||
return null;
|
||||
}
|
||||
root = root[0];
|
||||
}
|
||||
|
||||
// 3. Dynamic Node Wrapper Unwrapping ("json", "output", "data", "body")
|
||||
if (root.ValueKind == JsonValueKind.Object)
|
||||
{
|
||||
if (root.TryGetProperty("json", out var jsonChild) && jsonChild.ValueKind == JsonValueKind.Object)
|
||||
@@ -119,13 +116,12 @@ public class N8nService : IN8nService
|
||||
root = bodyChild;
|
||||
}
|
||||
|
||||
// 4. Source-Generated Deserialization directly from JsonElement
|
||||
var result = root.Deserialize(FinlyticJsonSerializerContext.Default.N8nResponsePayload);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Failed to communicate with or parse response from n8n webhook workflow.", "NewsChannel");
|
||||
await _finlyticLogger.LogErrorAsync(SettingKeys.NewsChannel, ex, "[N8nService] Failed to communicate with or parse response from n8n webhook workflow.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using FinlyticCore.Dtos.News;
|
||||
using FinlyticCore.Services;
|
||||
using FinlyticNews.Database;
|
||||
using FinlyticNews.Entities;
|
||||
using FinlyticNews.Util;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FinlyticNews.Services;
|
||||
@@ -39,22 +45,11 @@ public interface INewsDbService
|
||||
/// </summary>
|
||||
Task<NewsArticleEntity?> SaveArticleClassificationAsync(Guid id, N8nResponsePayload payload, List<MatchedAssetEntity> matchedAssets);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves articles ready for historical sync or sentiment processing.
|
||||
/// </summary>
|
||||
Task<List<NewsArticleEntity>> GetCompletedArticlesAsync(int limit, int offset, string? isin = null);
|
||||
Task<List<NewsArticleEntity>> GetArticlesByStatusAsync(string status);
|
||||
|
||||
/// <summary>
|
||||
/// Fetches articles matching specific lifecycle statuses (e.g. "Pending", "Scraping" for Phase 1 retry).
|
||||
/// </summary>
|
||||
Task<List<NewsArticleEntity>> GetArticlesByStatusAsync(params string[] statuses);
|
||||
|
||||
/// <summary>
|
||||
/// Fetches public daily news for API endpoints, filtering out intermediate or failed lifecycle states by default.
|
||||
/// </summary>
|
||||
Task<List<NewsArticleEntity>> GetFilteredNewsAsync(
|
||||
int limit = 20,
|
||||
int offset = 0,
|
||||
int limit,
|
||||
int offset,
|
||||
string? isin = null,
|
||||
DateTime? date = null,
|
||||
string? status = null,
|
||||
@@ -67,15 +62,12 @@ public interface INewsDbService
|
||||
public class NewsDbService : INewsDbService
|
||||
{
|
||||
private readonly NewsDbContext _context;
|
||||
private readonly ILogger<NewsDbService> _logger;
|
||||
private readonly IFinlyticLogger<NewsDbService> _finlyticLogger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="NewsDbService"/> class.
|
||||
/// </summary>
|
||||
public NewsDbService(NewsDbContext context, ILogger<NewsDbService> logger)
|
||||
public NewsDbService(NewsDbContext context, IFinlyticLogger<NewsDbService> finlyticLogger)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
_finlyticLogger = finlyticLogger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -103,9 +95,6 @@ public class NewsDbService : INewsDbService
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Lifecycle Step 1: Creates a new article in 'Pending' state as an immediate lock.
|
||||
/// </summary>
|
||||
public async Task<NewsArticleEntity> CreatePendingArticleAsync(
|
||||
string url,
|
||||
List<string>? discoveredIsins = null,
|
||||
@@ -121,7 +110,7 @@ public class NewsDbService : INewsDbService
|
||||
|
||||
if (existingArticle != null)
|
||||
{
|
||||
_logger.LogDebug("[Lifecycle] Article URL already exists (Duplicate hit): {Url}", trimmedUrl);
|
||||
await _finlyticLogger.LogDebugAsync(SettingKeys.NewsChannel, "[Lifecycle] Article URL already exists (Duplicate hit): {Url}", trimmedUrl);
|
||||
return existingArticle;
|
||||
}
|
||||
|
||||
@@ -146,7 +135,7 @@ public class NewsDbService : INewsDbService
|
||||
Language = language,
|
||||
ScrapedAt = DateTime.UtcNow,
|
||||
PublishedAt = finalPublishedAt,
|
||||
Status = "Pending" // 1. Pending State
|
||||
Status = "Pending"
|
||||
};
|
||||
|
||||
if (discoveredIsins != null && discoveredIsins.Count > 0)
|
||||
@@ -167,11 +156,11 @@ public class NewsDbService : INewsDbService
|
||||
{
|
||||
_context.NewsArticles.Add(article);
|
||||
await _context.SaveChangesAsync();
|
||||
_logger.LogDebug("[Lifecycle] Registered new article with status 'Pending'. ID: {Id}, Url: {Url}", article.Id, trimmedUrl);
|
||||
await _finlyticLogger.LogDebugAsync(SettingKeys.NewsChannel, "[Lifecycle] Registered new article with status 'Pending'. ID: {Id}, Url: {Url}", article.Id, trimmedUrl);
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] Concurrency hit during insert for URL: {Url}. Fetching existing fallback.", "NewsChannel", trimmedUrl);
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[NewsChannel] Concurrency hit during insert for URL: {Url}. Fetching existing fallback.", trimmedUrl);
|
||||
return await _context.NewsArticles.FirstAsync(a => a.SourceUrl == trimmedUrl);
|
||||
}
|
||||
|
||||
@@ -179,9 +168,6 @@ public class NewsDbService : INewsDbService
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Lifecycle Step 2 & 5: Updates state (e.g. Pending -> Processing -> Scraping / Failed / Analyzed).
|
||||
/// </summary>
|
||||
public async Task UpdateArticleStatusAsync(Guid id, string status)
|
||||
{
|
||||
var rowsAffected = await _context.NewsArticles
|
||||
@@ -190,11 +176,11 @@ public class NewsDbService : INewsDbService
|
||||
|
||||
if (rowsAffected == 0)
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] Attempted status transition for non-existing article. ID: {Id}", "NewsChannel", id);
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[NewsChannel] Attempted status transition for non-existing article. ID: {Id}", id);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("[Lifecycle] Transitioned article {Id} to status '{Status}'", id, status);
|
||||
await _finlyticLogger.LogDebugAsync(SettingKeys.NewsChannel, "[Lifecycle] Transitioned article {Id} to status '{Status}'", id, status);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,18 +193,15 @@ public class NewsDbService : INewsDbService
|
||||
|
||||
if (rowsAffected == 0)
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] Attempted URL update for non-existing article. ID: {Id}", "NewsChannel", id);
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[NewsChannel] Attempted URL update for non-existing article. ID: {Id}", id);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("[Lifecycle] Resolved redirect for article {Id} -> New URL: {Url}", id, resolvedUrl);
|
||||
await _finlyticLogger.LogDebugAsync(SettingKeys.NewsChannel, "[Lifecycle] Resolved redirect for article {Id} -> New URL: {Url}", id, resolvedUrl);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Lifecycle Step 4: Persists n8n classification and transitions status to 'Completed'.
|
||||
/// </summary>
|
||||
public async Task<NewsArticleEntity?> SaveArticleClassificationAsync(Guid id, N8nResponsePayload payload, List<MatchedAssetEntity> matchedAssets)
|
||||
{
|
||||
var article = await _context.NewsArticles
|
||||
@@ -226,7 +209,7 @@ public class NewsDbService : INewsDbService
|
||||
|
||||
if (article == null)
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] Article with ID {Id} not found for classification update.", "NewsChannel", id);
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[NewsChannel] Article with ID {Id} not found for classification update.", id);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -235,8 +218,6 @@ public class NewsDbService : INewsDbService
|
||||
article.Summary = payload.Summary;
|
||||
article.ContentRaw = payload.ContentRaw;
|
||||
article.Language = payload.Language;
|
||||
|
||||
// 🎯 Step 4: Classification finished -> Transition to 'Completed' (triggers MQTT broadcast)
|
||||
article.Status = "Completed";
|
||||
|
||||
if (DateTime.TryParse(payload.PublishedAt, out var publishedDate))
|
||||
@@ -251,7 +232,6 @@ public class NewsDbService : INewsDbService
|
||||
article.ScrapedAt = scrapedDate.ToUniversalTime();
|
||||
}
|
||||
|
||||
// Clean up previous temporary assets
|
||||
await _context.MatchedAssets.Where(m => m.NewsArticleId == id).ExecuteDeleteAsync();
|
||||
|
||||
article.MatchedAssets = new List<MatchedAssetEntity>();
|
||||
@@ -267,113 +247,65 @@ public class NewsDbService : INewsDbService
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
_logger.LogInformation("[Lifecycle] Article {Id} successfully classified and marked 'Completed'. Title: '{Title}'", article.Id, article.Title);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[Lifecycle] Article {Id} successfully classified and marked 'Completed'. Title: '{Title}'", article.Id, article.Title);
|
||||
|
||||
return article;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<List<NewsArticleEntity>> GetCompletedArticlesAsync(int limit, int offset, string? isin = null)
|
||||
public async Task<List<NewsArticleEntity>> GetArticlesByStatusAsync(string status)
|
||||
{
|
||||
var query = _context.NewsArticles
|
||||
return await _context.NewsArticles
|
||||
.Include(a => a.MatchedAssets)
|
||||
.Where(a => a.Status == "Completed" || a.Status == "Analyzed");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(isin))
|
||||
{
|
||||
var cleanIsin = isin.Trim();
|
||||
query = query.Where(a => a.MatchedAssets.Any(m => m.Isin == cleanIsin || m.Name == cleanIsin));
|
||||
}
|
||||
|
||||
return await query
|
||||
.Where(a => a.Status == status)
|
||||
.OrderByDescending(a => a.PublishedAt)
|
||||
.ThenByDescending(a => a.ScrapedAt)
|
||||
.ThenByDescending(a => a.Id)
|
||||
.Skip(offset)
|
||||
.Take(limit)
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Lifecycle Helper: Retrieves articles by target lifecycle status (e.g. "Pending", "Scraping" for Phase 1 Retry).
|
||||
/// </summary>
|
||||
public async Task<List<NewsArticleEntity>> GetArticlesByStatusAsync(params string[] statuses)
|
||||
{
|
||||
IQueryable<NewsArticleEntity> query = _context.NewsArticles
|
||||
.Include(a => a.MatchedAssets)
|
||||
.AsNoTracking();
|
||||
|
||||
if (statuses != null && statuses.Length > 0)
|
||||
{
|
||||
var cleanStatuses = statuses.Select(s => s.Trim()).ToList();
|
||||
query = query.Where(a => cleanStatuses.Contains(a.Status));
|
||||
}
|
||||
else
|
||||
{
|
||||
query = query.Where(a => a.Status != "Failed" && a.Status != "Duplicate");
|
||||
}
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// API Gateway Helper: Retrieves articles for UI rendering, excluding intermediate/failed states by default.
|
||||
/// </summary>
|
||||
public async Task<List<NewsArticleEntity>> GetFilteredNewsAsync(
|
||||
int limit = 20,
|
||||
int offset = 0,
|
||||
int limit,
|
||||
int offset,
|
||||
string? isin = null,
|
||||
DateTime? date = null,
|
||||
string? status = null,
|
||||
string? searchQuery = null)
|
||||
{
|
||||
IQueryable<NewsArticleEntity> query = _context.NewsArticles
|
||||
var query = _context.NewsArticles
|
||||
.Include(a => a.MatchedAssets)
|
||||
.AsNoTracking();
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
|
||||
// 1. Status Filter
|
||||
if (!string.IsNullOrWhiteSpace(status))
|
||||
{
|
||||
var targetStatus = status.Trim();
|
||||
query = query.Where(a => a.Status == targetStatus);
|
||||
}
|
||||
else
|
||||
{
|
||||
// By default, only show fully processed articles to the API/UI
|
||||
query = query.Where(a => a.Status == "Completed" || a.Status == "Analyzed");
|
||||
}
|
||||
|
||||
// 2. Date Filter
|
||||
if (date.HasValue)
|
||||
{
|
||||
// Erstelle ein exaktes UTC-Datum von 00:00:00 Uhr am gebuchten Tag
|
||||
var targetDate = new DateTime(date.Value.Year, date.Value.Month, date.Value.Day, 0, 0, 0, DateTimeKind.Utc);
|
||||
var nextDate = targetDate.AddDays(1);
|
||||
|
||||
query = query.Where(a => a.PublishedAt >= targetDate && a.PublishedAt < nextDate);
|
||||
}
|
||||
|
||||
// 3. ISIN / Symbol Filter
|
||||
if (!string.IsNullOrWhiteSpace(isin))
|
||||
{
|
||||
var cleanIsin = isin.Trim();
|
||||
query = query.Where(a => a.MatchedAssets.Any(m => m.Isin == cleanIsin || m.Name == cleanIsin));
|
||||
query = query.Where(a => a.MatchedAssets.Any(m => m.Isin == cleanIsin));
|
||||
}
|
||||
|
||||
if (date.HasValue)
|
||||
{
|
||||
var startUtc = date.Value.Date.ToUniversalTime();
|
||||
var endUtc = startUtc.AddDays(1);
|
||||
query = query.Where(a => a.PublishedAt >= startUtc && a.PublishedAt < endUtc);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(status))
|
||||
{
|
||||
query = query.Where(a => a.Status == status);
|
||||
}
|
||||
|
||||
// 4. Search Term
|
||||
if (!string.IsNullOrWhiteSpace(searchQuery))
|
||||
{
|
||||
var q = searchQuery.Trim();
|
||||
query = query.Where(a => EF.Functions.ILike(a.Title, $"%{q}%") || (a.Summary != null && EF.Functions.ILike(a.Summary, $"%{q}%")));
|
||||
var cleanSearch = searchQuery.Trim().ToLower();
|
||||
query = query.Where(a =>
|
||||
a.Title.ToLower().Contains(cleanSearch) ||
|
||||
a.Summary.ToLower().Contains(cleanSearch) ||
|
||||
a.MatchedAssets.Any(m => m.Name.ToLower().Contains(cleanSearch)));
|
||||
}
|
||||
|
||||
return await query
|
||||
.OrderByDescending(a => a.PublishedAt)
|
||||
.ThenByDescending(a => a.ScrapedAt)
|
||||
.ThenByDescending(a => a.Id)
|
||||
.Skip(offset)
|
||||
.Take(limit)
|
||||
.ToListAsync();
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FinlyticAssets.Models;
|
||||
using FinlyticAssets.Util;
|
||||
using FinlyticCore.Dtos.News;
|
||||
using FinlyticCore.Services;
|
||||
using FinlyticCore.Util;
|
||||
using FinlyticNews.Entities;
|
||||
using FinlyticNews.Util;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FinlyticNews.Services;
|
||||
|
||||
@@ -22,9 +26,6 @@ namespace FinlyticNews.Services;
|
||||
/// </summary>
|
||||
public class NewsScraperBackgroundService : BackgroundService
|
||||
{
|
||||
/// <summary>
|
||||
/// Internal wrapper to associate compiled regex patterns with the unmodified AssetIndex record.
|
||||
/// </summary>
|
||||
private record CompiledAssetMatcher(
|
||||
AssetIndex Asset,
|
||||
string CoreName,
|
||||
@@ -33,63 +34,63 @@ public class NewsScraperBackgroundService : BackgroundService
|
||||
);
|
||||
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly ILogger<NewsScraperBackgroundService> _logger;
|
||||
private readonly IFinlyticLogger<NewsScraperBackgroundService> _finlyticLogger;
|
||||
private readonly NewsMqttClient _mqttClient;
|
||||
private readonly int _intervalMinutes;
|
||||
private readonly string _indexPath;
|
||||
|
||||
// In-Memory Cache for compiled asset matchers to prevent re-reading & re-compiling Regex
|
||||
private List<CompiledAssetMatcher>? _cachedAssetMatchers;
|
||||
private DateTime _lastIndexLoadTime = DateTime.MinValue;
|
||||
|
||||
public NewsScraperBackgroundService(
|
||||
IServiceScopeFactory scopeFactory,
|
||||
ILogger<NewsScraperBackgroundService> logger,
|
||||
IFinlyticLogger<NewsScraperBackgroundService> finlyticLogger,
|
||||
NewsMqttClient mqttClient,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_logger = logger;
|
||||
_finlyticLogger = finlyticLogger;
|
||||
_mqttClient = mqttClient;
|
||||
|
||||
_intervalMinutes = configuration.GetValue<int>("ScrapingSettings:IntervalMinutes", 15);
|
||||
_indexPath = Path.Combine(Volumes.IndexRelativePath, "index.json");
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] NewsScraperBackgroundService started. Interval: {Minutes} minutes.", "NewsChannel", _intervalMinutes);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] NewsScraperBackgroundService started.");
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await RunScrapingCycleAsync(stoppingToken);
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var settings = scope.ServiceProvider.GetRequiredService<ISettingsService>();
|
||||
bool enabled = await settings.GetSettingAsync(SettingKeys.EnableAutoScraping, stoppingToken);
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
await RunScrapingCycleAsync(stoppingToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Auto-scraping is disabled via settings.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] An unhandled exception occurred during news scraping cycle.", "NewsChannel");
|
||||
await _finlyticLogger.LogErrorAsync(SettingKeys.NewsChannel, ex, "[NewsScraperBackgroundService] An unhandled exception occurred during news scraping cycle.");
|
||||
}
|
||||
|
||||
int intervalMinutes = _intervalMinutes;
|
||||
int intervalMinutes = 15;
|
||||
try
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var settingsDb = scope.ServiceProvider.GetService<ISettingsDbService>();
|
||||
if (settingsDb != null)
|
||||
{
|
||||
var settings = await settingsDb.GetSettingsAsync();
|
||||
if (settings?.ScrapingIntervalMinutes > 0)
|
||||
{
|
||||
intervalMinutes = settings.ScrapingIntervalMinutes;
|
||||
}
|
||||
}
|
||||
var settings = scope.ServiceProvider.GetRequiredService<ISettingsService>();
|
||||
intervalMinutes = await settings.GetSettingAsync(SettingKeys.ScrapeIntervalMinutes, stoppingToken);
|
||||
}
|
||||
catch { /* Ignore settings DB lookup failures */ }
|
||||
catch { }
|
||||
|
||||
var jitterSeconds = Random.Shared.Next(0, 300);
|
||||
var jitterSeconds = Random.Shared.Next(0, 60);
|
||||
var nextRunDelay = TimeSpan.FromMinutes(intervalMinutes) + TimeSpan.FromSeconds(jitterSeconds);
|
||||
_logger.LogInformation("[{Channel}] Scraping cycle completed. Next cycle in {Delay} (interval: {Minutes}m).", "NewsChannel", nextRunDelay, intervalMinutes);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Scraping cycle completed. Next cycle in {Delay} (interval: {Minutes}m).", nextRunDelay, intervalMinutes);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -101,7 +102,7 @@ public class NewsScraperBackgroundService : BackgroundService
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("[{Channel}] NewsScraperBackgroundService stopping.", "NewsChannel");
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] NewsScraperBackgroundService stopping.");
|
||||
}
|
||||
|
||||
private async Task RunScrapingCycleAsync(CancellationToken stoppingToken)
|
||||
@@ -111,28 +112,28 @@ public class NewsScraperBackgroundService : BackgroundService
|
||||
var discoveryService = scope.ServiceProvider.GetRequiredService<IArticleDiscoveryService>();
|
||||
var scraperService = scope.ServiceProvider.GetRequiredService<IPlaywrightScraperService>();
|
||||
var n8nService = scope.ServiceProvider.GetRequiredService<IN8nService>();
|
||||
var settings = scope.ServiceProvider.GetRequiredService<ISettingsService>();
|
||||
|
||||
var maxArticlesPerFeed = await settings.GetSettingAsync(SettingKeys.MaxArticlesPerFeed, stoppingToken);
|
||||
|
||||
// Load pre-compiled asset index matchers for zero-latency pre-filtering
|
||||
var assetMatchers = await GetOrLoadAssetMatchersAsync();
|
||||
_logger.LogInformation("[{Channel}] Loaded {Count} asset index items for text pre-filtering.", "NewsChannel", assetMatchers.Count);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Loaded {Count} asset index items for text pre-filtering.", assetMatchers.Count);
|
||||
|
||||
// 1. Scraping Retry Phase: query articles in status "Scraping" (failed Playwright runs)
|
||||
var failedArticles = await dbService.GetArticlesByStatusAsync("Scraping");
|
||||
if (failedArticles.Count > 0)
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Found {Count} articles in status 'Scraping' that failed to scrape previously. Retrying...", "NewsChannel", failedArticles.Count);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Found {Count} articles in status 'Scraping' that failed to scrape previously. Retrying...", failedArticles.Count);
|
||||
foreach (var article in failedArticles)
|
||||
{
|
||||
if (stoppingToken.IsCancellationRequested) break;
|
||||
await ProcessSingleArticleAsync(article, dbService, scraperService, n8nService, assetMatchers, stoppingToken);
|
||||
if (stoppingToken.IsCancellationRequested) return;
|
||||
await ProcessSingleArticleAsync(article, scraperService, n8nService, dbService, assetMatchers, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Link Discovery Phase: query RSS feeds and listing pages
|
||||
var sources = await dbService.GetSourcesAsync();
|
||||
if (sources.Count == 0)
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] No article sources configured in database. Skipping cycle.", "NewsChannel");
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] No article sources configured in database. Skipping cycle.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -140,86 +141,80 @@ public class NewsScraperBackgroundService : BackgroundService
|
||||
{
|
||||
if (stoppingToken.IsCancellationRequested) break;
|
||||
|
||||
_logger.LogInformation("[{Channel}] Starting article link discovery for source: {SourceName} ({Url})", "NewsChannel", source.Name, source.Source);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Starting article link discovery for source: {SourceName} ({Url})", source.Name, source.Source);
|
||||
var discoveredArticles = await discoveryService.DiscoverLinksAsync(source.Source, source.Type, stoppingToken);
|
||||
|
||||
if (discoveredArticles == null || discoveredArticles.Count == 0)
|
||||
{
|
||||
_logger.LogDebug("No links discovered from source: {SourceName}", source.Name);
|
||||
await _finlyticLogger.LogDebugAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] No links discovered from source: {SourceName}", source.Name);
|
||||
continue;
|
||||
}
|
||||
|
||||
_logger.LogInformation("[{Channel}] Discovered {Count} potential article links from {SourceName}.", "NewsChannel", discoveredArticles.Count, source.Name);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Discovered {Count} potential article links from {SourceName}.", discoveredArticles.Count, source.Name);
|
||||
|
||||
foreach (var discovered in discoveredArticles)
|
||||
var toProcess = discoveredArticles.Take(maxArticlesPerFeed > 0 ? maxArticlesPerFeed : 20);
|
||||
|
||||
foreach (var discovered in toProcess)
|
||||
{
|
||||
if (stoppingToken.IsCancellationRequested) break;
|
||||
|
||||
// Idempotency Check & Deduplication
|
||||
var isDuplicate = await dbService.IsUrlDuplicateAsync(discovered.Url);
|
||||
if (isDuplicate)
|
||||
if (await dbService.IsUrlDuplicateAsync(discovered.Url))
|
||||
{
|
||||
_logger.LogDebug("Skipping duplicate article URL: {Url}", discovered.Url);
|
||||
await _finlyticLogger.LogDebugAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Skipping duplicate article URL: {Url}", discovered.Url);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Register Initial Lock State in the database ("Pending")
|
||||
NewsArticleEntity? article;
|
||||
NewsArticleEntity article;
|
||||
try
|
||||
{
|
||||
article = await dbService.CreatePendingArticleAsync(
|
||||
discovered.Url,
|
||||
discovered.Isins,
|
||||
discovered.Title,
|
||||
discovered.Summary,
|
||||
discovered.PublishedAt,
|
||||
discovered.Language);
|
||||
discovered.Url,
|
||||
discovered.Isins,
|
||||
discovered.Title,
|
||||
discovered.Summary,
|
||||
discovered.PublishedAt,
|
||||
discovered.Language
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Failed to register initial pending state for URL: {Url}. Skipping.", "NewsChannel", discovered.Url);
|
||||
await _finlyticLogger.LogErrorAsync(SettingKeys.NewsChannel, ex, "[NewsScraperBackgroundService] Failed to register initial pending state for URL: {Url}. Skipping.", discovered.Url);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (article == null || article.Id == Guid.Empty)
|
||||
if (article.Id == Guid.Empty)
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] Created pending article has invalid/empty ID for URL: {Url}. Skipping.", "NewsChannel", discovered.Url);
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Created pending article has invalid/empty ID for URL: {Url}. Skipping.", discovered.Url);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Process single article pipeline
|
||||
await ProcessSingleArticleAsync(article, dbService, scraperService, n8nService, assetMatchers, stoppingToken);
|
||||
await ProcessSingleArticleAsync(article, scraperService, n8nService, dbService, assetMatchers, stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessSingleArticleAsync(
|
||||
NewsArticleEntity article,
|
||||
INewsDbService dbService,
|
||||
IPlaywrightScraperService scraperService,
|
||||
IN8nService n8nService,
|
||||
INewsDbService dbService,
|
||||
List<CompiledAssetMatcher> assetMatchers,
|
||||
CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 3. Extraction with Headless Browser (Transitions to "Processing")
|
||||
await dbService.UpdateArticleStatusAsync(article.Id, "Processing");
|
||||
var (resolvedUrl, rawText) = await scraperService.ScrapeArticleAsync(article.SourceUrl);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(rawText))
|
||||
{
|
||||
throw new InvalidOperationException("Scraping returned empty text body content.");
|
||||
}
|
||||
var (resolvedUrl, rawContent) = await scraperService.ScrapeArticleAsync(article.SourceUrl);
|
||||
|
||||
// Update resolved URL if redirect occurred
|
||||
if (!string.Equals(resolvedUrl, article.SourceUrl, StringComparison.OrdinalIgnoreCase))
|
||||
if (!string.IsNullOrWhiteSpace(resolvedUrl) &&
|
||||
!resolvedUrl.Equals(article.SourceUrl, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Redirect detected. Initial: {OldUrl} -> Resolved: {NewUrl}", "NewsChannel", article.SourceUrl, resolvedUrl);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Redirect detected. Initial: {OldUrl} -> Resolved: {NewUrl}", article.SourceUrl, resolvedUrl);
|
||||
if (await dbService.IsUrlDuplicateAsync(resolvedUrl))
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Redirected URL {ResolvedUrl} is a duplicate. Terminating processing.", "NewsChannel", resolvedUrl);
|
||||
await dbService.UpdateArticleStatusAsync(article.Id, "Duplicate");
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Redirected URL {ResolvedUrl} is a duplicate. Terminating processing.", resolvedUrl);
|
||||
await dbService.UpdateArticleStatusAsync(article.Id, "Failed");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -227,108 +222,85 @@ public class NewsScraperBackgroundService : BackgroundService
|
||||
article.SourceUrl = resolvedUrl;
|
||||
}
|
||||
|
||||
// 4. Pre-filtering Assets (Optimized with Pre-Compiled Regex Patterns)
|
||||
var title = article.Title ?? string.Empty;
|
||||
var summary = article.Summary ?? string.Empty;
|
||||
|
||||
var preFilteredAssets = assetMatchers.Where(matcher =>
|
||||
if (string.IsNullOrWhiteSpace(rawContent) || rawContent.Length < 60)
|
||||
{
|
||||
var asset = matcher.Asset;
|
||||
|
||||
// ISIN direct match
|
||||
if (rawText.Contains(asset.Isin, StringComparison.OrdinalIgnoreCase) ||
|
||||
title.Contains(asset.Isin, StringComparison.OrdinalIgnoreCase) ||
|
||||
summary.Contains(asset.Isin, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fast regex word boundary check on Full Name
|
||||
if (matcher.WordRegex != null && (matcher.WordRegex.IsMatch(rawText) || matcher.WordRegex.IsMatch(title)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fast regex word boundary check on Core Name
|
||||
if (matcher.CoreName.Length >= 3 && matcher.CoreWordRegex != null &&
|
||||
(matcher.CoreWordRegex.IsMatch(rawText) || matcher.CoreWordRegex.IsMatch(title)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
})
|
||||
.Select(m => new FilteredAssetPayload(m.Asset.Name, m.Asset.Isin))
|
||||
.ToList();
|
||||
|
||||
if (preFilteredAssets.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Pre-filtering: Article {Id} does not reference any known assets. Terminating pipeline.", "NewsChannel", article.Id);
|
||||
await dbService.UpdateArticleStatusAsync(article.Id, "Failed");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("[{Channel}] Pre-filtering matched {Count} assets for article {Id}.", "NewsChannel", preFilteredAssets.Count, article.Id);
|
||||
var discoveredIsins = article.MatchedAssets.Select(m => m.Isin).Where(i => !string.IsNullOrEmpty(i)).ToList();
|
||||
var preFilteredAssets = PreFilterAssets(rawContent, article.Title, assetMatchers, discoveredIsins);
|
||||
|
||||
// 5. Send to n8n Webhook Pipeline
|
||||
var n8nResponse = await n8nService.AnalyzeArticleAsync(rawText, preFilteredAssets, stoppingToken);
|
||||
if (n8nResponse == null)
|
||||
if (preFilteredAssets.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException("n8n AI webhook execution returned null or failed.");
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Pre-filtering: Article {Id} does not reference any known assets. Terminating pipeline.", article.Id);
|
||||
await dbService.UpdateArticleStatusAsync(article.Id, "Failed");
|
||||
return;
|
||||
}
|
||||
|
||||
// 6. Map and Save Completed Classification
|
||||
var matchedEntities = new List<MatchedAssetEntity>();
|
||||
foreach (var n8nAsset in n8nResponse.MatchedAssets)
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Pre-filtering matched {Count} assets for article {Id}.", preFilteredAssets.Count, article.Id);
|
||||
|
||||
var n8nResponse = await n8nService.AnalyzeArticleAsync(rawContent, preFilteredAssets, stoppingToken);
|
||||
if (n8nResponse == null)
|
||||
{
|
||||
var n8nCoreName = ExtractCoreAssetName(n8nAsset.Name);
|
||||
await dbService.UpdateArticleStatusAsync(article.Id, "Failed");
|
||||
return;
|
||||
}
|
||||
|
||||
var matchedIsin = preFilteredAssets.FirstOrDefault(fa =>
|
||||
fa.Name.Equals(n8nAsset.Name, StringComparison.OrdinalIgnoreCase) ||
|
||||
n8nAsset.Name.Contains(fa.Name, StringComparison.OrdinalIgnoreCase) ||
|
||||
(n8nCoreName.Length >= 3 && ExtractCoreAssetName(fa.Name).Equals(n8nCoreName, StringComparison.OrdinalIgnoreCase)))?.Isin;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(matchedIsin))
|
||||
var matchedEntities = new List<MatchedAssetEntity>();
|
||||
if (n8nResponse.MatchedAssets != null && n8nResponse.MatchedAssets.Count > 0)
|
||||
{
|
||||
foreach (var asset in n8nResponse.MatchedAssets)
|
||||
{
|
||||
matchedIsin = assetMatchers.FirstOrDefault(m =>
|
||||
m.Asset.Name.Equals(n8nAsset.Name, StringComparison.OrdinalIgnoreCase) ||
|
||||
n8nAsset.Name.Contains(m.Asset.Name, StringComparison.OrdinalIgnoreCase) ||
|
||||
(n8nCoreName.Length >= 3 && m.CoreName.Equals(n8nCoreName, StringComparison.OrdinalIgnoreCase)))?.Asset.Isin;
|
||||
}
|
||||
var preMatch = preFilteredAssets.FirstOrDefault(p => p.Name.Equals(asset.Name, StringComparison.OrdinalIgnoreCase));
|
||||
var isin = preMatch?.Isin ?? asset.Ticker ?? "";
|
||||
if (string.IsNullOrWhiteSpace(isin)) continue;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(matchedIsin))
|
||||
{
|
||||
matchedEntities.Add(new MatchedAssetEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
NewsArticleId = article.Id,
|
||||
Name = n8nAsset.Name,
|
||||
Isin = matchedIsin
|
||||
Isin = isin.Trim().ToUpperInvariant(),
|
||||
Name = !string.IsNullOrWhiteSpace(asset.Name) ? asset.Name.Trim() : isin.Trim().ToUpperInvariant()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var completedArticle = await dbService.SaveArticleClassificationAsync(article.Id, n8nResponse, matchedEntities);
|
||||
|
||||
if (completedArticle != null)
|
||||
if (matchedEntities.Count == 0)
|
||||
{
|
||||
foreach (var preMatch in preFilteredAssets)
|
||||
{
|
||||
matchedEntities.Add(new MatchedAssetEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
NewsArticleId = article.Id,
|
||||
Isin = preMatch.Isin,
|
||||
Name = preMatch.Name
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var updatedArticle = await dbService.SaveArticleClassificationAsync(article.Id, n8nResponse, matchedEntities);
|
||||
|
||||
if (updatedArticle != null)
|
||||
{
|
||||
// 7. MQTT Broadcast (Sends completed article to downstream services)
|
||||
var dto = new NewsArticleDto
|
||||
{
|
||||
Id = completedArticle.Id,
|
||||
Title = completedArticle.Title,
|
||||
Author = completedArticle.Author,
|
||||
Summary = completedArticle.Summary,
|
||||
ContentRaw = completedArticle.ContentRaw,
|
||||
Language = completedArticle.Language,
|
||||
SourceUrl = completedArticle.SourceUrl,
|
||||
ScrapedAt = completedArticle.ScrapedAt,
|
||||
PublishedAt = completedArticle.PublishedAt,
|
||||
MatchedAssets = completedArticle.MatchedAssets.Select(m => new MatchedAssetDto
|
||||
Id = updatedArticle.Id,
|
||||
Title = updatedArticle.Title,
|
||||
Author = updatedArticle.Author,
|
||||
Summary = updatedArticle.Summary,
|
||||
ContentRaw = updatedArticle.ContentRaw,
|
||||
Language = updatedArticle.Language,
|
||||
SourceUrl = updatedArticle.SourceUrl,
|
||||
ScrapedAt = updatedArticle.ScrapedAt,
|
||||
PublishedAt = updatedArticle.PublishedAt,
|
||||
Status = updatedArticle.Status,
|
||||
MatchedAssets = updatedArticle.MatchedAssets.Select(m => new MatchedAssetDto
|
||||
{
|
||||
Name = m.Name,
|
||||
Isin = m.Isin
|
||||
}).ToList(),
|
||||
Status = completedArticle.Status
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
await _mqttClient.BroadcastArticleAsync(dto);
|
||||
@@ -336,103 +308,120 @@ public class NewsScraperBackgroundService : BackgroundService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Failed to complete processing pipeline for article: {Url}. Transitioning to 'Scraping' for next cycle retry.", "NewsChannel", article.SourceUrl);
|
||||
try
|
||||
{
|
||||
await dbService.UpdateArticleStatusAsync(article.Id, "Scraping");
|
||||
}
|
||||
catch { /* Suppress database secondary errors */ }
|
||||
await _finlyticLogger.LogErrorAsync(SettingKeys.NewsChannel, ex, "[NewsScraperBackgroundService] Failed to complete processing pipeline for article: {Url}. Transitioning to 'Scraping' for next cycle retry.", article.SourceUrl);
|
||||
await dbService.UpdateArticleStatusAsync(article.Id, "Scraping");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns cached compiled asset matchers or parses the index file from disk if stale/missing.
|
||||
/// </summary>
|
||||
private List<FilteredAssetPayload> PreFilterAssets(
|
||||
string content,
|
||||
string? title,
|
||||
List<CompiledAssetMatcher> assetMatchers,
|
||||
List<string>? priorityIsins = null)
|
||||
{
|
||||
if (assetMatchers.Count == 0) return new List<FilteredAssetPayload>();
|
||||
|
||||
var fullText = (title != null ? title + " " + content : content);
|
||||
var matched = new Dictionary<string, FilteredAssetPayload>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
if (priorityIsins != null && priorityIsins.Count > 0)
|
||||
{
|
||||
foreach (var isin in priorityIsins)
|
||||
{
|
||||
var match = assetMatchers.FirstOrDefault(m => string.Equals(m.Asset.Isin, isin, StringComparison.OrdinalIgnoreCase));
|
||||
if (match != null && !matched.ContainsKey(match.Asset.Isin))
|
||||
{
|
||||
matched[match.Asset.Isin] = new FilteredAssetPayload(match.Asset.Name, match.Asset.Isin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var m in assetMatchers)
|
||||
{
|
||||
if (matched.ContainsKey(m.Asset.Isin)) continue;
|
||||
|
||||
if (fullText.Contains(m.Asset.Isin, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
matched[m.Asset.Isin] = new FilteredAssetPayload(m.Asset.Name, m.Asset.Isin);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m.WordRegex != null && m.WordRegex.IsMatch(fullText))
|
||||
{
|
||||
matched[m.Asset.Isin] = new FilteredAssetPayload(m.Asset.Name, m.Asset.Isin);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m.CoreWordRegex != null && m.CoreWordRegex.IsMatch(fullText))
|
||||
{
|
||||
matched[m.Asset.Isin] = new FilteredAssetPayload(m.Asset.Name, m.Asset.Isin);
|
||||
}
|
||||
}
|
||||
|
||||
return matched.Values.ToList();
|
||||
}
|
||||
|
||||
private async Task<List<CompiledAssetMatcher>> GetOrLoadAssetMatchersAsync()
|
||||
{
|
||||
if (_cachedAssetMatchers != null && (DateTime.UtcNow - _lastIndexLoadTime).TotalMinutes < 30)
|
||||
if (_cachedAssetMatchers != null && (DateTime.UtcNow - _lastIndexLoadTime).TotalMinutes < 60)
|
||||
{
|
||||
return _cachedAssetMatchers;
|
||||
}
|
||||
|
||||
if (!File.Exists(_indexPath))
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] Asset index file not found at: {Path}. Pre-filtering will match 0 assets.", "NewsChannel", _indexPath);
|
||||
return [];
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[NewsScraperBackgroundService] Asset index file not found at: {Path}. Pre-filtering will match 0 assets.", _indexPath);
|
||||
return new List<CompiledAssetMatcher>();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await using var stream = File.OpenRead(_indexPath);
|
||||
|
||||
// Standard Deserialization for AssetIndex list
|
||||
var rawList = await JsonSerializer.DeserializeAsync<List<AssetIndex>>(stream);
|
||||
using var stream = File.OpenRead(_indexPath);
|
||||
var indexList = await JsonSerializer.DeserializeAsync<List<AssetIndex>>(stream);
|
||||
|
||||
if (rawList != null)
|
||||
if (indexList == null || indexList.Count == 0)
|
||||
{
|
||||
_cachedAssetMatchers = rawList.Select(asset =>
|
||||
{
|
||||
var coreName = ExtractCoreAssetName(asset.Name);
|
||||
return new CompiledAssetMatcher(
|
||||
Asset: asset,
|
||||
CoreName: coreName,
|
||||
WordRegex: BuildWordRegex(asset.Name),
|
||||
CoreWordRegex: BuildWordRegex(coreName)
|
||||
);
|
||||
}).ToList();
|
||||
|
||||
_lastIndexLoadTime = DateTime.UtcNow;
|
||||
return _cachedAssetMatchers;
|
||||
return new List<CompiledAssetMatcher>();
|
||||
}
|
||||
|
||||
var compiled = new List<CompiledAssetMatcher>(indexList.Count);
|
||||
foreach (var asset in indexList)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(asset.Name) || string.IsNullOrWhiteSpace(asset.Isin))
|
||||
continue;
|
||||
|
||||
var rawName = asset.Name.Trim();
|
||||
var coreName = ExtractCoreName(rawName);
|
||||
|
||||
Regex? wordRegex = null;
|
||||
if (rawName.Length >= 4)
|
||||
{
|
||||
wordRegex = new Regex($@"\b{Regex.Escape(rawName)}\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
|
||||
}
|
||||
|
||||
Regex? coreWordRegex = null;
|
||||
if (!string.IsNullOrWhiteSpace(coreName) && coreName.Length >= 4 && !coreName.Equals(rawName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
coreWordRegex = new Regex($@"\b{Regex.Escape(coreName)}\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
|
||||
}
|
||||
|
||||
compiled.Add(new CompiledAssetMatcher(asset, coreName, wordRegex, coreWordRegex));
|
||||
}
|
||||
|
||||
_cachedAssetMatchers = compiled;
|
||||
_lastIndexLoadTime = DateTime.UtcNow;
|
||||
return _cachedAssetMatchers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Failed to read or parse asset index file from {Path}.", "NewsChannel", _indexPath);
|
||||
}
|
||||
|
||||
return _cachedAssetMatchers ?? [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper to pre-compile Word Boundary Regex for an asset name.
|
||||
/// </summary>
|
||||
private static Regex? BuildWordRegex(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name)) return null;
|
||||
try
|
||||
{
|
||||
return new Regex($@"\b{Regex.Escape(name)}\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
await _finlyticLogger.LogErrorAsync(SettingKeys.NewsChannel, ex, "[NewsScraperBackgroundService] Failed to read or parse asset index file from {Path}.", _indexPath);
|
||||
return _cachedAssetMatchers ?? new List<CompiledAssetMatcher>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the core name of an asset by removing parenthetical metadata and corporate suffixes.
|
||||
/// </summary>
|
||||
private static string ExtractCoreAssetName(string name)
|
||||
private static string ExtractCoreName(string rawName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name)) return string.Empty;
|
||||
|
||||
int parenIndex = name.IndexOf('(');
|
||||
if (parenIndex >= 0)
|
||||
{
|
||||
name = name[..parenIndex];
|
||||
}
|
||||
|
||||
name = name.Trim();
|
||||
|
||||
var suffixes = new[] { "Inc.", "Inc", "AG", "SE", "Co.", "Co", "Corp.", "Corp", "Ltd.", "Ltd", "plc", "GmbH", "SA", "NV", "Group" };
|
||||
foreach (var suffix in suffixes)
|
||||
{
|
||||
if (name.EndsWith(" " + suffix, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
name = name[..^suffix.Length].Trim();
|
||||
}
|
||||
}
|
||||
|
||||
return name;
|
||||
var cleaned = Regex.Replace(rawName, @"\b(AG|SE|SA|NV|PLC|INC|CORP|LLC|GMBH|CO|KG|HOLDING|GROUP|CLASS\s+[A-Z])\b", "", RegexOptions.IgnoreCase);
|
||||
return cleaned.Trim(' ', '.', ',', '-');
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,9 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FinlyticCore.Services;
|
||||
using FinlyticNews.Adapters.Scraping;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using FinlyticNews.Util;
|
||||
using Microsoft.Playwright;
|
||||
|
||||
namespace FinlyticNews.Services;
|
||||
@@ -25,7 +26,7 @@ public interface IPlaywrightScraperService
|
||||
/// <inheritdoc />
|
||||
public class PlaywrightScraperService : IPlaywrightScraperService, IAsyncDisposable
|
||||
{
|
||||
private readonly ILogger<PlaywrightScraperService> _logger;
|
||||
private readonly IFinlyticLogger<PlaywrightScraperService> _finlyticLogger;
|
||||
private readonly IEnumerable<ArticleScraperAdapter> _scraperAdapters;
|
||||
|
||||
private IPlaywright? _playwright;
|
||||
@@ -36,21 +37,20 @@ public class PlaywrightScraperService : IPlaywrightScraperService, IAsyncDisposa
|
||||
/// Initializes a new instance of the <see cref="PlaywrightScraperService"/> class.
|
||||
/// </summary>
|
||||
public PlaywrightScraperService(
|
||||
ILogger<PlaywrightScraperService> logger,
|
||||
IFinlyticLogger<PlaywrightScraperService> finlyticLogger,
|
||||
IEnumerable<ArticleScraperAdapter> scraperAdapters)
|
||||
{
|
||||
_logger = logger;
|
||||
_finlyticLogger = finlyticLogger;
|
||||
_scraperAdapters = scraperAdapters;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<(string ResolvedUrl, string Content)> ScrapeArticleAsync(string url)
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Launching browser context to scrape article: {Url}", "NewsChannel", url);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[PlaywrightScraperService] Launching browser context to scrape article: {Url}", url);
|
||||
|
||||
var browser = await GetOrInitBrowserAsync();
|
||||
|
||||
// Fast, isolated browser context (incognito tab environment) per article
|
||||
await using var context = await browser.NewContextAsync(new BrowserNewContextOptions
|
||||
{
|
||||
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
@@ -61,7 +61,6 @@ public class PlaywrightScraperService : IPlaywrightScraperService, IAsyncDisposa
|
||||
|
||||
try
|
||||
{
|
||||
// 1. Initial Navigation with DOMContentLoaded wait
|
||||
var response = await page.GotoAsync(url, new PageGotoOptions
|
||||
{
|
||||
WaitUntil = WaitUntilState.DOMContentLoaded,
|
||||
@@ -74,115 +73,86 @@ public class PlaywrightScraperService : IPlaywrightScraperService, IAsyncDisposa
|
||||
}
|
||||
|
||||
var finalUrl = page.Url;
|
||||
_logger.LogDebug("[{Channel}] Navigation completed. Initial final URL: {Url}", "NewsChannel", finalUrl);
|
||||
await _finlyticLogger.LogDebugAsync(SettingKeys.NewsChannel, "[PlaywrightScraperService] Navigation completed. Initial final URL: {Url}", finalUrl);
|
||||
|
||||
// 2. Resolve Host Specific Scraper Adapter
|
||||
var uri = new Uri(url);
|
||||
var host = uri.Host;
|
||||
var adapter = _scraperAdapters.FirstOrDefault(a => host.Contains(a.Hostname, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
IPage targetPage = page;
|
||||
var host = new Uri(finalUrl).Host;
|
||||
var adapter = _scraperAdapters.FirstOrDefault(a =>
|
||||
host.EndsWith(a.Hostname, StringComparison.OrdinalIgnoreCase) ||
|
||||
a.Hostname.EndsWith(host, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (adapter != null)
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Executing adapter redirect check for host: {Host}", "NewsChannel", adapter.Hostname);
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[PlaywrightScraperService] Executing adapter redirect check for host: {Host}", adapter.Hostname);
|
||||
try
|
||||
{
|
||||
var resolvedRedirectUrl = await adapter.TryResolveRedirectUrlAsync(page);
|
||||
|
||||
// Loop Protection: Navigate only if redirect target is a new URL
|
||||
if (!string.IsNullOrWhiteSpace(resolvedRedirectUrl) &&
|
||||
!string.Equals(page.Url, resolvedRedirectUrl, StringComparison.OrdinalIgnoreCase))
|
||||
!resolvedRedirectUrl.Equals(finalUrl, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogInformation("[{Channel}] Redirect resolved to target URL: {Url}", "NewsChannel", resolvedRedirectUrl);
|
||||
|
||||
var refererUrl = page.Url;
|
||||
finalUrl = resolvedRedirectUrl;
|
||||
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[PlaywrightScraperService] Redirect resolved to target URL: {Url}", resolvedRedirectUrl);
|
||||
|
||||
var redirectResponse = await page.GotoAsync(resolvedRedirectUrl, new PageGotoOptions
|
||||
{
|
||||
WaitUntil = WaitUntilState.DOMContentLoaded,
|
||||
Timeout = 30000,
|
||||
Referer = refererUrl
|
||||
Timeout = 30000
|
||||
});
|
||||
|
||||
if (redirectResponse == null)
|
||||
{
|
||||
_logger.LogWarning("[{Channel}] Failed to load response for redirect URL: {Url}", "NewsChannel", resolvedRedirectUrl);
|
||||
}
|
||||
else
|
||||
{
|
||||
finalUrl = page.Url;
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, "[PlaywrightScraperService] Failed to load response for redirect URL: {Url}", resolvedRedirectUrl);
|
||||
}
|
||||
|
||||
// Check if redirect opened a new tab/popup
|
||||
var matchedPage = page.Context.Pages.FirstOrDefault(p => p.Url == resolvedRedirectUrl);
|
||||
if (matchedPage != null)
|
||||
{
|
||||
targetPage = matchedPage;
|
||||
}
|
||||
finalUrl = page.Url;
|
||||
host = new Uri(finalUrl).Host;
|
||||
|
||||
adapter = _scraperAdapters.FirstOrDefault(a =>
|
||||
host.EndsWith(a.Hostname, StringComparison.OrdinalIgnoreCase) ||
|
||||
a.Hostname.EndsWith(host, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "[{Channel}] Failed to resolve redirect through adapter for host: {Host}. Continuing with current page.", "NewsChannel", adapter.Hostname);
|
||||
await _finlyticLogger.LogWarningAsync(SettingKeys.NewsChannel, ex, "[PlaywrightScraperService] Failed to resolve redirect through adapter for host: {Host}. Continuing with current page.", adapter.Hostname);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Re-resolve detail page adapter for final target page
|
||||
var targetUri = new Uri(targetPage.Url);
|
||||
var targetHost = targetUri.Host;
|
||||
var targetAdapter = _scraperAdapters.FirstOrDefault(a => targetHost.Contains(a.Hostname, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
// Wait a brief moment for dynamic scripts / DOM settling
|
||||
await targetPage.WaitForTimeoutAsync(1000);
|
||||
|
||||
// 4. Extract Content via Mozilla Readability (or Adapter Fallback)
|
||||
string extractedText = string.Empty;
|
||||
|
||||
if (targetAdapter != null)
|
||||
string content;
|
||||
if (adapter != null)
|
||||
{
|
||||
var readabilityResult = await targetAdapter.ExtractArticleContentAsync(targetPage);
|
||||
if (readabilityResult != null && !string.IsNullOrWhiteSpace(readabilityResult.TextContent))
|
||||
{
|
||||
extractedText = readabilityResult.TextContent;
|
||||
}
|
||||
var result = await adapter.ExtractArticleContentAsync(page);
|
||||
content = result?.TextContent ?? await FallbackExtractContentAsync(page);
|
||||
}
|
||||
|
||||
// Standard Fallback: Body Text / Selector Extraction
|
||||
if (string.IsNullOrWhiteSpace(extractedText))
|
||||
else
|
||||
{
|
||||
var bodySelector = targetAdapter?.ArticleBodySelector ?? "body";
|
||||
var locator = targetPage.Locator(bodySelector);
|
||||
|
||||
if (await locator.CountAsync() > 0)
|
||||
{
|
||||
extractedText = await locator.First.InnerTextAsync();
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(extractedText))
|
||||
{
|
||||
extractedText = await targetPage.EvaluateAsync<string>(
|
||||
$"() => document.querySelector('{bodySelector}')?.innerText ?? ''");
|
||||
}
|
||||
content = await FallbackExtractContentAsync(page);
|
||||
}
|
||||
|
||||
return (finalUrl, extractedText?.Trim() ?? string.Empty);
|
||||
return (finalUrl, content);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[{Channel}] Failed to scrape page content from URL: {Url}", "NewsChannel", url);
|
||||
await _finlyticLogger.LogErrorAsync(SettingKeys.NewsChannel, ex, "[PlaywrightScraperService] Failed to scrape page content from URL: {Url}", url);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await context.CloseAsync();
|
||||
await page.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Thread-safe singleton initialization of the Chromium browser instance.
|
||||
/// </summary>
|
||||
private async Task<string> FallbackExtractContentAsync(IPage page)
|
||||
{
|
||||
var innerText = await page.EvaluateAsync<string>(@"() => {
|
||||
const scripts = document.querySelectorAll('script, style, noscript, nav, header, footer, iframe, svg');
|
||||
scripts.forEach(s => s.remove());
|
||||
|
||||
const main = document.querySelector('article, main, .article-content, #content, .story-body') || document.body;
|
||||
return main ? main.innerText : document.body.innerText;
|
||||
}");
|
||||
|
||||
return innerText?.Trim() ?? string.Empty;
|
||||
}
|
||||
|
||||
private async Task<IBrowser> GetOrInitBrowserAsync()
|
||||
{
|
||||
if (_browser != null && _browser.IsConnected)
|
||||
@@ -202,10 +172,16 @@ public class PlaywrightScraperService : IPlaywrightScraperService, IAsyncDisposa
|
||||
_browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
||||
{
|
||||
Headless = true,
|
||||
Args = ["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage"]
|
||||
Args = new[]
|
||||
{
|
||||
"--no-sandbox",
|
||||
"--disable-setuid-sandbox",
|
||||
"--disable-dev-shm-usage",
|
||||
"--disable-gpu"
|
||||
}
|
||||
});
|
||||
|
||||
_logger.LogInformation("[{Channel}] Initialized shared Chromium browser instance.", "NewsChannel");
|
||||
await _finlyticLogger.LogInfoAsync(SettingKeys.NewsChannel, "[PlaywrightScraperService] Initialized shared Chromium browser instance.");
|
||||
return _browser;
|
||||
}
|
||||
finally
|
||||
@@ -214,9 +190,6 @@ public class PlaywrightScraperService : IPlaywrightScraperService, IAsyncDisposa
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the Playwright and Browser instances cleanly during service shutdown.
|
||||
/// </summary>
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_browser != null)
|
||||
|
||||
Reference in New Issue
Block a user