Skip to content

Commit

Permalink
Merge pull request #201 from rprouse/issue/200
Browse files Browse the repository at this point in the history
Download Enbridge bills
  • Loading branch information
rprouse authored Oct 28, 2024
2 parents 3e09fd0 + 0fb09ae commit d67b840
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Guppi.Console/Guppi.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://github.com/rprouse/guppi</PackageProjectUrl>
<RepositoryUrl>https://github.com/rprouse/guppi</RepositoryUrl>
<PackageId>dotnet-guppi</PackageId>
<Version>6.3.0</Version>
<Version>6.3.1</Version>
<PackAsTool>true</PackAsTool>
<ToolCommandName>guppi</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
Expand Down
2 changes: 1 addition & 1 deletion Guppi.Console/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Guppi.Console": {
"commandName": "Project",
"commandLineArgs": "bills alectra"
"commandLineArgs": "bills all"
}
}
}
40 changes: 35 additions & 5 deletions Guppi.Console/Skills/BillsSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,39 @@ internal class BillsSkill(IBillService service) : ISkill

public IEnumerable<Command> GetCommands()
{
var option = new Option<int>(new string[] { "--months", "-m" }, () => 1, "The number of months of bills to download.");

var all = new Command("all", "Download bills from all providers")
{
option
};
all.Handler = CommandHandler.Create(async (int months) => await DownloadAllBills(months));

var alectra = new Command("alectra", "Download bills from Alectra")
{
option
};
alectra.Handler = CommandHandler.Create(async (int months) => await DownloadAlectraBills(months));

var enbridge = new Command("enbridge", "Download bills from Enbridge")
{
option
};
alectra.Handler = CommandHandler.Create(async () => await DownloadAlectraBills());
enbridge.Handler = CommandHandler.Create(async (int months) => await DownloadEnbridgeBills(months));

var configure = new Command("configure", "Configures the Bill provider");
configure.AddAlias("config");
configure.Handler = CommandHandler.Create(() => Configure());

var install = new Command("install", "Installs Playwright for the Bill provider");
install.Handler = CommandHandler.Create(() => _service.InstallPlaywright());

var command = new Command("bills", "Download bills from online")
{
all,
alectra,
enbridge,
install,
configure
};
command.AddAlias("billing");
Expand All @@ -36,13 +57,22 @@ public IEnumerable<Command> GetCommands()
return new List<Command> { command };
}

private async Task DownloadAlectraBills()
{
private async Task DownloadAllBills(int months) =>
await DownloadBills(":spiral_notepad: Download Bills", months, _service.DownloadAllBills);

private async Task DownloadAlectraBills(int months) =>
await DownloadBills(":high_voltage: Alectra Bills", months, _service.DownloadAlectraBills);

private async Task DownloadEnbridgeBills(int months) =>
await DownloadBills(":chart_increasing: Enbridge Bills", months, _service.DownloadEnbridgeBills);

private static async Task DownloadBills(string title, int months, Func<int, Task> downloader)
{
try
{
AnsiConsoleHelper.TitleRule(":high_voltage: Alectra Bills");
AnsiConsoleHelper.TitleRule(title);

await _service.DownloadAlectraBills();
await downloader(months);

AnsiConsoleHelper.Rule("white");
}
Expand Down
5 changes: 4 additions & 1 deletion Guppi.Core/Interfaces/Services/IBillService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace Guppi.Core.Interfaces.Services;

public interface IBillService
{
Task DownloadAlectraBills();
Task DownloadAllBills(int months);
Task DownloadAlectraBills(int months);
Task DownloadEnbridgeBills(int months);
void InstallPlaywright();
void Configure();
}
48 changes: 48 additions & 0 deletions Guppi.Core/Providers/WorkbookProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.IO;
using ClosedXML.Excel;

namespace Guppi.Core.Providers;

public class WorkbookProvider
{
readonly string _filename;
readonly IXLWorkbook _workbook;
readonly IXLWorksheet _worksheet;
int row = 0;

/// <summary>
/// Creates a new instance of the WorksheetProvider
/// </summary>
/// <param name="filename">The filename for the workbook</param>
/// <param name="sheet">The name of the worksheet</param>
/// <param name="delete">Deletes and recreates the workbook if it exists</param>
public WorkbookProvider(string filename, string sheet, IEnumerable<string> headers, bool delete = true)
{
_filename = filename;

if (delete && File.Exists(_filename))
{
File.Delete(_filename);
}

_workbook = new XLWorkbook();
_worksheet = _workbook.Worksheets.Add("Bills");

AddRow(headers);
}

public void AddRow(IEnumerable<string> values)
{
row++;
int col = 1;
foreach (string value in values) {
_worksheet.Cell(row, col++).Value = value;
}
}

public void Save()
{
_workbook.SaveAs(_filename);
}
}
Loading

0 comments on commit d67b840

Please sign in to comment.