Skip to content

Commit

Permalink
Add news command
Browse files Browse the repository at this point in the history
  • Loading branch information
totollygeek committed Apr 7, 2024
1 parent 01cacdb commit 8b36edb
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/awesome.app/Dump/DumpPersonExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class DumpPersonExecutor(
bool? showPrivateMembers = null,
bool? showSalary = null) : OperationExecutor
{
public override string OperationName => "Dump Objects";
public override string OperationName => "Dump a Person";
public override int Execute()
{
var dumpPerson = person ?? GeneratePerson();
Expand Down
2 changes: 1 addition & 1 deletion src/awesome.app/Figlet/FigletExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace TOTOllyGeek.Awesome.Figlet;

internal class FigletExecutor(string? text = null, FiggleFont? font = null) : OperationExecutor
{
public override string OperationName => "Figlet";
public override string OperationName => "Generate a Figlet";

public override int Execute()
{
Expand Down
2 changes: 2 additions & 0 deletions src/awesome.app/Menu/MenuCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Spectre.Console.Cli;
using TOTOllyGeek.Awesome.Dump;
using TOTOllyGeek.Awesome.Figlet;
using TOTOllyGeek.Awesome.News;
using TOTOllyGeek.Awesome.Repl;

namespace TOTOllyGeek.Awesome.Menu;
Expand All @@ -22,6 +23,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
.PageSize(10)
.AddChoices(
new FigletExecutor(),
new NewsExecutor(),
new DumpPersonExecutor(),
new ReplExecutor(),
new ExitExecutor()));
Expand Down
22 changes: 22 additions & 0 deletions src/awesome.app/News/NewsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Spectre.Console.Cli;
using TOTOllyGeek.Awesome.Lib;

namespace TOTOllyGeek.Awesome.News;

public class NewsCommand : Command<NewsCommand.Settings>
{
public class Settings : CommandSettings
{
[Description("Tag (query) for which to search")]
[CommandArgument(1, "[SearchTag]")]
[DefaultValue("NASA")]
public string SearchTag { get; init; }
}

public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
{
return new NewsExecutor(settings.SearchTag).Execute();
}
}
88 changes: 88 additions & 0 deletions src/awesome.app/News/NewsExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#nullable enable
using System;
using System.Linq;
using NewsAPI;
using NewsAPI.Constants;
using NewsAPI.Models;
using Spectre.Console;

namespace TOTOllyGeek.Awesome.News;

internal class NewsExecutor(string? searchTag = null) : OperationExecutor
{
private const string NewsApiKeyVariableName = "NEWS_API_KEY";
public override string OperationName => "What are the latest news?";

public override int Execute()
{
var key = GetApiKeyOrThrow();
var searchQuery = searchTag ?? "NASA";

var newsApiClient = new NewsApiClient(key);
var articlesResponse = newsApiClient.GetEverything(new EverythingRequest
{
Q = searchQuery,
SortBy = SortBys.Popularity,
Language = Languages.EN,
From = DateTime.Now.Subtract(TimeSpan.FromDays(7)),
Page = 0,
PageSize = 10
});

if (articlesResponse.Status != Statuses.Ok) return 1;

var articles =
articlesResponse.Articles
.Where(a => !a.Title.Equals("[Removed]", StringComparison.OrdinalIgnoreCase))
.ToArray();

AnsiConsole.Write(BuildTable(articles));

return 0;
}

private static Table BuildTable(Article[] articles)
{
var table =
new Table()
.Border(TableBorder.Double)
.Centered()
.Width(200);

table.AddColumn(
new TableColumn("Article")
.Centered()
.Width(50));
table.AddColumn(new TableColumn("Description")
.Width(100));

foreach (var article in articles)
{
var titlePanel = new Panel(article.Title.EscapeMarkup()).Border(BoxBorder.Ascii);
var infoColumns =
new Columns(
new Markup($"[lime underline]{article.Author.EscapeMarkup()}[/]"),
new Markup($"[lime underline]{article.PublishedAt:f}[/]"));

var infoRows = new Rows(titlePanel, infoColumns);
var descriptionPanel = new Panel(article.Description.EscapeMarkup()).Border(BoxBorder.Rounded);

table.AddRow(infoRows, descriptionPanel);
}

return table;
}

private static string GetApiKeyOrThrow()
{
var key = Environment.GetEnvironmentVariable(NewsApiKeyVariableName);

if (key == null)
{
throw new InvalidOperationException(
$"NewsAPI key was not provided and also is not set into \"{NewsApiKeyVariableName}\" environment variable.");
}

return key;
}
}
2 changes: 2 additions & 0 deletions src/awesome.app/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using TOTOllyGeek.Awesome.Dump;
using TOTOllyGeek.Awesome.Figlet;
using TOTOllyGeek.Awesome.Menu;
using TOTOllyGeek.Awesome.News;
using TOTOllyGeek.Awesome.Repl;

var app = new CommandApp();
Expand All @@ -10,6 +11,7 @@
{
config.AddCommand<MenuCommand>("menu");
config.AddCommand<FigletCommand>("figlet");
config.AddCommand<NewsCommand>("news");
config.AddCommand<DumpPersonCommand>("dump");
config.AddCommand<ReplCommand>("repl");
});
Expand Down
2 changes: 1 addition & 1 deletion src/awesome.app/Repl/ReplExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal class ReplExecutor() : OperationExecutor
{
private const string CSharpReplDotnetTool = "csharprepl";

public override string OperationName => "C# REPL";
public override string OperationName => "C# Run-Eval-Print Loop";

public override int Execute()
{
Expand Down
1 change: 1 addition & 0 deletions src/awesome.app/awesome.app.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageReference Include="NewsAPI" Version="0.7.0" />
<PackageReference Include="Spectre.Console" Version="0.48.1-preview.0.38" />
<PackageReference Include="Spectre.Console.Cli" Version="0.48.1-preview.0.38" />
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.48.1-preview.0.38" />
Expand Down

0 comments on commit 8b36edb

Please sign in to comment.