Skip to content

Commit

Permalink
Merge pull request #100 from times-yasunori/Yasunori.NET
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoya authored Oct 11, 2024
2 parents 99a253e + d9c4f7e commit c8750d9
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 36 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,21 @@ jobs:
test-YasunoriDotNET:
name: Build & Test Yasunori.NET
runs-on: ubuntu-latest
defaults:
run:
working-directory: packages/Yasunori.NET
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore packages/Yasunori.NET
run: dotnet restore
- name: Build
run: dotnet build --no-restore packages/Yasunori.NET
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal packages/Yasunori.NET
run: dotnet test --no-build --verbosity normal
test-clojure-yasunori:
name: Build & Test clojure-yasunori
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ This is the sole JSON Schema exclusively for Awesome Yasunori in the world.
### [Awesome Yasunori Image Generator](https://image.yasunori.dev/ogp?id=1)

This is an application that generates images from Awesome Yasunori.

### Yasunori.NET

Client software for console output using Yasunori APIs.
Yasunori list output, ID individual specification output, and OGP image output (Sixel support terminal required) are available.

It's being developed in [packages/Yasunori.NET](./packages/Yasunori.NET).
13 changes: 11 additions & 2 deletions packages/Yasunori.NET/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@

```
Yasunori
Yasunori -list
Yasunori {-l | --list}
Yasunori {-i | --image}
Yasunori id [id...]
Yasunori {-i | --image} id [id...]
```

### Non arguments

Show ramdom

- API: `https://api.yasunori.dev/awesome/random`
- Image: `https://image.yasunori.dev/ogp`

### `-list`
### `-l` or `--list`

Show yasunori entries.

- API: `https://api.yasunori.dev/awesome`

### `-i` or `--image`

Get OGP image and show.
(Required Sixel supported terminal)

### id

Show specified entry of ID number.

- API: `https://api.yasunori.dev/awesome/{id}`
- Image: `https://image.yasunori.dev/ogp?id={id}`

## Build

Expand Down
96 changes: 68 additions & 28 deletions packages/Yasunori.NET/cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CommandLine;
using Yasunori.Net;

namespace Yasunori;
Expand All @@ -6,36 +7,75 @@ class Program
{
private static void Main(string[] args)
{
if (args.Length == 0)
{
var task = Client.GetRandom();
task.Wait();
Console.WriteLine(Markdown.RenderAsVT100(task.Result));
}
else if (args[0] == "-list")
{
var task = Client.GetAll();
task.Wait();
foreach (var y in task.Result.OrderBy(yasu => yasu.Id))
Parser.Default.ParseArguments<Options>(args)
.WithParsed(options =>
{
Console.WriteLine(y.ToString());
}
}
else
{
var tasks = new List<Task<YasunoriData>>();
foreach (var arg in args)
{
if (uint.TryParse(arg, out var id))
if (options.ShowList)
{
tasks.Add(Client.GetById(id));
var task = Client.GetAll();
task.Wait();
foreach (var y in task.Result.OrderBy(yasu => yasu.Id))
{
Console.WriteLine(y.ToString());
}
return;
}
}
Task.WaitAll(tasks.ToArray());
foreach (var task in tasks)
{
Console.WriteLine(Markdown.RenderAsVT100(task.Result));
}
}
var ids = options.IdList.ToArray();
if (ids.Length == 0)
{
if (options.ShowAsImage)
{
var task = Client.GetOGP();
task.Wait();
Console.WriteLine(Sixel.Encode(task.Result));
}
else
{
var task = Client.GetRandom();
task.Wait();
Console.WriteLine(Markdown.RenderAsVT100(task.Result));
}
return;
}
if (options.ShowAsImage)
{
var tasks = new List<Task<Stream>>();
foreach (var id in ids)
{
tasks.Add(Client.GetOGP(id));
}
Task.WaitAll(tasks.ToArray());
foreach (var task in tasks)
{
Console.WriteLine(Sixel.Encode(task.Result));
}
}
else
{
var tasks = new List<Task<YasunoriData>>();
foreach (var id in ids)
{
tasks.Add(Client.GetById(id));
}
Task.WaitAll(tasks.ToArray());
foreach (var task in tasks)
{
Console.WriteLine(Markdown.RenderAsVT100(task.Result));
}
}
});
}
}

class Options
{
[Option('l', "list", Required = false, HelpText = "Show Yasunori list")]
public bool ShowList { get; set; }

[Option('i', "image", Required = false, HelpText = "Show OGP Image (Required Sixel supported terminal)")]
public bool ShowAsImage { get; set; }

[Value(0, MetaName = "id")]
public IEnumerable<uint> IdList { get; set; } = [];
}
5 changes: 5 additions & 0 deletions packages/Yasunori.NET/cli/Yasunori.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.1.0</VersionPrefix>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\lib\Yasunori.Net.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,22 @@ public static async Task<T> GetYasunori<T>(string path)
return yasunori;
}

private static HttpClient HttpClient = new HttpClient()
private static Lazy<HttpClient> _client = new(() => new HttpClient() { BaseAddress = new Uri(BASE_URL) });
private static HttpClient HttpClient => _client.Value;

public const string OGP_URL = "https://image.yasunori.dev";
public const string OGP_PATH = "/ogp";

public static async Task<Stream> GetOGP()
{
BaseAddress = new Uri(BASE_URL)
};
return await ImgClinet.GetStreamAsync(OGP_PATH);
}

public static async Task<Stream> GetOGP(uint id)
{
return await ImgClinet.GetStreamAsync($"{OGP_PATH}?id={id}");
}

private static Lazy<HttpClient> _imgClient = new(() => new HttpClient() { BaseAddress = new Uri(OGP_URL) });
private static HttpClient ImgClinet => _imgClient.Value;
}
Loading

0 comments on commit c8750d9

Please sign in to comment.