From 3dcd7191973157009c15e660d5087573839a6336 Mon Sep 17 00:00:00 2001 From: Marcel <14852157+Marcel0024@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:23:38 +0200 Subject: [PATCH] Constants for file --- FundaScraper/App/FundaScraperBackgroundService.cs | 2 +- FundaScraper/App/WebhookDB.cs | 10 +++++----- FundaScraper/App/WebhookSink.cs | 2 +- FundaScraper/Constants.cs | 10 ++++++++++ 4 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 FundaScraper/Constants.cs diff --git a/FundaScraper/App/FundaScraperBackgroundService.cs b/FundaScraper/App/FundaScraperBackgroundService.cs index 5e1c52b..3b52d4d 100644 --- a/FundaScraper/App/FundaScraperBackgroundService.cs +++ b/FundaScraper/App/FundaScraperBackgroundService.cs @@ -38,7 +38,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) new(nameof(ListingModel.ZipCode), settings.Value.ZipCodeSelector) ]) .AddSink(webhookSink) - .AddSink(new CsvFileSink(Path.Combine("/data", "results.csv"), dataCleanupOnStart: true)) + .AddSink(new CsvFileSink(Constants.FileNames.ResultsFilePath, dataCleanupOnStart: true)) .WithParallelismDegree(settings.Value.ParallelismDegree) .PageCrawlLimit(settings.Value.PageCrawlLimit); diff --git a/FundaScraper/App/WebhookDB.cs b/FundaScraper/App/WebhookDB.cs index 7b3acff..136f3aa 100644 --- a/FundaScraper/App/WebhookDB.cs +++ b/FundaScraper/App/WebhookDB.cs @@ -4,7 +4,7 @@ namespace FundaScraper.App; internal class WebhookDB { - private readonly string DatabaseFilePath = Path.Combine("/data", "webhooks-history.json"); + private readonly string WebhooksHistoryJson = Constants.FileNames.WebhooksHistoryJson; private Dictionary Listings { get; init; } public WebhookDB() @@ -26,14 +26,14 @@ internal async Task SaveWebHook(ListingModel entry) private async Task GetHistoryFile() { - if (!File.Exists(DatabaseFilePath)) + if (!File.Exists(WebhooksHistoryJson)) { return new DbModel([]); } try { - var json = await File.ReadAllTextAsync(DatabaseFilePath); + var json = await File.ReadAllTextAsync(WebhooksHistoryJson); return JsonSerializer.Deserialize(json)!; } catch @@ -46,9 +46,9 @@ private async Task SaveHistoryFile(DbModel model) { var json = JsonSerializer.Serialize(model); - Directory.CreateDirectory(Path.GetDirectoryName(DatabaseFilePath)!); + Directory.CreateDirectory(Path.GetDirectoryName(WebhooksHistoryJson)!); - await File.WriteAllTextAsync(DatabaseFilePath, json); + await File.WriteAllTextAsync(WebhooksHistoryJson, json); } } diff --git a/FundaScraper/App/WebhookSink.cs b/FundaScraper/App/WebhookSink.cs index 9be0c65..e760a2a 100644 --- a/FundaScraper/App/WebhookSink.cs +++ b/FundaScraper/App/WebhookSink.cs @@ -29,7 +29,7 @@ public async Task EmitAsync(ParsedData entity, CancellationToken cancellationTok try { - listingModel = JsonSerializer.Deserialize(entity.Data.ToString()); + listingModel = JsonSerializer.Deserialize(entity.Data.ToString())!; } catch { return; } diff --git a/FundaScraper/Constants.cs b/FundaScraper/Constants.cs new file mode 100644 index 0000000..bb8e39a --- /dev/null +++ b/FundaScraper/Constants.cs @@ -0,0 +1,10 @@ +namespace FundaScraper; + +internal static class Constants +{ + internal static class FileNames + { + internal static string WebhooksHistoryJson = Path.Combine("/data", "webhooks-history.json"); + internal static string ResultsFilePath = Path.Combine("/data", "results.csv"); + } +}