- Updated editorconfig rules to hopefully look a bit nicer.

- Removed configureawait(false) from everywhere as it doesnt' do anything in a console app and just makes the code look ugly
- Started using .WhenAll extension instead of Task.WhenAll to make it look nicer when chaining methods
This commit is contained in:
Kwoth
2021-12-28 21:14:26 +01:00
parent d093f7eed7
commit 723447c7d4
171 changed files with 1523 additions and 1594 deletions

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Net.Http.Json;
namespace NadekoBot.Modules.Nsfw.Common;
@@ -13,7 +13,7 @@ public abstract class DapiImageDownloader : ImageDownloader<DapiImageObject>
public abstract Task<bool> IsTagValid(string tag, CancellationToken cancel = default);
protected async Task<bool> AllTagsValid(string[] tags, CancellationToken cancel = default)
{
var results = await Task.WhenAll(tags.Select(tag => IsTagValid(tag, cancel)));
var results = await tags.Select(tag => IsTagValid(tag, cancel)).WhenAll();
// if any of the tags is not valid, the query is not valid
foreach (var result in results)
@@ -32,14 +32,13 @@ public abstract class DapiImageDownloader : ImageDownloader<DapiImageObject>
if (tags.Length > 2)
return new();
if (!await AllTagsValid(tags, cancel).ConfigureAwait(false))
if (!await AllTagsValid(tags, cancel))
return new();
var tagString = ImageDownloaderHelper.GetTagString(tags, isExplicit);
var uri = $"{_baseUrl}/posts.json?limit=200&tags={tagString}&page={page}";
var imageObjects = await _http.GetFromJsonAsync<DapiImageObject[]>(uri, _serializerOptions, cancel)
.ConfigureAwait(false);
var imageObjects = await _http.GetFromJsonAsync<DapiImageObject[]>(uri, _serializerOptions, cancel);
if (imageObjects is null)
return new();
return imageObjects

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Net.Http.Json;
namespace NadekoBot.Modules.Nsfw.Common;
@@ -15,10 +15,10 @@ public class DerpibooruImageDownloader : ImageDownloader<DerpiImageObject>
var uri = $"https://www.derpibooru.org/api/v1/json/search/images?q={tagString.Replace('+', ',')}&per_page=49&page={page}";
using var req = new HttpRequestMessage(HttpMethod.Get, uri);
req.Headers.AddFakeHeaders();
using var res = await _http.SendAsync(req, cancel).ConfigureAwait(false);
using var res = await _http.SendAsync(req, cancel);
res.EnsureSuccessStatusCode();
var container = await res.Content.ReadFromJsonAsync<DerpiContainer>(_serializerOptions, cancel).ConfigureAwait(false);
var container = await res.Content.ReadFromJsonAsync<DerpiContainer>(_serializerOptions, cancel);
if (container?.Images is null)
return new();

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Net.Http.Json;
namespace NadekoBot.Modules.Nsfw.Common;
@@ -15,10 +15,10 @@ public class E621ImageDownloader : ImageDownloader<E621Object>
var uri = $"https://e621.net/posts.json?limit=32&tags={tagString}&page={page}";
using var req = new HttpRequestMessage(HttpMethod.Get, uri);
req.Headers.AddFakeHeaders();
using var res = await _http.SendAsync(req, cancel).ConfigureAwait(false);
using var res = await _http.SendAsync(req, cancel);
res.EnsureSuccessStatusCode();
var data = await res.Content.ReadFromJsonAsync<E621Response>(_serializerOptions, cancel).ConfigureAwait(false);
var data = await res.Content.ReadFromJsonAsync<E621Response>(_serializerOptions, cancel);
if (data?.Posts is null)
return new();

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Text.Json;
namespace NadekoBot.Modules.Nsfw.Common;
@@ -15,7 +15,7 @@ public class GelbooruImageDownloader : ImageDownloader<DapiImageObject>
var uri = $"http://gelbooru.com/index.php?page=dapi&s=post&json=1&q=index&limit=100" +
$"&tags={tagString}&pid={page}";
using var req = new HttpRequestMessage(HttpMethod.Get, uri);
using var res = await _http.SendAsync(req, cancel).ConfigureAwait(false);
using var res = await _http.SendAsync(req, cancel);
res.EnsureSuccessStatusCode();
var resString = await res.Content.ReadAsStringAsync(cancel);
if (string.IsNullOrWhiteSpace(resString))

View File

@@ -29,7 +29,7 @@ public abstract class ImageDownloader<T> : IImageDownloader
public async Task<List<ImageData>> DownloadImageDataAsync(string[] tags, int page, bool isExplicit = false,
CancellationToken cancel = default)
{
var images = await DownloadImagesAsync(tags, page, isExplicit, cancel).ConfigureAwait(false);
var images = await DownloadImagesAsync(tags, page, isExplicit, cancel);
return images.Select(x => x.ToCachedImageData(Booru)).ToList();
}
}

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Net.Http.Json;
namespace NadekoBot.Modules.Nsfw.Common;
@@ -15,8 +15,7 @@ public sealed class KonachanImageDownloader : ImageDownloader<DapiImageObject>
{
var tagString = ImageDownloaderHelper.GetTagString(tags, isExplicit);
var uri = $"{_baseUrl}/post.json?s=post&q=index&limit=200&tags={tagString}&page={page}";
var imageObjects = await _http.GetFromJsonAsync<DapiImageObject[]>(uri, _serializerOptions, cancel)
.ConfigureAwait(false);
var imageObjects = await _http.GetFromJsonAsync<DapiImageObject[]>(uri, _serializerOptions, cancel);
if (imageObjects is null)
return new();
return imageObjects

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Net.Http.Json;
namespace NadekoBot.Modules.Nsfw.Common;
@@ -14,7 +14,7 @@ public class Rule34ImageDownloader : ImageDownloader<Rule34Object>
var tagString = ImageDownloaderHelper.GetTagString(tags);
var uri = $"https://rule34.xxx/index.php?page=dapi&s=post&q=index&json=1&limit=100" +
$"&tags={tagString}&pid={page}";
var images = await _http.GetFromJsonAsync<List<Rule34Object>>(uri, _serializerOptions, cancel).ConfigureAwait(false);
var images = await _http.GetFromJsonAsync<List<Rule34Object>>(uri, _serializerOptions, cancel);
if (images is null)
return new();

View File

@@ -20,7 +20,7 @@ public sealed class SankakuImageDownloader : ImageDownloader<SankakuImageObject>
var tagString = ImageDownloaderHelper.GetTagString(tags, false);
var uri = $"{_baseUrl}/posts?tags={tagString}&limit=50";
var data = await _http.GetStringAsync(uri).ConfigureAwait(false);
var data = await _http.GetStringAsync(uri);
return JsonSerializer.Deserialize<SankakuImageObject[]>(data, _serializerOptions)
.Where(x => !string.IsNullOrWhiteSpace(x.FileUrl) && x.FileType.StartsWith("image"))
.ToList();

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Net.Http.Json;
namespace NadekoBot.Modules.Nsfw.Common;
@@ -16,8 +16,7 @@ public sealed class YandereImageDownloader : ImageDownloader<DapiImageObject>
var tagString = ImageDownloaderHelper.GetTagString(tags, isExplicit);
var uri = $"{_baseUrl}/post.json?limit=200&tags={tagString}&page={page}";
var imageObjects = await _http.GetFromJsonAsync<DapiImageObject[]>(uri, _serializerOptions, cancel)
.ConfigureAwait(false);
var imageObjects = await _http.GetFromJsonAsync<DapiImageObject[]>(uri, _serializerOptions, cancel);
if (imageObjects is null)
return new();
return imageObjects

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using NadekoBot.Modules.Searches.Common;
using Newtonsoft.Json.Linq;
@@ -25,15 +25,14 @@ public class NSFW : NadekoModule<ISearchImagesService>
using (var http = _httpFactory.CreateClient())
{
obj = JArray.Parse(await http
.GetStringAsync($"http://api.oboobs.ru/boobs/{new NadekoRandom().Next(0, 10330)}")
.ConfigureAwait(false))[0];
.GetStringAsync($"http://api.oboobs.ru/boobs/{new NadekoRandom().Next(0, 10330)}"))[0];
}
await ctx.Channel.SendMessageAsync($"http://media.oboobs.ru/{obj["preview"]}").ConfigureAwait(false);
await ctx.Channel.SendMessageAsync($"http://media.oboobs.ru/{obj["preview"]}");
}
catch (Exception ex)
{
await SendErrorAsync(ex.Message).ConfigureAwait(false);
await SendErrorAsync(ex.Message);
}
}
@@ -45,15 +44,14 @@ public class NSFW : NadekoModule<ISearchImagesService>
using (var http = _httpFactory.CreateClient())
{
obj = JArray.Parse(await http
.GetStringAsync($"http://api.obutts.ru/butts/{new NadekoRandom().Next(0, 4335)}")
.ConfigureAwait(false))[0];
.GetStringAsync($"http://api.obutts.ru/butts/{new NadekoRandom().Next(0, 4335)}"))[0];
}
await Channel.SendMessageAsync($"http://media.obutts.ru/{obj["preview"]}").ConfigureAwait(false);
await Channel.SendMessageAsync($"http://media.obutts.ru/{obj["preview"]}");
}
catch (Exception ex)
{
await SendErrorAsync(ex.Message).ConfigureAwait(false);
await SendErrorAsync(ex.Message);
}
}
@@ -70,7 +68,7 @@ public class NSFW : NadekoModule<ISearchImagesService>
if (!_service.AutoHentaiTimers.TryRemove(ctx.Channel.Id, out t)) return;
t.Change(Timeout.Infinite, Timeout.Infinite); //proper way to disable the timer
await ReplyConfirmLocalizedAsync(strs.stopped).ConfigureAwait(false);
await ReplyConfirmLocalizedAsync(strs.stopped);
return;
}
@@ -82,12 +80,12 @@ public class NSFW : NadekoModule<ISearchImagesService>
try
{
if (tags is null || tags.Length == 0)
await InternalDapiCommand(null, true, _service.Hentai).ConfigureAwait(false);
await InternalDapiCommand(null, true, _service.Hentai);
else
{
var groups = tags.Split('|');
var group = groups[_rng.Next(0, groups.Length)];
await InternalDapiCommand(group.Split(' '), true, _service.Hentai).ConfigureAwait(false);
await InternalDapiCommand(group.Split(' '), true, _service.Hentai);
}
}
catch
@@ -120,7 +118,7 @@ public class NSFW : NadekoModule<ISearchImagesService>
if (!_service.AutoBoobTimers.TryRemove(ctx.Channel.Id, out t)) return;
t.Change(Timeout.Infinite, Timeout.Infinite);
await ReplyConfirmLocalizedAsync(strs.stopped).ConfigureAwait(false);
await ReplyConfirmLocalizedAsync(strs.stopped);
return;
}
@@ -131,7 +129,7 @@ public class NSFW : NadekoModule<ISearchImagesService>
{
try
{
await InternalBoobs().ConfigureAwait(false);
await InternalBoobs();
}
catch
{
@@ -160,7 +158,7 @@ public class NSFW : NadekoModule<ISearchImagesService>
if (!_service.AutoButtTimers.TryRemove(ctx.Channel.Id, out t)) return;
t.Change(Timeout.Infinite, Timeout.Infinite); //proper way to disable the timer
await ReplyConfirmLocalizedAsync(strs.stopped).ConfigureAwait(false);
await ReplyConfirmLocalizedAsync(strs.stopped);
return;
}
@@ -171,7 +169,7 @@ public class NSFW : NadekoModule<ISearchImagesService>
{
try
{
await InternalButts(ctx.Channel).ConfigureAwait(false);
await InternalButts(ctx.Channel);
}
catch
{
@@ -206,15 +204,14 @@ public class NSFW : NadekoModule<ISearchImagesService>
_service.Konachan(ctx.Guild?.Id, true, tags),
_service.Gelbooru(ctx.Guild?.Id, true, tags));
var linksEnum = images?.Where(l => l != null).ToArray();
if (images is null || !linksEnum.Any())
var linksEnum = images.Where(l => l != null).ToArray();
if (!linksEnum.Any())
{
await ReplyErrorLocalizedAsync(strs.no_results).ConfigureAwait(false);
await ReplyErrorLocalizedAsync(strs.no_results);
return;
}
await ctx.Channel.SendMessageAsync(string.Join("\n\n", linksEnum.Select(x => x.Url)))
.ConfigureAwait(false);
await ctx.Channel.SendMessageAsync(string.Join("\n\n", linksEnum.Select(x => x.Url)));
}
finally
{
@@ -277,15 +274,14 @@ public class NSFW : NadekoModule<ISearchImagesService>
using (var http = _httpFactory.CreateClient())
{
obj = JArray.Parse(await http
.GetStringAsync($"http://api.oboobs.ru/boobs/{new NadekoRandom().Next(0, 12000)}")
.ConfigureAwait(false))[0];
.GetStringAsync($"http://api.oboobs.ru/boobs/{new NadekoRandom().Next(0, 12000)}"))[0];
}
await ctx.Channel.SendMessageAsync($"http://media.oboobs.ru/{obj["preview"]}").ConfigureAwait(false);
await ctx.Channel.SendMessageAsync($"http://media.oboobs.ru/{obj["preview"]}");
}
catch (Exception ex)
{
await SendErrorAsync(ex.Message).ConfigureAwait(false);
await SendErrorAsync(ex.Message);
}
}
@@ -299,15 +295,14 @@ public class NSFW : NadekoModule<ISearchImagesService>
using (var http = _httpFactory.CreateClient())
{
obj = JArray.Parse(await http
.GetStringAsync($"http://api.obutts.ru/butts/{new NadekoRandom().Next(0, 6100)}")
.ConfigureAwait(false))[0];
.GetStringAsync($"http://api.obutts.ru/butts/{new NadekoRandom().Next(0, 6100)}"))[0];
}
await ctx.Channel.SendMessageAsync($"http://media.obutts.ru/{obj["preview"]}").ConfigureAwait(false);
await ctx.Channel.SendMessageAsync($"http://media.obutts.ru/{obj["preview"]}");
}
catch (Exception ex)
{
await SendErrorAsync(ex.Message).ConfigureAwait(false);
await SendErrorAsync(ex.Message);
}
}
@@ -322,7 +317,7 @@ public class NSFW : NadekoModule<ISearchImagesService>
await SendConfirmAsync(GetText(strs.blacklisted_tag_list),
blTags.Any()
? string.Join(", ", blTags)
: "-").ConfigureAwait(false);
: "-");
}
else
{

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using Microsoft.Extensions.Caching.Memory;
namespace NadekoBot.Modules.Nsfw.Common;
@@ -48,7 +48,7 @@ public class SearchImageCacher : INService
/// <returns>Whether any image is found.</returns>
private async Task<bool> UpdateImagesInternalAsync(string[] tags, bool forceExplicit, Booru type, CancellationToken cancel)
{
var images = await DownloadImagesAsync(tags, forceExplicit, type, cancel).ConfigureAwait(false);
var images = await DownloadImagesAsync(tags, forceExplicit, type, cancel);
if (images is null || images.Count == 0)
{
// Log.Warning("Got no images for {0}, tags: {1}", type, string.Join(", ", tags));
@@ -214,7 +214,7 @@ public class SearchImageCacher : INService
try
{
// if image is not found, update the cache and query again
success = await UpdateImagesInternalAsync(tags, forceExplicit, type, cancel).ConfigureAwait(false);
success = await UpdateImagesInternalAsync(tags, forceExplicit, type, cancel);
}
catch (HttpRequestException)
{
@@ -253,7 +253,7 @@ public class SearchImageCacher : INService
page = _rng.Next(0, 11);
}
var result = await DownloadImagesAsync(tags, isExplicit, type, page, cancel).ConfigureAwait(false);
var result = await DownloadImagesAsync(tags, isExplicit, type, page, cancel);
if (result is null or { Count: 0 })
{

View File

@@ -92,8 +92,7 @@ public class SearchImagesService : ISearchImagesService, INService
tags[i] = "lesbian";
}
var result = await _cache.GetImageNew(tags, forceExplicit, dapi, blTags ?? new HashSet<string>(), cancel)
.ConfigureAwait(false);
var result = await _cache.GetImageNew(tags, forceExplicit, dapi, blTags ?? new HashSet<string>(), cancel);
if (result is null)
{
@@ -200,7 +199,7 @@ public class SearchImagesService : ISearchImagesService, INService
try
{
JToken obj;
obj = JArray.Parse(await _http.GetStringAsync($"http://api.oboobs.ru/boobs/{_rng.Next(0, 12000)}").ConfigureAwait(false))[0];
obj = JArray.Parse(await _http.GetStringAsync($"http://api.oboobs.ru/boobs/{_rng.Next(0, 12000)}"))[0];
return new()
{
Error = "",