using System.Net.Http.Headers; using System.Text; using BotFramework.Utils; using Microsoft.Extensions.Configuration; using Telegram.Bot.Types; using West.TelegramBot.ContentStore.Model; using West.TelegramBot.ContentStore.Option; namespace West.TelegramBot.ContentStore.Service; public abstract class AbstractKittiesService : IRandomMediaService { private readonly AllowNsfw _allowNsfw; protected abstract string VideoType { get; } public abstract StoreItem StoreItem { get; } private readonly HttpClient _httpClient; protected AbstractKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) { var section = configuration.GetSection("KittiesService"); var uri = section.GetValue("Uri"); var token = section.GetValue("Token"); _httpClient = new HttpClient { BaseAddress = uri, DefaultRequestHeaders = { Authorization = new AuthenticationHeaderValue("Bearer", token) } }; _allowNsfw = allowNsfw; } public async Task GetRandomMediaAsync() { var response = await _httpClient.GetAsync($"v1/{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( new InputFile(await response.Content.ReadAsStreamAsync()), new HtmlString().Url(videoSource, videoTitle).ToString(), true ); } public async Task CanView(Chat chat, User user) { return await _allowNsfw.GetValueAsync(chat.Id); } }