-
Notifications
You must be signed in to change notification settings - Fork 45
/
ThumbnailProvider.cs
203 lines (179 loc) · 8.1 KB
/
ThumbnailProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using CompatBot.ThumbScrapper;
using CompatBot.Utils;
using DSharpPlus;
using DSharpPlus.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
namespace CompatBot.Database.Providers;
internal static class ThumbnailProvider
{
private static readonly HttpClient HttpClient = HttpClientFactory.Create();
private static readonly PsnClient.Client PsnClient = new();
private static readonly MemoryCache ColorCache = new(new MemoryCacheOptions{ ExpirationScanFrequency = TimeSpan.FromDays(1) });
public static async Task<string?> GetThumbnailUrlAsync(this DiscordClient client, string? productCode)
{
if (string.IsNullOrEmpty(productCode))
return null;
productCode = productCode.ToUpperInvariant();
var tmdbInfo = await PsnClient.GetTitleMetaAsync(productCode, Config.Cts.Token).ConfigureAwait(false);
if (tmdbInfo is { Icon.Url: string tmdbIconUrl })
return tmdbIconUrl;
await using var db = new ThumbnailDb();
var thumb = await db.Thumbnail.FirstOrDefaultAsync(t => t.ProductCode == productCode).ConfigureAwait(false);
//todo: add search task if not found
if (thumb?.EmbeddableUrl is {Length: >0} embeddableUrl)
return embeddableUrl;
if (string.IsNullOrEmpty(thumb?.Url) || !ScrapeStateProvider.IsFresh(thumb.Timestamp))
{
var gameTdbCoverUrl = await GameTdbScraper.GetThumbAsync(productCode).ConfigureAwait(false);
if (!string.IsNullOrEmpty(gameTdbCoverUrl))
{
if (thumb is null)
thumb = (await db.Thumbnail.AddAsync(new() {ProductCode = productCode, Url = gameTdbCoverUrl}).ConfigureAwait(false)).Entity;
else
thumb.Url = gameTdbCoverUrl;
thumb.Timestamp = DateTime.UtcNow.Ticks;
await db.SaveChangesAsync().ConfigureAwait(false);
}
}
if (string.IsNullOrEmpty(thumb?.Url))
return null;
var contentName = thumb.ContentId ?? thumb.ProductCode;
var (embedUrl, _) = await GetEmbeddableUrlAsync(client, contentName, thumb.Url).ConfigureAwait(false);
if (embedUrl is null)
return null;
thumb.EmbeddableUrl = embedUrl;
await db.SaveChangesAsync().ConfigureAwait(false);
return embedUrl;
}
public static async Task<string?> GetTitleNameAsync(string? productCode, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(productCode))
return null;
productCode = productCode.ToUpperInvariant();
await using var db = new ThumbnailDb();
var thumb = await db.Thumbnail.FirstOrDefaultAsync(
t => t.ProductCode == productCode,
cancellationToken: cancellationToken
).ConfigureAwait(false);
if (thumb?.Name is string result)
return result;
var title = (await PsnClient.GetTitleMetaAsync(productCode, cancellationToken).ConfigureAwait(false))?.Name;
try
{
if (!string.IsNullOrEmpty(title))
{
if (thumb == null)
await db.Thumbnail.AddAsync(new()
{
ProductCode = productCode,
Name = title,
}, cancellationToken).ConfigureAwait(false);
else
thumb.Name = title;
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
}
catch (Exception e)
{
Config.Log.Warn(e);
}
return title;
}
public static async Task<(string? url, DiscordColor color)> GetThumbnailUrlWithColorAsync(DiscordClient client, string contentId, DiscordColor defaultColor, string? url = null)
{
if (string.IsNullOrEmpty(contentId))
throw new ArgumentException("ContentID can't be empty", nameof(contentId));
contentId = contentId.ToUpperInvariant();
await using var db = new ThumbnailDb();
var info = await db.Thumbnail.FirstOrDefaultAsync(ti => ti.ContentId == contentId, Config.Cts.Token).ConfigureAwait(false);
info ??= new() {Url = url};
if (info.Url is null)
return (null, defaultColor);
DiscordColor? analyzedColor = null;
if (string.IsNullOrEmpty(info.EmbeddableUrl))
{
var (embedUrl, image) = await GetEmbeddableUrlAsync(client, contentId, info.Url).ConfigureAwait(false);
if (embedUrl is string eUrl)
{
info.EmbeddableUrl = eUrl;
if (image is byte[] jpg)
{
Config.Log.Trace("Getting dominant color for " + eUrl);
analyzedColor = ColorGetter.Analyze(jpg, defaultColor);
if (analyzedColor.HasValue
&& analyzedColor.Value.Value != defaultColor.Value)
info.EmbedColor = analyzedColor.Value.Value;
}
await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
}
}
if (!info.EmbedColor.HasValue && !analyzedColor.HasValue
|| info.EmbedColor.HasValue && info.EmbedColor.Value == defaultColor.Value)
{
var c = await GetImageColorAsync(info.EmbeddableUrl, defaultColor).ConfigureAwait(false);
if (c.HasValue && c.Value.Value != defaultColor.Value)
{
info.EmbedColor = c.Value.Value;
await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
}
}
var color = info.EmbedColor.HasValue ? new(info.EmbedColor.Value) : defaultColor;
return (info.EmbeddableUrl, color);
}
public static async Task<(string? url, byte[]? image)> GetEmbeddableUrlAsync(DiscordClient client, string contentId, string url)
{
try
{
if (!string.IsNullOrEmpty(Path.GetExtension(url)))
return (url, null);
await using var imgStream = await HttpClient.GetStreamAsync(url).ConfigureAwait(false);
await using var memStream = Config.MemoryStreamManager.GetStream();
await imgStream.CopyToAsync(memStream).ConfigureAwait(false);
// minimum jpg size is 119 bytes, png is 67 bytes
if (memStream.Length < 64)
return (null, null);
memStream.Seek(0, SeekOrigin.Begin);
var spam = await client.GetChannelAsync(Config.ThumbnailSpamId).ConfigureAwait(false);
var message = await spam.SendMessageAsync(new DiscordMessageBuilder().AddFile(contentId + ".jpg", memStream).WithContent(contentId)).ConfigureAwait(false);
url = message.Attachments[0].Url;
return (url, memStream.ToArray());
}
catch (Exception e)
{
Config.Log.Warn(e);
}
return (null, null);
}
public static async Task<DiscordColor?> GetImageColorAsync(string? url, DiscordColor? defaultColor = null)
{
try
{
if (string.IsNullOrEmpty(url))
return null;
if (ColorCache.TryGetValue(url, out DiscordColor? result))
return result;
await using var imgStream = await HttpClient.GetStreamAsync(url).ConfigureAwait(false);
await using var memStream = Config.MemoryStreamManager.GetStream();
await imgStream.CopyToAsync(memStream).ConfigureAwait(false);
// minimum jpg size is 119 bytes, png is 67 bytes
if (memStream.Length < 64)
return null;
memStream.Seek(0, SeekOrigin.Begin);
Config.Log.Trace("Getting dominant color for " + url);
result = ColorGetter.Analyze(memStream.ToArray(), defaultColor);
ColorCache.Set(url, result, TimeSpan.FromHours(1));
return result;
}
catch (Exception e)
{
Config.Log.Warn(e);
}
return null;
}
}