Skip to content

Commit

Permalink
Merge pull request #33 from bobbahbrown/beestation-fix
Browse files Browse the repository at this point in the history
Version 1.3.3
  • Loading branch information
bobbah authored Apr 12, 2021
2 parents 000a37b + dde73b2 commit cb52f93
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 47 deletions.
6 changes: 3 additions & 3 deletions CentCom.API/CentCom.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>1.3.2</Version>
<AssemblyVersion>1.3.2.0</AssemblyVersion>
<FileVersion>1.3.2.0</FileVersion>
<Version>1.3.3</Version>
<AssemblyVersion>1.3.3.0</AssemblyVersion>
<FileVersion>1.3.3.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
6 changes: 3 additions & 3 deletions CentCom.Common/CentCom.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>1.3.2</Version>
<AssemblyVersion>1.3.2.0</AssemblyVersion>
<FileVersion>1.3.2.0</FileVersion>
<Version>1.3.3</Version>
<AssemblyVersion>1.3.3.0</AssemblyVersion>
<FileVersion>1.3.3.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 3 additions & 4 deletions CentCom.Server/BanSources/BeeBanParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using CentCom.Server.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -29,7 +28,7 @@ public class BeeBanParser : BanParser
};
public override bool SourceSupportsBanIDs => true;
private readonly BeeBanService _banService;
private const int PAGES_PER_BATCH = 12;
private const int PagesPerBatch = 12;

