Skip to content

Commit

Permalink
Create a spreadsheet with the bills
Browse files Browse the repository at this point in the history
  • Loading branch information
rprouse committed Oct 27, 2024
1 parent ae48200 commit a2d4ec2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions Guppi.Core/Guppi.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ClosedXML" Version="0.104.1" />
<PackageReference Include="Google.Apis.Tasks.v1" Version="1.68.0.3468" />
<PackageReference Include="Google.Apis.Calendar.v3" Version="1.68.0.3557" />
<PackageReference Include="Ical.Net" Version="4.3.1" />
Expand Down
38 changes: 31 additions & 7 deletions Guppi.Core/Services/BillService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Threading.Tasks;
using ClosedXML.Excel;
using Guppi.Core.Configurations;
using Guppi.Core.Exceptions;
using Guppi.Core.Interfaces.Services;
Expand All @@ -12,6 +13,11 @@ internal class BillService : IBillService
{
private readonly BillConfiguration _configuration = Configuration.Load<BillConfiguration>("billing");

readonly static string _downloadsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", "bills");

int _row = 2;
IXLWorksheet _worksheet;

private bool Configured => _configuration.Configured;

public async Task DownloadAlectraBills()
Expand All @@ -21,6 +27,19 @@ public async Task DownloadAlectraBills()
throw new UnconfiguredException("Please configure the Billing provider");
}

// Create an Excel spreadsheet to store the billing data
string path = Path.Combine(_downloadsPath, "Bills.xlsx");
if (File.Exists(path))
{
File.Delete(path);
}
var workbook = new ClosedXML.Excel.XLWorkbook();
_worksheet = workbook.Worksheets.Add("Bills");

_worksheet.Cell(1, 1).Value = "Account";
_worksheet.Cell(1, 2).Value = "Date";
_worksheet.Cell(1, 3).Value = "Amount";

using var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
var context = await browser.NewContextAsync();
Expand All @@ -40,9 +59,11 @@ public async Task DownloadAlectraBills()
await DownloadBillsForAccount(page, "7076520332");

await Task.Delay(5000);

workbook.SaveAs(path);
}

private static async Task DownloadBillsForAccount(IPage page, string account)
private async Task DownloadBillsForAccount(IPage page, string account)
{
// Navigate to Billing History
await page.GotoAsync("https://myalectra.alectrautilities.com/portal/#/billinghistory");
Expand Down Expand Up @@ -79,13 +100,18 @@ private static async Task DownloadBillsForAccount(IPage page, string account)
amount = a.ToString("N2");
}

_worksheet.Cell(_row, 1).Value = account;
_worksheet.Cell(_row, 2).Value = date;
_worksheet.Cell(_row, 3).Value = amount;
_row++;

Console.WriteLine($"{account} {date} {amount}");

await DownloadBill(page, account, $"{date} {amount}");
}
}

private static async Task DownloadBill(IPage page, string account, string bill)
private async Task DownloadBill(IPage page, string account, string bill)
{
// Open the billing page in a new tab
var billingPage = await page.RunAndWaitForPopupAsync(async () =>
Expand All @@ -96,12 +122,10 @@ private static async Task DownloadBill(IPage page, string account, string bill)
// Listen for download events so we can specify the
billingPage.Download += async (_, download) =>
{
string downloadsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", "bills");

// Ensure the directory exists
Directory.CreateDirectory(downloadsPath);
Directory.CreateDirectory(_downloadsPath);

var filePath = Path.Combine(downloadsPath, $"{account} {bill}.pdf");
var filePath = Path.Combine(_downloadsPath, $"{account} {bill}.pdf");
await download.SaveAsAsync(filePath);

await billingPage.CloseAsync();
Expand All @@ -117,7 +141,7 @@ async void billingPage_Dialog_EventHandler(object sender, IDialog dialog)
billingPage.Dialog += billingPage_Dialog_EventHandler;

// Click the download button
var download = await billingPage.RunAndWaitForDownloadAsync(async () =>
await billingPage.RunAndWaitForDownloadAsync(async () =>
{
await billingPage.GetByRole(AriaRole.Img, new() { Name = "Download PDF" }).ClickAsync();
});
Expand Down

0 comments on commit a2d4ec2

Please sign in to comment.