mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[store] kitties now have own http client factory
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
using West.TelegramBot.ContentStore.Model;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
|
public interface IKittiesApi
|
||||||
|
{
|
||||||
|
public Task<KittiesResponse> GetKittiesImageAsync(string type);
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
using System.Text;
|
||||||
|
using West.TelegramBot.ContentStore.Model;
|
||||||
|
|
||||||
|
namespace West.TelegramBot.ContentStore.API;
|
||||||
|
|
||||||
|
public class KittiesApi : IKittiesApi
|
||||||
|
{
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
|
public KittiesApi(HttpClient client)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<KittiesResponse> GetKittiesImageAsync(string type)
|
||||||
|
{
|
||||||
|
var httpResponse = await _client.GetAsync($"v1/{type}");
|
||||||
|
var headers = httpResponse.Headers;
|
||||||
|
var titleBytes =
|
||||||
|
Convert.FromBase64String(headers.GetValues("X-Video-Title").First());
|
||||||
|
|
||||||
|
return new KittiesResponse(
|
||||||
|
await httpResponse.Content.ReadAsStreamAsync(),
|
||||||
|
headers.GetValues("X-Video-Source").First(),
|
||||||
|
Encoding.UTF8.GetString(titleBytes)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
using Kruzya.TelegramBot.Core;
|
using Kruzya.TelegramBot.Core;
|
||||||
using Kruzya.TelegramBot.Core.Extensions;
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
using Kruzya.TelegramBot.Core.Options;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
using West.TelegramBot.ContentStore.Option;
|
using West.TelegramBot.ContentStore.Option;
|
||||||
using West.TelegramBot.ContentStore.Service;
|
using West.TelegramBot.ContentStore.Service;
|
||||||
|
|
||||||
@@ -27,6 +29,17 @@ public class ContentStore : Module
|
|||||||
{
|
{
|
||||||
services.AddOption<AllowNsfw>();
|
services.AddOption<AllowNsfw>();
|
||||||
|
|
||||||
|
var kittiesConfig = Configuration.GetSection("KittiesService");
|
||||||
|
services.AddHttpClient<IKittiesApi, KittiesApi>(c =>
|
||||||
|
{
|
||||||
|
c.BaseAddress = kittiesConfig.GetValue<Uri>("Uri");
|
||||||
|
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
|
||||||
|
"Bearer",
|
||||||
|
kittiesConfig.GetValue<string>("token")
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
foreach (var serviceType in MediaServiceTypes)
|
foreach (var serviceType in MediaServiceTypes)
|
||||||
{
|
{
|
||||||
services.AddScoped(typeof(IRandomMediaService), serviceType);
|
services.AddScoped(typeof(IRandomMediaService), serviceType);
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
namespace West.TelegramBot.ContentStore.Model;
|
||||||
|
|
||||||
|
public class KittiesResponse
|
||||||
|
{
|
||||||
|
public Stream Content { get; set; }
|
||||||
|
public string Source { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
|
||||||
|
public KittiesResponse(Stream content, string source, string title)
|
||||||
|
{
|
||||||
|
Content = content;
|
||||||
|
Source = source;
|
||||||
|
Title = title;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,5 +15,6 @@ public abstract class AbstractNsfwService : IRandomMediaService
|
|||||||
_allowNsfw = allowNsfw;
|
_allowNsfw = allowNsfw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> CanView(Chat chat, User user) => await _allowNsfw.GetValueAsync(chat.Id);
|
public async Task<bool> CanView(Chat chat, User user)
|
||||||
|
=> await _allowNsfw.GetValueAsync(chat.Id);
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
using System.Net.Http.Headers;
|
using BotFramework.Utils;
|
||||||
using System.Text;
|
|
||||||
using BotFramework.Utils;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
using West.TelegramBot.ContentStore.Model;
|
using West.TelegramBot.ContentStore.Model;
|
||||||
using West.TelegramBot.ContentStore.Option;
|
using West.TelegramBot.ContentStore.Option;
|
||||||
|
|
||||||
@@ -10,41 +8,25 @@ namespace West.TelegramBot.ContentStore.Service.Kitties;
|
|||||||
|
|
||||||
public abstract class AbstractKittiesService : IRandomMediaService
|
public abstract class AbstractKittiesService : IRandomMediaService
|
||||||
{
|
{
|
||||||
|
private readonly IKittiesApi _api;
|
||||||
private readonly AllowNsfw _allowNsfw;
|
private readonly AllowNsfw _allowNsfw;
|
||||||
protected abstract string VideoType { get; }
|
protected abstract string VideoType { get; }
|
||||||
|
|
||||||
public abstract StoreItem StoreItem { get; }
|
public abstract StoreItem StoreItem { get; }
|
||||||
private readonly HttpClient _httpClient;
|
|
||||||
|
|
||||||
protected AbstractKittiesService(IConfiguration configuration, AllowNsfw allowNsfw)
|
protected AbstractKittiesService(IKittiesApi api, AllowNsfw allowNsfw)
|
||||||
{
|
{
|
||||||
var section = configuration.GetSection("KittiesService");
|
_api = api;
|
||||||
|
|
||||||
var uri = section.GetValue<Uri>("Uri");
|
|
||||||
var token = section.GetValue<string>("Token");
|
|
||||||
|
|
||||||
_httpClient = new HttpClient
|
|
||||||
{
|
|
||||||
BaseAddress = uri,
|
|
||||||
DefaultRequestHeaders =
|
|
||||||
{
|
|
||||||
Authorization = new AuthenticationHeaderValue("Bearer", token)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
_allowNsfw = allowNsfw;
|
_allowNsfw = allowNsfw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<RandomMedia> GetRandomMediaAsync()
|
public async Task<RandomMedia> GetRandomMediaAsync()
|
||||||
{
|
{
|
||||||
var response = await _httpClient.GetAsync($"v1/{VideoType}");
|
var response = await _api.GetKittiesImageAsync(VideoType);
|
||||||
var headers = response.Headers;
|
|
||||||
var videoSource = headers.GetValues("X-Video-Source").FirstOrDefault(string.Empty);
|
|
||||||
var videoTitleBase64 = headers.GetValues("X-Video-Title").FirstOrDefault(string.Empty);
|
|
||||||
var videoTitle = Encoding.UTF8.GetString(Convert.FromBase64String(videoTitleBase64));
|
|
||||||
|
|
||||||
return new RandomMedia(
|
return new RandomMedia(
|
||||||
new InputFile(await response.Content.ReadAsStreamAsync()),
|
new InputFile(response.Content),
|
||||||
new HtmlString().Url(videoSource, videoTitle).ToString(),
|
new HtmlString().Url(response.Source, response.Title).ToString(),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Kruzya.TelegramBot.Core.Data;
|
using Kruzya.TelegramBot.Core.Data;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
using West.TelegramBot.ContentStore.Model;
|
using West.TelegramBot.ContentStore.Model;
|
||||||
using West.TelegramBot.ContentStore.Option;
|
using West.TelegramBot.ContentStore.Option;
|
||||||
|
|
||||||
@@ -7,7 +8,7 @@ namespace West.TelegramBot.ContentStore.Service.Kitties;
|
|||||||
|
|
||||||
public class GayKittiesService : AbstractKittiesService
|
public class GayKittiesService : AbstractKittiesService
|
||||||
{
|
{
|
||||||
public GayKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) : base(configuration, allowNsfw)
|
public GayKittiesService(IKittiesApi api, AllowNsfw allowNsfw) : base(api, allowNsfw)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using West.TelegramBot.ContentStore.API;
|
||||||
using West.TelegramBot.ContentStore.Model;
|
using West.TelegramBot.ContentStore.Model;
|
||||||
using West.TelegramBot.ContentStore.Option;
|
using West.TelegramBot.ContentStore.Option;
|
||||||
|
|
||||||
@@ -9,7 +10,7 @@ public class StraightKittiesService : AbstractKittiesService
|
|||||||
protected override string VideoType => "straight";
|
protected override string VideoType => "straight";
|
||||||
public override StoreItem StoreItem { get; } = new("kitties_straight", "Прон", 40, 100);
|
public override StoreItem StoreItem { get; } = new("kitties_straight", "Прон", 40, 100);
|
||||||
|
|
||||||
public StraightKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) : base(configuration, allowNsfw)
|
public StraightKittiesService(IKittiesApi api, AllowNsfw allowNsfw) : base(api, allowNsfw)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user