Add safecheck client

This commit is contained in:
2025-06-01 21:46:24 -03:00
parent 6ebfde013a
commit 117d8b5ab6
9 changed files with 170 additions and 1 deletions

View File

@ -7,6 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tribufu.Generated", "src\Tr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tribufu.Example", "src\Tribufu.Example\Tribufu.Example.csproj", "{D6392A29-E2DC-4050-B4C1-B279DD2D226D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tribufu.SafeCheck", "src\Tribufu.SafeCheck\Tribufu.SafeCheck.csproj", "{B1463692-23C0-435B-A018-B6ED8F305E8D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -53,8 +57,23 @@ Global
{D6392A29-E2DC-4050-B4C1-B279DD2D226D}.Release|x64.Build.0 = Release|Any CPU
{D6392A29-E2DC-4050-B4C1-B279DD2D226D}.Release|x86.ActiveCfg = Release|Any CPU
{D6392A29-E2DC-4050-B4C1-B279DD2D226D}.Release|x86.Build.0 = Release|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Debug|x64.ActiveCfg = Debug|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Debug|x64.Build.0 = Debug|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Debug|x86.ActiveCfg = Debug|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Debug|x86.Build.0 = Debug|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Release|Any CPU.Build.0 = Release|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Release|x64.ActiveCfg = Release|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Release|x64.Build.0 = Release|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Release|x86.ActiveCfg = Release|Any CPU
{B1463692-23C0-435B-A018-B6ED8F305E8D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B1463692-23C0-435B-A018-B6ED8F305E8D} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
EndGlobalSection
EndGlobal

View File

@ -3,6 +3,7 @@
using dotenv.net;
using Tribufu.Generated.Client;
using Tribufu.SafeCheck;
namespace Tribufu.Test
{
@ -19,7 +20,9 @@ namespace Tribufu.Test
try
{
var result = await tribufu.GetUserInfoAsync();
//var result = await tribufu.GetUserInfoAsync();
var safecheck = new TribufuSafeCheck(apiKey, "http://localhost:5100");
var result = await safecheck.CheckImageAsync("");
Console.WriteLine(result);
}
catch (ApiException e)

View File

@ -10,5 +10,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tribufu\Tribufu.csproj" />
<ProjectReference Include="..\Tribufu.SafeCheck\Tribufu.SafeCheck.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1 @@
# Tribufu

View File

@ -0,0 +1,25 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
using Newtonsoft.Json;
namespace Tribufu.SafeCheck
{
public class SafeCheckDetections
{
[JsonProperty("sexy")]
public double Sexy { get; set; }
[JsonProperty("porn")]
public double Porn { get; set; }
[JsonProperty("neutral")]
public double Neutral { get; set; }
[JsonProperty("hentai")]
public double Hentai { get; set; }
[JsonProperty("drawing")]
public double Drawing { get; set; }
}
}

View File

@ -0,0 +1,18 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
using Newtonsoft.Json;
namespace Tribufu.SafeCheck
{
public class SafeCheckRequest
{
[JsonProperty("image_url")]
public string ImageUrl { get; set; }
public SafeCheckRequest(string imageUrl)
{
ImageUrl = imageUrl;
}
}
}

View File

@ -0,0 +1,19 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
using Newtonsoft.Json;
namespace Tribufu.SafeCheck
{
public class SafecheckResponse
{
[JsonProperty("safe")]
public bool Safe { get; set; }
[JsonProperty("reason")]
public string? Reason { get; set; }
[JsonProperty("detections")]
public SafeCheckDetections? Detections { get; set; }
}
}

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageId>Tribufu.SafeCheck</PackageId>
<Description>Tribufu SafeCheck</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
<PropertyGroup>
<AppDesignerFolder>Properties</AppDesignerFolder>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="RestSharp" />
<PackageReference Include="System.ComponentModel.Annotations" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tribufu\Tribufu.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,56 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
using Newtonsoft.Json;
using RestSharp;
namespace Tribufu.SafeCheck
{
public sealed class TribufuSafeCheck
{
private readonly RestClient _client;
private readonly string? _apiKey;
public const string DefaultBaseUrl = "https://safecheck.tribufu.com";
public TribufuSafeCheck(string? apiKey = null, string baseUrl = DefaultBaseUrl)
{
var options = new RestClientOptions(baseUrl)
{
UserAgent = TribufuApi.GetUserAgent(),
};
_client = new RestClient(options);
_apiKey = apiKey;
}
public async Task<bool> CheckImageAsync(string imageUrl)
{
if (string.IsNullOrWhiteSpace(imageUrl))
{
throw new ArgumentException("Image URL cannot be null or empty.", nameof(imageUrl));
}
var request = new RestRequest("/image", Method.Post);
request.AddHeader("Content-Type", "application/json");
if (!string.IsNullOrEmpty(_apiKey))
{
request.AddHeader("Authorization", $"ApiKey {_apiKey}");
}
var requestBody = new SafeCheckRequest(imageUrl);
request.AddStringBody(JsonConvert.SerializeObject(requestBody), DataFormat.Json);
var response = await _client.ExecuteAsync(request);
if (!response.IsSuccessful)
{
throw new Exception($"Request failed: {response.StatusCode} - {response.Content}");
}
var result = JsonConvert.DeserializeObject<SafecheckResponse>(response.Content!);
return result?.Safe ?? false;
}
}
}