public BeeBanParser(DatabaseContext dbContext, BeeBanService banService, ILogger<BeeBanParser> logger) : base(dbContext, logger)
{
Expand All @@ -51,13 +50,13 @@ public override async Task<IEnumerable<Ban>> FetchNewBansAsync()

while (true)
{
var batch = await _banService.GetBansBatchedAsync(page, PAGES_PER_BATCH);
var batch = (await _banService.GetBansBatchedAsync(page, PagesPerBatch)).ToArray();
foundBans.AddRange(batch);
if (!batch.Any() || batch.Any(x => recent.Any(y => y.BanID == x.BanID)))
{
break;
}
page += PAGES_PER_BATCH;
page += PagesPerBatch;
}

return foundBans;
Expand Down
6 changes: 3 additions & 3 deletions CentCom.Server/CentCom.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<AssemblyVersion>1.3.2.0</AssemblyVersion>
<FileVersion>1.3.2.0</FileVersion>
<Version>1.3.2</Version>
<AssemblyVersion>1.3.3.0</AssemblyVersion>
<FileVersion>1.3.3.0</FileVersion>
<Version>1.3.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
58 changes: 24 additions & 34 deletions CentCom.Server/Services/BeeBanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,54 @@ namespace CentCom.Server.Services
public class BeeBanService
{
private readonly IRestClient _client;
private readonly string _baseURL = "https://beestation13.com/";
private readonly Regex _pagesPattern = new Regex("page [0-9]+ of (?<maxpages>[0-9]+)");
private readonly static BanSource _lrpSource = new BanSource() { Name = "bee-lrp" };
private readonly static BanSource _mrpSource = new BanSource() { Name = "bee-mrp" };
private const string BaseUrl = "https://beestation13.com/";
private readonly Regex _pagesPattern = new Regex("page [0-9]+ of (?<maxpages>[0-9]+)", RegexOptions.Compiled);
private static readonly BanSource LrpSource = new BanSource() { Name = "bee-lrp" };
private static readonly BanSource MrpSource = new BanSource() { Name = "bee-mrp" };
private readonly ILogger _logger;

public BeeBanService(ILogger<BeeBanService> logger)
{
_client = new RestClient(_baseURL);
_client = new RestClient(BaseUrl);
_logger = logger;
}

public async Task<IEnumerable<Ban>> GetBansAsync(int page = 1)
private async Task<IEnumerable<Ban>> GetBansAsync(int page = 1)
{
var request = new RestRequest("bans", Method.GET, DataFormat.Json).AddQueryParameter("json", "1").AddQueryParameter("page", page.ToString());
var response = await _client.ExecuteAsync(request);

if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
_logger.LogError($"Beestation website returned a non-200 HTTP response code: {response.StatusCode}, aborting parse.");
throw new BanSourceUnavailableException($"Beestation website returned a non-200 HTTP response code: {response.StatusCode}, aborting parse.");
_logger.LogError("Beestation website returned a non-200 HTTP response code: {StatusCode}, aborting parse", response.StatusCode);
throw new BanSourceUnavailableException($"Beestation website returned a non-200 HTTP response code: {response.StatusCode}, aborting parse");
}

var toReturn = new List<Ban>();
var content = JsonSerializer.Deserialize<IEnumerable<Dictionary<string, JsonElement>>>(response.Content);
foreach (var b in content)
{
var expiryString = b["unban_date"].GetString() ?? b["expire_date"].GetString();
var expiryString = b["unbanned_datetime"].GetString() ?? b["expiration_time"].GetString();
var toAdd = new Ban()
{
BannedOn = DateTime.Parse(b["ban_date"].GetString()).ToUniversalTime(),
BannedBy = b["banner"].GetString(),
BanType = ParseBanType(b["type"].GetString()),
Expires = expiryString == null ? (DateTime?)null : DateTime.Parse(expiryString).ToUniversalTime(),
CKey = b["user"].GetString(),
BannedOn = DateTime.SpecifyKind(DateTime.Parse(b["bantime"].GetString()), DateTimeKind.Utc),
BannedBy = b["a_ckey"].GetString(),
BanType = b["roles"].EnumerateArray().Select(x => x.GetString()).Contains("Server")
? BanType.Server
: BanType.Job,
Expires = expiryString == null ? (DateTime?)null : DateTime.SpecifyKind(DateTime.Parse(expiryString), DateTimeKind.Utc),
CKey = b["ckey"].GetString(),
Reason = b["reason"].GetString(),
BanID = b["id"].GetInt32().ToString(),
SourceNavigation = ParseBanSource(b["server"].GetString())
SourceNavigation = ParseBanSource(b["server_name"].GetString())
};

if (toAdd.BanType == BanType.Job)
{
toAdd.AddJobRange(b["job"].EnumerateArray().Select(x => x.GetString()));
toAdd.AddJobRange(b["roles"].EnumerateArray().Select(x => x.GetString()));
}

if (b["global"].GetBoolean())
if (b["global_ban"].GetInt32() == 1)
{
toAdd.AddAttribute(BanAttribute.BeeStationGlobal);
}
Expand All @@ -88,43 +90,31 @@ await range.AsyncParallelForEach(async page =>
return toReturn;
}

public async Task<int> GetNumberOfPagesAsync()
private async Task<int> GetNumberOfPagesAsync()
{
var request = new RestRequest("bans", Method.GET, DataFormat.None);
var result = await _client.ExecuteAsync(request);

if (result.StatusCode != System.Net.HttpStatusCode.OK)
{
throw new Exception($"Unexpected non-200 status code [{result.StatusCode}] when trying to retrieve number of ban pages on beestation13.com.");
throw new Exception($"Unexpected non-200 status code [{result.StatusCode}] when trying to retrieve number of ban pages on beestation13.com");
}

var match = _pagesPattern.Match(result.Content);
if (!match.Success)
{
throw new Exception("Failed to find page numbers on beestation13.com bans page");
}
else
{
return int.Parse(match.Groups["maxpages"].Value);
}
}

private static BanType ParseBanType(string raw)
{
return (raw.ToLower()) switch
{
"server" => BanType.Server,
"job" => BanType.Job,
_ => throw new Exception($"Failed to convert raw value of Beestation ban to BanType: \"{raw}\""),
};
return int.Parse(match.Groups["maxpages"].Value);
}

private static BanSource ParseBanSource(string raw)
{
return (raw.ToLower()) switch
{
"bs_golden" => _lrpSource,
"bs_sage" => _mrpSource,
"bs_golden" => LrpSource,
"bs_sage" => MrpSource,
_ => throw new Exception($"Failed to convert raw value of Beestation ban source to BanSource: \"{raw}\""),
};
}
Expand Down

0 comments on commit cb52f93

Please sign in to comment.