Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributors Page that auto-fills using GitHub API #26

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions ReplayBrowser/Helpers/GitHubApiHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System.Net.Http;
using System.Text.Json;
using Microsoft.Extensions.Caching.Memory;
using Serilog;

namespace ReplayBrowser.Helpers;

public class GitHubApiHelper
{
private readonly IMemoryCache _memoryCache;
private readonly string? _apiToken;

public GitHubApiHelper(IConfiguration configuration, IMemoryCache memoryCache)
{
_memoryCache = memoryCache;

try
{
_apiToken = configuration["GitHubAPIToken"];
}
catch (Exception e)
{
Log.Error("GitHubAPIToken not set, contributors cannot be fetched.");
}
}

public async Task<List<GitHubAccount>> GetContributors()
{
if (!_memoryCache.TryGetValue("GitHubContributors", out List<GitHubAccount> contributors))
{
try
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/Simyon264/ReplayBrowser/contributors"))
{
request.Headers.TryAddWithoutValidation("Accept", "application/vnd.github+json");
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {_apiToken}");
request.Headers.TryAddWithoutValidation("X-GitHub-Api-Version", "2022-11-28");
request.Headers.TryAddWithoutValidation("User-Agent", "YourAppNameHere");

var response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();

var responseBody = await response.Content.ReadAsStringAsync();

var responseJson = JsonSerializer.Deserialize<List<JsonElement>>(responseBody);
contributors = new List<GitHubAccount>();

foreach (var obj in responseJson)
{
string accountName = null;
string accountImageURL = null;
string accountLink = null;

if (obj.TryGetProperty("login", out var loginProperty))
{
accountName = loginProperty.GetString();
}

if (obj.TryGetProperty("avatar_url", out var avatarUrlProperty))
{
accountImageURL = avatarUrlProperty.GetString();
}

if (obj.TryGetProperty("html_url", out var htmlUrlProperty))
{
accountLink = htmlUrlProperty.GetString();
}

if (accountName != null && accountImageURL != null && accountLink != null)
{
var account = new GitHubAccount
{
AccountName = accountName,
AccountImageUrl = accountImageURL,
AccountLink = accountLink
};

contributors.Add(account);
}
else
{
Log.Warning("A required property was missing in the JSON response while attempting to fetch project contributors.");
}
}

Log.Information("Successfully fetched project contributor list.");

// Set cache options and cache the contributors list
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMinutes(30)); // Adjust expiration as needed

_memoryCache.Set("GitHubContributors", contributors, cacheEntryOptions);
}
}
}
catch (Exception e)
{
Log.Error($"Exception when querying GitHub: {e.Message} - {e.StackTrace}");

// return empty list
return new List<GitHubAccount>();
}
}

return contributors;
}
}

public struct GitHubAccount
{
public string AccountName { get; init; }
public string AccountImageUrl { get; init; }
public string AccountLink { get; init; }
}
34 changes: 34 additions & 0 deletions ReplayBrowser/Pages/Contributors.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@page "/contributors"

@inject GitHubApiHelper GitHubApiHelper

@using Microsoft.AspNetCore.Components.Web
@using ReplayBrowser.Pages.Shared
@using ReplayBrowser.Helpers

<PageTitle>Contributors</PageTitle>

<h3>Contributors</h3>

<div style="display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; align-items: center; padding: 20px;">
@if (_contributors.Count == 0)
{
<h4 style="color: red">Error fetching contributors from the GitHub API</h4>
}
else
{
@foreach (var contrib in _contributors)
{
<ContributorCard ContributorName="@contrib.AccountName" ContributorImage="@contrib.AccountImageUrl" ContributorLink="@contrib.AccountLink" />
}
}
</div>

@code {
private List<GitHubAccount> _contributors = new List<GitHubAccount>();

protected async override Task OnInitializedAsync()
{
_contributors = await GitHubApiHelper.GetContributors();
}
}
29 changes: 29 additions & 0 deletions ReplayBrowser/Pages/Shared/ContributorCard.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="card"
style="display: flex; flex-direction: column; align-items: center; justify-content: center; width: 200px;">
<div class="card-title">
<br /><h4>@ContributorName</h4>
</div>

<div>
<div>
<img src="@ContributorImage"
style="width: 50px; height: 50px; border-radius: 100px; transform: translateX(calc(25.8px / 2))"
alt="Profile Picture of the Contributor"
/>
</div>

