Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change crawler formatting #111

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tools/DanaCrawler/DanaCrawler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
google-api.json
appsettings.Development.json
130 changes: 55 additions & 75 deletions tools/DanaCrawler/DanaCrawler/GoogleSheetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,40 @@ public GoogleSheetsService(string credentialsJsonPath, string spreadsheetId)
}

public async Task InsertHelpRequests(List<HelpRequest> requests)
{
await InsertNeedsRequestsAsync(requests);
await InsertOffersRequestsAsync(requests);
}

private async Task InsertOffersRequestsAsync(List<HelpRequest> requests)
{
var headerRow = new List<object>
{
"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<IList<object>> { headerRow };
rows.AddRange(requests.OrderBy(x => x.Id).Select(request => new List<object>
rows.AddRange(requests.OrderBy(x => x.Id).Where(x => x.Type == "ofrece").Select(request => new List<object>
{
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
Expand All @@ -62,79 +68,53 @@ public async Task InsertHelpRequests(List<HelpRequest> 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<HelpRequest> requests)
{
var requests = new List<Request>
var headerRow = new List<object>
{
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<IList<object>> { headerRow };
rows.AddRange(requests.OrderBy(x => x.Id).Where(x => x.Type == "necesita").Select(request => new List<object>
{
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();
}
}
2 changes: 1 addition & 1 deletion tools/DanaCrawler/DanaCrawler/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private async Task ExportToGoogleSheets(List<HelpRequest> helpRequests)
try
{
await sheetsService.InsertHelpRequests(helpRequests);
await sheetsService.FormatSheet();
//await sheetsService.FormatSheet();
_logger.LogInformation("Data exported successfully!");
}
catch (Exception ex)
Expand Down