Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sboulema committed Oct 30, 2024
0 parents commit ddb3e09
Show file tree
Hide file tree
Showing 18 changed files with 392 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: F1Widget

on:
push:
branches:
- main
- 'feature/**'

env:
version: '1.0.${{ github.run_number }}'
tag: ${{ github.ref_name == 'main' && 'latest' || github.run_number }}

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
push: true
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/feedarr:${{ env.tag }}
${{ secrets.DOCKERHUB_USERNAME }}/feedarr:${{ github.run_number }}
ghcr.io/${{ github.actor }}/feedarr:${{ env.tag }}
ghcr.io/${{ github.actor }}/feedarr:${{ github.run_number }}
- name: Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.PORT }}
script: /home/sboulema/deployDockerContainer F1Widget
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vs
/src/artifacts
/src/.vs
/src/*.user
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM mcr.microsoft.com/dotnet/sdk:9.0-alpine AS sdk
WORKDIR /app
COPY . .
RUN dotnet publish --runtime linux-musl-x64 --self-contained -c Release -o out

FROM mcr.microsoft.com/dotnet/runtime-deps:9.0-alpine
RUN apk add --no-cache tzdata
WORKDIR /app
COPY --from=sdk /app/out .
ENTRYPOINT ["./F1Widget"]
27 changes: 27 additions & 0 deletions F1Widget.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "F1Widget", "src/F1Widget.csproj", "{CEA09048-92A9-4B95-B864-0021BC48D317}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EA9E00E2-7791-43D9-BF61-EE0EB9BF8047}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CEA09048-92A9-4B95-B864-0021BC48D317}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEA09048-92A9-4B95-B864-0021BC48D317}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEA09048-92A9-4B95-B864-0021BC48D317}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEA09048-92A9-4B95-B864-0021BC48D317}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CEA09048-92A9-4B95-B864-0021BC48D317} = {EA9E00E2-7791-43D9-BF61-EE0EB9BF8047}
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Samir Boulema

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# F1 Widget
Android KWGT widget that displays the next upcoming Formula 1 race.

## API
The API provides the widget with all the session data and start times needed by the widget.

It acts like a small proxy between the widget and the source F1 Calendar JSON formatting the JSON so that it can easily be consumed by the widget

### Settings
The API has a single setting controlling which time zone to show the session times in.
The setting can be changed using environment variables.

| Name | Value |
| ---- | ----- |
| TZ | TZ identifier taken from [List of tz database time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) |

## Widgets
TODO

## Documentation

### Transformation steps
- Based on the current year we get the correct JSON from the F1 Calendar GitHub
- Order the races based on the FP1 start times
- Find the first race that has a session in the future
- Convert session times to configured time zone
- Format session times to fit on the widget

### Model
The widget requires a JSON model likes this:

```
{
"race": {
"location": "Sao Paulo",
"fp1": "01 Nov 15:30",
"fp2": null,
"fp3": null,
"qualifying": "02 Nov 19:00",
"sprint": "02 Nov 15:00",
"gp": "03 Nov 18:00"
}
}
```

## Links
- [F1 Calendar](https://f1calendar.com/)
- [F1 Calendar GitHub](https://github.com/sportstimes/f1)
- [Kustom HQ](https://docs.kustom.rocks/)
- [KWGT](https://play.google.com/store/apps/details?id=org.kustom.widget&hl=nl)
- [Time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
26 changes: 26 additions & 0 deletions src/Converters/DateTimeJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Globalization;

namespace F1Widget.Converters;

public class DateTimeJsonConverter : JsonConverter<DateTime>
{
private static readonly CultureInfo Culture = CultureInfo.InvariantCulture;
private const string Format = "dd MMM HH:mm";

public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (!DateTime.TryParseExact(reader.GetString(), Format, Culture, DateTimeStyles.None, out var dateTime))
{
throw new JsonException();
}

return dateTime;
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("dd MMM HH:mm"));
}
}
5 changes: 5 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<UseArtifactsOutput>true</UseArtifactsOutput>
</PropertyGroup>
</Project>
9 changes: 9 additions & 0 deletions src/F1Widget.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
27 changes: 27 additions & 0 deletions src/F1Widget.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "F1Widget", "F1Widget.csproj", "{CEA09048-92A9-4B95-B864-0021BC48D317}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EA9E00E2-7791-43D9-BF61-EE0EB9BF8047}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CEA09048-92A9-4B95-B864-0021BC48D317}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEA09048-92A9-4B95-B864-0021BC48D317}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEA09048-92A9-4B95-B864-0021BC48D317}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEA09048-92A9-4B95-B864-0021BC48D317}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CEA09048-92A9-4B95-B864-0021BC48D317} = {EA9E00E2-7791-43D9-BF61-EE0EB9BF8047}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions src/Models/Calendar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace F1Widget.Models;

public class Calendar
{
public List<Race> Races { get; set; } = [];
}
6 changes: 6 additions & 0 deletions src/Models/NextRaceResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace F1Widget.Models;

public class NextRaceResponse
{
public NextRaceResponseRace? Race { get; set; }
}
30 changes: 30 additions & 0 deletions src/Models/NextRaceResponseRace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using F1Widget.Converters;
using System.Text.Json.Serialization;

namespace F1Widget.Models;

public class NextRaceResponseRace
{
public string Location { get; set; } = string.Empty;

[JsonConverter(typeof(DateTimeJsonConverter))]
[JsonPropertyName("fp1")]
public DateTime? FP1 { get; set; }

[JsonConverter(typeof(DateTimeJsonConverter))]
[JsonPropertyName("fp2")]
public DateTime? FP2 { get; set; }

[JsonConverter(typeof(DateTimeJsonConverter))]
[JsonPropertyName("fp3")]
public DateTime? FP3 { get; set; }

[JsonConverter(typeof(DateTimeJsonConverter))]
public DateTime? Qualifying { get; set; }

[JsonConverter(typeof(DateTimeJsonConverter))]
public DateTime? Sprint { get; set; }

[JsonConverter(typeof(DateTimeJsonConverter))]
public DateTime? GP { get; set; }
}
8 changes: 8 additions & 0 deletions src/Models/Race.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace F1Widget.Models;

public class Race
{
public string Location { get; set; } = string.Empty;

public Dictionary<string, DateTime> Sessions { get; set; } = [];
}
67 changes: 67 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using F1Widget.Models;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Http.Json;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

builder.Services
.Configure<JsonOptions>(options =>
{
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});

var app = builder.Build();

var destinationTimeZone = TimeZoneInfo.FindSystemTimeZoneById(app.Configuration["TZ"] ?? "Etc/UTC");

app.MapGet("/next", async Task<Results<Ok<NextRaceResponse>, NotFound>>() =>
{
var calendarURL = $"https://raw.githubusercontent.com/sportstimes/f1/refs/heads/main/_db/f1/{DateTime.UtcNow.Year}.json";

var client = new HttpClient();
var calendar = await client.GetFromJsonAsync<Calendar>(calendarURL);

if (calendar == null)
{
return TypedResults.NotFound();
}

var nextRace = calendar
.Races
.OrderBy(race => race.Sessions["fp1"])
.FirstOrDefault(race => race.Sessions.Any(session => session.Value >= DateTime.UtcNow));

if (nextRace == null)
{
return TypedResults.NotFound();
}

return TypedResults.Ok(
new NextRaceResponse
{
Race = new()
{
Location = nextRace.Location,
FP1 = GetSessionDateTime(nextRace, "fp1"),
FP2 = GetSessionDateTime(nextRace, "fp2"),
FP3 = GetSessionDateTime(nextRace, "fp3"),
Qualifying = GetSessionDateTime(nextRace, "qualifying"),
Sprint = GetSessionDateTime(nextRace, "sprint"),
GP = GetSessionDateTime(nextRace, "gp"),
}
}
);
});

app.Run();

DateTime? GetSessionDateTime(Race race, string sessionName)
{
if (!race.Sessions.TryGetValue(sessionName, out var sessionDateTime))
{
return null;
}

return TimeZoneInfo.ConvertTimeFromUtc(sessionDateTime, destinationTimeZone);
}
24 changes: 24 additions & 0 deletions src/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5176",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7283;http://localhost:5176",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"TZ": "Europe/Amsterdam"
}
}
}
}
Loading

0 comments on commit ddb3e09

Please sign in to comment.