<br />

<a href="@ContributorLink" class="btn btn-primary"
style="margin-bottom: 25px">
GitHub
</a>
</div>
</div>


@code {
[Parameter] public required string ContributorName { get; set; }
[Parameter] public required string ContributorImage { get; set; }
[Parameter] public required string ContributorLink { get; set; }
}
2 changes: 1 addition & 1 deletion ReplayBrowser/Pages/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@

<footer class="footer text-muted text-center">
<div class="container">
<p>Replay Browser is a project by Simyon, YuNii and Saphire. Source code available on <a href="https://github.com/Simyon264/ReplayBrowser">GitHub</a>.</p>
<p>Replay Browser is a project by Simyon and <a href="/contributors">contributors</a>. Source code available on <a href="https://github.com/Simyon264/ReplayBrowser">GitHub</a>.</p>
</div>

<div class="container">
Expand Down
1 change: 1 addition & 0 deletions ReplayBrowser/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<ReplayParserService>();
services.AddSingleton<AnalyticsService>();
services.AddSingleton<NoticeHelper>();
services.AddSingleton<GitHubApiHelper>();

services.AddHostedService<BackgroundServiceStarter<ReplayParserService>>();
services.AddHostedService<BackgroundServiceStarter<AccountService>>();
Expand Down
34 changes: 17 additions & 17 deletions ReplayBrowser/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,135 +9,135 @@
"ReplayUrls": [
{
"url": "https://cdn.networkgamez.com/replays/replays/goobstation/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "goobstation",
"fallBackServerId": "goobstation",
"replayRegex": "(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip",
"serverNameRegex": ""
},
{
"url": "http://cdn.harmony14.com:27690/replays/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "harmony",
"fallBackServerId": "harmony",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://replays.rouny-ss14.com/replays/alamo/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "alamo",
"fallBackServerId": "rmc14",
"replayRegex": "(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip",
"serverNameRegex": ""
},
{
"url": "https://replays.rouny-ss14.com/replays/normandy/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "rouny_2",
"fallBackServerId": "rmc14",
"replayRegex": "(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip",
"serverNameRegex": ""
},
{
"url": "https://moon.spacestation14.com/replays/rain_one_one_gateway/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "rain_one_one_gateway",
"fallBackServerId": "wizards_rain",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/rain_one_three_core/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "rain_one_three_core",
"fallBackServerId": "wizards_rain",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/rain_two_one_tide/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "rain_two_one_tide",
"fallBackServerId": "wizards_rain",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://sector-umbra.net/replays/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "Sector Umbra",
"fallBackServerId": "sector-umbra",
"replayRegex": "(\\d{4}-\\d{2}-\\d{2})-round_\\d+\\.zip",
"serverNameRegex": ""
},
{
"url": "https://axolotl.yuniiworks.de/replays/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "axolotl",
"fallBackServerId": "axolotl",
"replayRegex": "^[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "(^[a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/leviathan/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "leviathan",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/lizard/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "lizard",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/miros/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "miros",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/salamander/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "salamander",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://moon.spacestation14.com/replays/vulture/",
"provider": "caddy",
"provider": "dummy",
"fallBackServerName": "vulture",
"fallBackServerId": "wizards",
"replayRegex": "[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "([a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://replays.delta-v.org/apoapsis/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "Server 1",
"fallBackServerId": "deltav",
"replayRegex": "^[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "(^[a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://replays.delta-v.org/periapsis/",
"provider": "nginx",
"provider": "dummy",
"fallBackServerName": "Server 2",
"fallBackServerId": "deltav",
"replayRegex": "^[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
"serverNameRegex": "(^[a-zA-Z0-9-]+)-\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2}-round_\\d+\\.zip$"
},
{
"url": "https://replays.delta-v.org/horizon/",
"provider": "nginx",
"provider": "dummy",
bhenrich marked this conversation as resolved.
Show resolved Hide resolved
"fallBackServerName": "Server 3",
"fallBackServerId": "deltav",
"replayRegex": "^[a-zA-Z0-9-]+-(\\d{4}_\\d{2}_\\d{2}-\\d{2}_\\d{2})-round_\\d+\\.zip$",
Expand Down
Loading