From 190d1ac6824701706243729831d7eddc846a00d6 Mon Sep 17 00:00:00 2001 From: BSanchidrian Date: Fri, 8 Nov 2024 10:35:42 +0100 Subject: [PATCH] Add vehicle and availability --- .../DanaCrawler/GoogleSheetService.cs | 15 ++++--- tools/DanaCrawler/DanaCrawler/HelpRequest.cs | 44 ++++++++++++++++++- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs b/tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs index 92f4e2c5..e79fbb5f 100644 --- a/tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs +++ b/tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs @@ -1,4 +1,5 @@ -using Google.Apis.Auth.OAuth2; +using System.Text.Json; +using Google.Apis.Auth.OAuth2; using Google.Apis.Services; using Google.Apis.Sheets.v4; using Google.Apis.Sheets.v4.Data; @@ -35,12 +36,14 @@ private async Task InsertOffersRequestsAsync(List requests) { var headerRow = new List { - "Created At", "ID", "Status", "Town","Description", "Help Types", "Number of People", - "Name", "Location", "ContactInfo", "People Needed", + "Created At", "ID", "Status", "Town","Description", "Help Types", "Availability", "Vehicle", + "Number of People", "Name", "Location", "ContactInfo", "People Needed", }; + var offerRequests = requests.Where(x => x.Type == "ofrece"); + var rows = new List> { headerRow }; - rows.AddRange(requests.OrderBy(x => x.Id).Where(x => x.Type == "ofrece").Select(request => new List + rows.AddRange(offerRequests.OrderBy(x => x.Id).Select(request => new List { request.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss"), request.Id, @@ -49,6 +52,8 @@ private async Task InsertOffersRequestsAsync(List requests) request.Town?.Name ?? "", request.Description ?? "", string.Join(", ", request.HelpType ?? []), + string.Join(", ", request.Resources?.Availability ?? []), + request.Resources?.Vehicle ?? "NO", request.NumberOfPeople ?? 0, request.Name ?? "", request.Location ?? "", @@ -80,7 +85,7 @@ private async Task InsertNeedsRequestsAsync(List requests) var headerRow = new List { "Created At", "ID", "Status", "Town","Description", "Help Types", "Number of People", - "Name", "Location", "ContactInfo", "People Needed", + "Name", "Location", "ContactInfo", "People Needed", }; var rows = new List> { headerRow }; diff --git a/tools/DanaCrawler/DanaCrawler/HelpRequest.cs b/tools/DanaCrawler/DanaCrawler/HelpRequest.cs index 367341f8..b1ae82f8 100644 --- a/tools/DanaCrawler/DanaCrawler/HelpRequest.cs +++ b/tools/DanaCrawler/DanaCrawler/HelpRequest.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; +using Newtonsoft.Json; using Supabase.Postgrest.Attributes; using Supabase.Postgrest.Models; @@ -63,9 +64,50 @@ internal sealed class HelpRequest : BaseModel [Column("contact_info")] public string ContactInfo { get; init; } + [Column("resources")] + [Newtonsoft.Json.JsonConverter(typeof(ResourcesJsonConverter))] + public Resources? Resources { get; init; } + [Column("created_at")] public DateTime CreatedAt { get; init; } [JsonProperty("towns")] public Town Town { get; set; } +} + +internal sealed class Resources +{ + [JsonPropertyName("vehicle")] + public string? Vehicle { get; set; } + [JsonPropertyName("availability")] + public List Availability { get; set; } = new(); + [JsonPropertyName("radius")] + public string? Radius { get; set; } + [JsonPropertyName("experience")] + public string? Experience { get; set; } = string.Empty; +} + +internal sealed class ResourcesJsonConverter : Newtonsoft.Json.JsonConverter +{ + public override Resources ReadJson(JsonReader reader, Type objectType, Resources existingValue, bool hasExistingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.String) + { + // If we get a string, parse it as JSON + string jsonString = (string)reader.Value; + return JsonConvert.DeserializeObject(jsonString); + } + else if (reader.TokenType == JsonToken.StartObject) + { + // If we get a JSON object directly, deserialize it + return serializer.Deserialize(reader); + } + + return null; + } + + public override void WriteJson(JsonWriter writer, Resources value, JsonSerializer serializer) + { + serializer.Serialize(writer, value); + } } \ No newline at end of file