|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Text.Json.Serialization; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Flurl.Http; |
| 9 | +using JetBrains.Annotations; |
| 10 | +using SmartImage.Lib.Model; |
| 11 | +using SmartImage.Lib.Results; |
| 12 | + |
| 13 | +namespace SmartImage.Lib.Engines.Impl.Search; |
| 14 | + |
| 15 | +public class FluffleEngine : BaseSearchEngine, IDisposable |
| 16 | +{ |
| 17 | + |
| 18 | + public const string URL_ENDPOINT = "https://api.fluffle.xyz/v1/"; |
| 19 | + public const string URL_BASE = "https://fluffle.xyz/"; |
| 20 | + |
| 21 | + public FluffleEngine() : base(URL_BASE, URL_ENDPOINT) |
| 22 | + { |
| 23 | + MaxSize = 4_194_304; // MiB |
| 24 | + } |
| 25 | + |
| 26 | + protected override bool VerifyQuery(SearchQuery q) |
| 27 | + { |
| 28 | + return base.VerifyQuery(q); |
| 29 | + } |
| 30 | + |
| 31 | + protected override SearchResultStatus Verify(SearchQuery q) |
| 32 | + { |
| 33 | + return base.Verify(q); |
| 34 | + } |
| 35 | + |
| 36 | + public override async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default) |
| 37 | + { |
| 38 | + var br = await base.GetResultAsync(query, token); |
| 39 | + |
| 40 | + var s = $"{R1.Name}/{SearchClient.Asm.GetName().Version} (by {R1.Author} on GitHub)"; |
| 41 | + |
| 42 | + var sr = await Client.Request(EndpointUrl, "search") |
| 43 | + .WithHeaders(new |
| 44 | + { |
| 45 | + User_Agent = s |
| 46 | + }) |
| 47 | + .WithTimeout(Timeout) |
| 48 | + .PostMultipartAsync(c => |
| 49 | + { |
| 50 | + c.AddFile("file", query.Uni.Stream, "file"); |
| 51 | + c.AddString("includeNsfw", true.ToString()); |
| 52 | + c.AddString("limit", 32.ToString()); |
| 53 | + |
| 54 | + // c.AddString("platforms", null) |
| 55 | + // c.AddString("createLink", false) |
| 56 | + }, cancellationToken: token); |
| 57 | + |
| 58 | + var obj = await sr.GetJsonAsync<FluffleResponse>(); |
| 59 | + |
| 60 | + foreach (FluffleResult result in obj.Results) { |
| 61 | + var item = result.Convert(br, out var c); |
| 62 | + br.Results.Add(item); |
| 63 | + } |
| 64 | + |
| 65 | + return br; |
| 66 | + } |
| 67 | + |
| 68 | + protected override async ValueTask<Url> GetRawUrlAsync(SearchQuery query) |
| 69 | + { |
| 70 | + return await base.GetRawUrlAsync(query); |
| 71 | + } |
| 72 | + |
| 73 | + public override SearchEngineOptions EngineOption => SearchEngineOptions.Fluffle; |
| 74 | + |
| 75 | + public override void Dispose() |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +public class FluffleResultCredit |
| 82 | +{ |
| 83 | + [JsonPropertyName("id")] |
| 84 | + public int Id { get; set; } |
| 85 | + |
| 86 | + [JsonPropertyName("name")] |
| 87 | + public string Name { get; set; } |
| 88 | +} |
| 89 | + |
| 90 | +public class FluffleResult : IResultConvertable |
| 91 | +{ |
| 92 | + [JsonPropertyName("id")] |
| 93 | + public int Id { get; set; } |
| 94 | + |
| 95 | + [JsonPropertyName("score")] |
| 96 | + public double Score { get; set; } |
| 97 | + |
| 98 | + [JsonPropertyName("match")] |
| 99 | + public string Match { get; set; } |
| 100 | + |
| 101 | + [JsonPropertyName("platform")] |
| 102 | + public string Platform { get; set; } |
| 103 | + |
| 104 | + [JsonPropertyName("location")] |
| 105 | + public string Location { get; set; } |
| 106 | + |
| 107 | + [JsonPropertyName("isSfw")] |
| 108 | + public bool IsSfw { get; set; } |
| 109 | + |
| 110 | + [JsonPropertyName("thumbnail")] |
| 111 | + public FluffleResultThumbnail Thumbnail { get; set; } |
| 112 | + |
| 113 | + [JsonPropertyName("credits")] |
| 114 | + public List<FluffleResultCredit> Credits { get; set; } |
| 115 | + |
| 116 | + public SearchResultItem Convert(SearchResult sr, out SearchResultItem[] children) |
| 117 | + { |
| 118 | + children = []; |
| 119 | + |
| 120 | + var sri = new SearchResultItem(sr) |
| 121 | + { |
| 122 | + Artist = Credits.FirstOrDefault()?.Name, |
| 123 | + Url = Location, |
| 124 | + Similarity = Score * 100.0d, |
| 125 | + Metadata = this, |
| 126 | + Thumbnail = Thumbnail?.Location, |
| 127 | + Site = Platform |
| 128 | + }; |
| 129 | + return sri; |
| 130 | + } |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +public class FluffleResponse |
| 135 | +{ |
| 136 | + [JsonPropertyName("id")] |
| 137 | + public string Id { get; set; } |
| 138 | + |
| 139 | + [JsonPropertyName("stats")] |
| 140 | + public FluffleResultStats Stats { get; set; } |
| 141 | + |
| 142 | + [JsonPropertyName("results")] |
| 143 | + public List<FluffleResult> Results { get; set; } |
| 144 | +} |
| 145 | + |
| 146 | +public class FluffleResultStats |
| 147 | +{ |
| 148 | + [JsonPropertyName("count")] |
| 149 | + public int Count { get; set; } |
| 150 | + |
| 151 | + [JsonPropertyName("elapsedMilliseconds")] |
| 152 | + public int ElapsedMilliseconds { get; set; } |
| 153 | +} |
| 154 | + |
| 155 | +public class FluffleResultThumbnail |
| 156 | +{ |
| 157 | + [JsonPropertyName("width")] |
| 158 | + public int Width { get; set; } |
| 159 | + |
| 160 | + [JsonPropertyName("centerX")] |
| 161 | + public int CenterX { get; set; } |
| 162 | + |
| 163 | + [JsonPropertyName("height")] |
| 164 | + public int Height { get; set; } |
| 165 | + |
| 166 | + [JsonPropertyName("centerY")] |
| 167 | + public int CenterY { get; set; } |
| 168 | + |
| 169 | + [JsonPropertyName("location")] |
| 170 | + public string Location { get; set; } |
| 171 | +} |
| 172 | + |
0 commit comments