From dfe8bc8dfd3e7baee917ab08b1496a06814878b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Sanchidri=C3=A1n?= Date: Wed, 6 Nov 2024 22:33:12 +0100 Subject: [PATCH] Change formatting --- tools/DanaCrawler/DanaCrawler/.gitignore | 2 + .../DanaCrawler/GoogleSheetService.cs | 130 ++++++++---------- tools/DanaCrawler/DanaCrawler/Worker.cs | 2 +- 3 files changed, 58 insertions(+), 76 deletions(-) create mode 100644 tools/DanaCrawler/DanaCrawler/.gitignore diff --git a/tools/DanaCrawler/DanaCrawler/.gitignore b/tools/DanaCrawler/DanaCrawler/.gitignore new file mode 100644 index 00000000..ad3a53ff --- /dev/null +++ b/tools/DanaCrawler/DanaCrawler/.gitignore @@ -0,0 +1,2 @@ +google-api.json +appsettings.Development.json \ No newline at end of file diff --git a/tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs b/tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs index da6b6256..92f4e2c5 100644 --- a/tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs +++ b/tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs @@ -26,34 +26,40 @@ public GoogleSheetsService(string credentialsJsonPath, string spreadsheetId) } public async Task InsertHelpRequests(List requests) + { + await InsertNeedsRequestsAsync(requests); + await InsertOffersRequestsAsync(requests); + } + + private async Task InsertOffersRequestsAsync(List requests) { var headerRow = new List { - "ID", "Type", "Name", "Location", "Description", - "Urgency", "Number of People", "Status", "Latitude", "Longitude", - "ContactInfo", "Help Types", "People Needed", "Town", "Created At" + "Created At", "ID", "Status", "Town","Description", "Help Types", "Number of People", + "Name", "Location", "ContactInfo", "People Needed", }; var rows = new List> { headerRow }; - rows.AddRange(requests.OrderBy(x => x.Id).Select(request => new List + rows.AddRange(requests.OrderBy(x => x.Id).Where(x => x.Type == "ofrece").Select(request => new List { + request.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss"), request.Id, - request.Type ?? "", - request.Name ?? "", - request.Location ?? "", + //request.Type ?? "", + request.Status ?? "", + request.Town?.Name ?? "", request.Description ?? "", - request.Urgency ?? "", + string.Join(", ", request.HelpType ?? []), request.NumberOfPeople ?? 0, - request.Status ?? "", - request.Latitude ?? "", - request.Longitud ?? "", + request.Name ?? "", + request.Location ?? "", request.ContactInfo ?? "", - string.Join(", ", request.HelpType ?? []), + //request.Urgency ?? "", + //request.Latitude ?? "", + //request.Longitud ?? "", request.PeopleNeeded, - request.Town?.Name ?? "", - request.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss") })); + var valueRange = new ValueRange { Values = rows @@ -62,79 +68,53 @@ public async Task InsertHelpRequests(List requests) var updateRequest = _sheetsService.Spreadsheets.Values.Update( valueRange, _spreadsheetId, - "RAW!A1:O" + "OfreceCRMV2!A1:O" ); updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED; await updateRequest.ExecuteAsync(); } - public async Task FormatSheet() + private async Task InsertNeedsRequestsAsync(List requests) { - var requests = new List + var headerRow = new List { - new Request - { - RepeatCell = new RepeatCellRequest - { - Range = new GridRange - { - SheetId = 0, - StartRowIndex = 0, - EndRowIndex = 1 - }, - Cell = new CellData - { - UserEnteredFormat = new CellFormat - { - BackgroundColor = new Color { Red = 0.8f, Green = 0.8f, Blue = 0.8f }, - TextFormat = new TextFormat - { - Bold = true - } - } - }, - Fields = "userEnteredFormat(backgroundColor,textFormat)" - } - }, - new Request - { - AutoResizeDimensions = new AutoResizeDimensionsRequest - { - Dimensions = new DimensionRange - { - SheetId = 0, - Dimension = "COLUMNS", - StartIndex = 0, - EndIndex = 15 - } - } - }, - new Request - { - UpdateSheetProperties = new UpdateSheetPropertiesRequest - { - Properties = new SheetProperties - { - SheetId = 0, - GridProperties = new GridProperties - { - FrozenRowCount = 1 - } - }, - Fields = "gridProperties.frozenRowCount" - } - } + "Created At", "ID", "Status", "Town","Description", "Help Types", "Number of People", + "Name", "Location", "ContactInfo", "People Needed", }; - var batchUpdateRequest = new BatchUpdateSpreadsheetRequest + var rows = new List> { headerRow }; + rows.AddRange(requests.OrderBy(x => x.Id).Where(x => x.Type == "necesita").Select(request => new List + { + request.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss"), + request.Id, + //request.Type ?? "", + request.Status ?? "", + request.Town?.Name ?? "", + request.Description ?? "", + string.Join(", ", request.HelpType ?? []), + request.NumberOfPeople ?? 0, + request.Name ?? "", + request.Location ?? "", + request.ContactInfo ?? "", + //request.Urgency ?? "", + //request.Latitude ?? "", + //request.Longitud ?? "", + request.PeopleNeeded, + })); + + var valueRange = new ValueRange { - Requests = requests + Values = rows }; - await _sheetsService.Spreadsheets.BatchUpdate( - batchUpdateRequest, - _spreadsheetId - ).ExecuteAsync(); + var updateRequest = _sheetsService.Spreadsheets.Values.Update( + valueRange, + _spreadsheetId, + "NecesitaCRMV2!A1:O" + ); + updateRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.USERENTERED; + + await updateRequest.ExecuteAsync(); } } \ No newline at end of file diff --git a/tools/DanaCrawler/DanaCrawler/Worker.cs b/tools/DanaCrawler/DanaCrawler/Worker.cs index 4f963caf..e9d4ac5d 100644 --- a/tools/DanaCrawler/DanaCrawler/Worker.cs +++ b/tools/DanaCrawler/DanaCrawler/Worker.cs @@ -45,7 +45,7 @@ private async Task ExportToGoogleSheets(List helpRequests) try { await sheetsService.InsertHelpRequests(helpRequests); - await sheetsService.FormatSheet(); + //await sheetsService.FormatSheet(); _logger.LogInformation("Data exported successfully!"); } catch (Exception ex)