1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Threading.Tasks;
- using System.Text.Json;
- using VeloeAvaloniaKemonoPartyApp.Models;
- using VeloeAvaloniaKemonoPartyApp.Services;
- namespace VeloeKemonoPartyApp.Services
- {
- public class KemonoHttpClient : IDisposable
- {
- public HttpClient httpClient;
- public List<Post> posts;
- public KemonoHttpClient()
- {
- httpClient = new HttpClient()
- {
- BaseAddress = new Uri("https://kemono.su"),
-
- };
- posts = new List<Post>();
- }
- public async Task<List<Creator>> GetCreatorsList(bool reloadSource)
- {
- if (System.IO.File.Exists(await RegisteredServices.StorageService.GetCacheFolderAsync() + "/creators.json") && new System.IO.FileInfo(await RegisteredServices.StorageService.GetCacheFolderAsync() + "/creators.json").CreationTime > DateTime.Now.AddDays(-7) && !reloadSource)
- {
- try
- {
- using (var stream = System.IO.File.OpenRead(await RegisteredServices.StorageService.GetCacheFolderAsync() + "/creators.json"))
- {
- return await JsonSerializer.DeserializeAsync<List<Creator>>(stream);
- }
- }
- catch
- {
- System.IO.File.Delete(await RegisteredServices.StorageService.GetCacheFolderAsync() + "/creators.json");
- }
- }
- try
- {
- using (HttpResponseMessage response = await httpClient.GetAsync("https://kemono.su/api/v1/creators.txt"))
- using (HttpContent content = response.Content)
- {
- using (var stream = System.IO.File.OpenWrite(await RegisteredServices.StorageService.GetCacheFolderAsync() + "/creators.json"))
- {
- stream.Write(await response.Content.ReadAsByteArrayAsync());
- }
- return await JsonSerializer.DeserializeAsync<List<Creator>>(await response.Content.ReadAsStreamAsync());
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- public async Task<List<Post>> GetPostsList(string id, string service, int start, string search = "")
- {
- try
- {
- using (HttpResponseMessage response = await httpClient.GetAsync($"https://kemono.su/api/v1/{service}/user/{id}?o={start}" + (!string.IsNullOrEmpty(search) ? $"&q={search}" : string.Empty)))
- using (HttpContent content = response.Content)
- {
- if (response.IsSuccessStatusCode)
- {
- string jsonString = await response.Content.ReadAsStringAsync();
- // ... Read the string.
- return JsonSerializer.Deserialize<List<Post>>(jsonString);
- }
- return new List<Post>();
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- public void Dispose()
- {
- httpClient?.Dispose();
- }
- }
- }
|