|
@@ -5,6 +5,8 @@ using System.Threading.Tasks;
|
|
|
using System.Text.Json;
|
|
|
using VeloeAvaloniaKemonoPartyApp.Models;
|
|
|
using VeloeAvaloniaKemonoPartyApp.Services;
|
|
|
+using System.Linq;
|
|
|
+using Splat;
|
|
|
|
|
|
namespace VeloeKemonoPartyApp.Services
|
|
|
{
|
|
@@ -12,7 +14,10 @@ namespace VeloeKemonoPartyApp.Services
|
|
|
{
|
|
|
public HttpClient httpClient;
|
|
|
|
|
|
- public List<Post> posts;
|
|
|
+ private string _postsIds = string.Empty;
|
|
|
+ private Queue<Post> _posts;
|
|
|
+ private HashSet<string> _ids;
|
|
|
+ private const int _postsPerLoad = 10;
|
|
|
public KemonoHttpClient()
|
|
|
{
|
|
|
httpClient = new HttpClient()
|
|
@@ -20,23 +25,25 @@ namespace VeloeKemonoPartyApp.Services
|
|
|
BaseAddress = new Uri("https://kemono.su"),
|
|
|
|
|
|
};
|
|
|
- posts = new List<Post>();
|
|
|
+ _posts = new Queue<Post>(50);
|
|
|
+ _ids = new HashSet<string>();
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
+ if (System.IO.File.Exists(await Locator.Current.GetService<IStorageService>().GetCacheFolderAsync() + "/creators.json") && new System.IO.FileInfo(await Locator.Current.GetService<IStorageService>().GetCacheFolderAsync() + "/creators.json").CreationTime > DateTime.Now.AddDays(-7) && !reloadSource)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- using (var stream = System.IO.File.OpenRead(await RegisteredServices.StorageService.GetCacheFolderAsync() + "/creators.json"))
|
|
|
+ using (var stream = System.IO.File.OpenRead(await Locator.Current.GetService<IStorageService>().GetCacheFolderAsync() + "/creators.json"))
|
|
|
{
|
|
|
return await JsonSerializer.DeserializeAsync<List<Creator>>(stream);
|
|
|
}
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
- System.IO.File.Delete(await RegisteredServices.StorageService.GetCacheFolderAsync() + "/creators.json");
|
|
|
+ System.IO.File.Delete(await Locator.Current.GetService<IStorageService>().GetCacheFolderAsync() + "/creators.json");
|
|
|
+ throw;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -45,12 +52,13 @@ namespace VeloeKemonoPartyApp.Services
|
|
|
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"))
|
|
|
+ var result = await JsonSerializer.DeserializeAsync<List<Creator>>(await response.Content.ReadAsStreamAsync());
|
|
|
+
|
|
|
+ using (var stream = System.IO.File.Create(await Locator.Current.GetService<IStorageService>().GetCacheFolderAsync() + "/creators.json"))
|
|
|
{
|
|
|
stream.Write(await response.Content.ReadAsByteArrayAsync());
|
|
|
}
|
|
|
-
|
|
|
- return await JsonSerializer.DeserializeAsync<List<Creator>>(await response.Content.ReadAsStreamAsync());
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception)
|
|
@@ -59,25 +67,67 @@ namespace VeloeKemonoPartyApp.Services
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public async Task<List<Post>> GetPostsList(string id, string service, int start, string search = "")
|
|
|
+ public async Task<IEnumerable<Post>> GetPostsList(string id, string service, int start, string search = "")
|
|
|
{
|
|
|
- try
|
|
|
+ if (_posts.Count > 0 && _postsIds.Equals(service+id, StringComparison.Ordinal))
|
|
|
{
|
|
|
- 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)
|
|
|
+ var result = new List<Post>();
|
|
|
+ for (int i = 0; i < _postsPerLoad && _posts.Count > 0; i++)
|
|
|
{
|
|
|
- if (response.IsSuccessStatusCode)
|
|
|
- {
|
|
|
- string jsonString = await response.Content.ReadAsStringAsync();
|
|
|
- // ... Read the string.
|
|
|
- return JsonSerializer.Deserialize<List<Post>>(jsonString);
|
|
|
- }
|
|
|
- return new List<Post>();
|
|
|
+ var post = _posts.Dequeue();
|
|
|
+ _ids.Remove(post.Id);
|
|
|
+ result.Add(post);
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
- catch (Exception)
|
|
|
+ else
|
|
|
{
|
|
|
- throw;
|
|
|
+ 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.
|
|
|
+ var resultList = JsonSerializer.Deserialize<List<Post>>(jsonString);
|
|
|
+
|
|
|
+ var i = 0;
|
|
|
+
|
|
|
+ if (!_postsIds.Equals(service + id, StringComparison.Ordinal))
|
|
|
+ {
|
|
|
+ _posts.Clear();
|
|
|
+ _ids.Clear();
|
|
|
+ _postsIds = service + id;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (var post in resultList)
|
|
|
+ {
|
|
|
+ foreach (var item in post.Attachments)
|
|
|
+ {
|
|
|
+ item.Service = post.Service;
|
|
|
+ item.User = post.User;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (i >= _postsPerLoad && !_ids.Contains(post.Id))
|
|
|
+ {
|
|
|
+ _ids.Add(post.Id);
|
|
|
+ _posts.Enqueue(post);
|
|
|
+ }
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ return resultList.Take(_postsPerLoad);
|
|
|
+ }
|
|
|
+ return Enumerable.Empty<Post>();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ throw;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|