use image blurhash for rate command for better ratings

This commit is contained in:
13xforever
2025-04-05 10:37:10 +05:00
parent 4a2a44b8b3
commit 933b2248de
4 changed files with 46 additions and 13 deletions

View File

@@ -10,10 +10,7 @@ public static class TmdbHasher
private static readonly byte[] HmacKey = "F5DE66D2680E255B2DF79E74F890EBF349262F618BCAE2A9ACCDEE5156CE8DF2CDF2D48C71173CDC2594465B87405D197CF1AED3B7E9671EEB56CA6753C2E6B0".FromHexString();
public static string GetTitleHash(string productId)
{
using var hmacSha1 = new HMACSHA1(HmacKey);
return hmacSha1.ComputeHash(Encoding.ASCII.GetBytes(productId)).ToHexString();
}
=> HMACSHA1.HashData(HmacKey, Encoding.UTF8.GetBytes(productId)).ToHexString();
public static byte[] FromHexString(this string hexString)
{

View File

@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System.Net.Http;
using System.Text.RegularExpressions;
using CompatApiClient.Utils;
using CompatBot.Database;
using CompatBot.Database.Providers;
@@ -11,6 +12,7 @@ namespace CompatBot.Commands;
internal static partial class Misc
{
private static readonly HttpClient HttpClient = HttpClientFactory.Create();
private static readonly Random rng = new();
private static readonly List<string> EightBallAnswers =
@@ -343,14 +345,28 @@ internal static partial class Misc
whatever = whatever.ToLowerInvariant().StripInvisibleAndDiacritics();
var originalWhatever = whatever;
var matches = Instead().Matches(whatever);
if (matches.Any())
if (matches is [.., Match last])
{
var insteadWhatever = matches.Last().Groups["instead"].Value.TrimEager();
var insteadWhatever = last.Groups["instead"].Value.TrimEager();
if (!string.IsNullOrEmpty(insteadWhatever))
whatever = insteadWhatever;
}
foreach (var attachment in ctx.Message.Attachments)
{
if (attachment is { Width: > 0, Height: > 0, Url: {Length: >0} url })
{
await using var imgStream = await HttpClient.GetStreamAsync(url).ConfigureAwait(false);
if (await ColorGetter.GetBlurHashAsync(imgStream).ConfigureAwait(false) is {Length: >0} hash)
{
#if DEBUG
Config.Log.Trace($"Got image blur hash {hash}");
#endif
whatever += $" {hash}";
continue;
}
}
whatever += $" {attachment.FileSize}";
}
var nekoUser = await ctx.Client.GetUserAsync(272032356922032139ul).ConfigureAwait(false);
var nekoMember = await ctx.Client.GetMemberAsync(nekoUser).ConfigureAwait(false);
@@ -494,7 +510,11 @@ internal static partial class Misc
await ctx.Channel.SendMessageAsync("Rating nothing makes _**so much**_ sense, right?").ConfigureAwait(false);
else
{
var seed = (prefix + whatever).GetHashCode(StringComparison.CurrentCultureIgnoreCase);
var seedMsg = prefix + whatever;
var seed = seedMsg.GetStableHash();
#if DEBUG
Config.Log.Trace($"Rating seed is {seed:x8} for '{seedMsg}'");
#endif
var seededRng = new Random(seed);
var answer = choices[seededRng.Next(choices.Count)];
var msgBuilder = new DiscordMessageBuilder()

View File

@@ -1,4 +1,6 @@
using System.IO;
using Blurhash.ImageSharp;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.ColorSpaces;
using SixLabors.ImageSharp.ColorSpaces.Conversion;
using SixLabors.ImageSharp.PixelFormats;
@@ -8,7 +10,7 @@ namespace CompatBot.Utils;
internal static class ColorGetter
{
public static SixLabors.ImageSharp.Color GetDominantColor(SixLabors.ImageSharp.Image<Rgba32> img)
public static Color GetDominantColor(Image<Rgba32> img)
{
//img.Mutate(x => x.Resize(new ResizeOptions { Sampler = KnownResamplers.NearestNeighbor, Size = new Size(100, 0) }));
int r = 0;
@@ -42,7 +44,7 @@ internal static class ColorGetter
{
try
{
var img = await SixLabors.ImageSharp.Image.LoadAsync<Rgba32>(jpg).ConfigureAwait(false);
var img = await Image.LoadAsync<Rgba32>(jpg).ConfigureAwait(false);
var quantizerOptions = new QuantizerOptions { Dither = null, MaxColors = 4 };
var sampling = new ExtensivePixelSamplingStrategy();
var quantizer = new WuQuantizer().CreatePixelSpecificQuantizer<Rgba32>(new(), quantizerOptions);
@@ -69,4 +71,18 @@ internal static class ColorGetter
return defaultColor;
}
}
public static async ValueTask<string?> GetBlurHashAsync(Stream attachment)
{
try
{
var img = await Image.LoadAsync<Rgba32>(attachment).ConfigureAwait(false);
return Blurhasher.Encode(img, 4, 3);
}
catch (Exception e)
{
Config.Log.Warn(e, "Failed to get image blur hash");
return null;
}
}
}

View File

@@ -1,5 +1,6 @@
using System.Buffers;
using System.Globalization;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using CompatBot.Utils.Extensions;
using HomoglyphConverter;
@@ -447,9 +448,8 @@ public static partial class StringUtils
internal static int GetStableHash(this string str)
{
using var sha256 = System.Security.Cryptography.SHA256.Create();
var data = Encoding.UTF8.GetBytes(str);
var hash = sha256.ComputeHash(data);
var data = Encoding.UTF8.GetBytes(str.ToLowerInvariant());
var hash = SHA256.HashData(data);
return BitConverter.ToInt32(hash, 0);
}