mirror of
https://github.com/yawaflua/SPWorldsWrapper.git
synced 2025-12-11 16:16:22 +02:00
Compare commits
4 Commits
v1.0.3
...
yawaflua-p
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9a51cd600 | ||
|
|
85c4d5e099 | ||
|
|
1f81fd5cbd | ||
|
|
1201e16caa |
@@ -1,5 +1,5 @@
|
||||
# SPWorldsWrapper
|
||||

|
||||

|
||||
|
||||
Actions pass:
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PackageId>SPWorldsWrapper</PackageId>
|
||||
|
||||
@@ -2,13 +2,22 @@
|
||||
using SPWorldsWrapper.Types;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace SPWorldsWrapper
|
||||
{
|
||||
public class AuthentificationError : Exception
|
||||
{
|
||||
public AuthentificationError(string? message, Exception? innerException) : base(message, innerException) { }
|
||||
public AuthentificationError() : base() { }
|
||||
public AuthentificationError(string? message) : base(message) { }
|
||||
}
|
||||
public class SPWrapper
|
||||
{
|
||||
public readonly HttpClient client;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Асинхронный wrapper для работы напрямую с сайтом, а не с API SPWorlds.ru
|
||||
@@ -21,7 +30,11 @@ namespace SPWorldsWrapper
|
||||
client = new(handler);
|
||||
client.BaseAddress = new Uri("https://spworlds.ru/api/");
|
||||
cookieContainer.Add(client.BaseAddress, new Cookie("jeff", token));
|
||||
spwLogin();
|
||||
var spwLoginMethod = spwLogin();
|
||||
if (!spwLoginMethod.Result)
|
||||
{
|
||||
throw new AuthentificationError("Ошибка парсинга данных от сайта. Проверьте токен, IP сервера и статус сайта SPWORLDS.RU");
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
@@ -30,11 +43,24 @@ namespace SPWorldsWrapper
|
||||
/// Если иное - токен слетел!
|
||||
/// </summary>
|
||||
/// <returns>Ничего</returns>
|
||||
public void spwLogin()
|
||||
public async Task<bool> spwLogin()
|
||||
{
|
||||
var content = new StringContent(@"{}");
|
||||
var responseMessage = client.PostAsync("auth/refresh_token", content).Result;
|
||||
Console.WriteLine(responseMessage.Content.ReadAsStringAsync().Result);
|
||||
var responseMessage = await client.PostAsync("auth/refresh_token", content);
|
||||
var bodyFromSPW = await responseMessage.Content.ReadAsStringAsync();
|
||||
var serializedBody = JsonNode.Parse(bodyFromSPW);
|
||||
if (serializedBody == null)
|
||||
{
|
||||
Console.Error.WriteLine("error: Some error returned from site.");
|
||||
Console.WriteLine("debug: please, check your authorization token");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(await responseMessage.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
|
||||
return serializedBody != null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение данных пользователя по юзернейму.
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using Microsoft.VisualBasic;
|
||||
using SPWorldsWrapper.Types.UserTypes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -6,45 +8,59 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SPWorldsWrapper.Types
|
||||
{
|
||||
public class CardsOwned
|
||||
{
|
||||
public string name { get; set; }
|
||||
public int color { get; set; }
|
||||
public string number { get; set; }
|
||||
public string id { get; set; }
|
||||
}
|
||||
|
||||
public class City
|
||||
{
|
||||
public Mayor mayor { get; set; }
|
||||
public string name { get; set; }
|
||||
public int x { get; set; }
|
||||
public int z { get; set; }
|
||||
}
|
||||
|
||||
public class Mayor
|
||||
{
|
||||
public string id { get; set; }
|
||||
}
|
||||
|
||||
public class SPUser
|
||||
{
|
||||
public string id { get; set; }
|
||||
public bool isBanned { get; set; }
|
||||
public User user { get; set; }
|
||||
public List<string> roles { get; set; }
|
||||
public string[] roles { get; set; }
|
||||
public City? city { get; set; }
|
||||
public string status { get; set; }
|
||||
public DateTime createdAt { get; set; }
|
||||
public List<CardsOwned> cardsOwned { get; set; }
|
||||
public bool isFollowed { get; set; }
|
||||
public bool isFollowingYou { get; set; }
|
||||
}
|
||||
|
||||
public class User
|
||||
{
|
||||
public bool isAdmin { get; set; }
|
||||
public string minecraftUUID { get; set; }
|
||||
public string username { get; set; }
|
||||
public Dictionary<string, object> toKeyValuePairs()
|
||||
{
|
||||
string cards = "[\n";
|
||||
foreach (var card in cardsOwned)
|
||||
{
|
||||
cards += " {\n";
|
||||
foreach (var kvp in card.toKeyValuePairs())
|
||||
{
|
||||
cards += $" {kvp.Key}: {kvp.Value},\n";
|
||||
}
|
||||
cards += " },\n";
|
||||
}
|
||||
cards += "]";
|
||||
return new ()
|
||||
{
|
||||
{ "id", id },
|
||||
{ "isBanned", isBanned },
|
||||
{ "status", status },
|
||||
{ "created_at", createdAt },
|
||||
{ "isFollowed", isFollowed },
|
||||
{ "isFollowingYou", isFollowingYou },
|
||||
{ "user", user.ToString() },
|
||||
{ "roles", $"[{string.Join(", ", roles)}]" },
|
||||
{ "city", city?.ToString() ?? "Null" },
|
||||
{ "cardsOwner", cards },
|
||||
};
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string stringToReturn = "{\n";
|
||||
foreach (var kvp in toKeyValuePairs())
|
||||
{
|
||||
stringToReturn += $"\n {kvp.Key}: {kvp.Value},";
|
||||
}
|
||||
stringToReturn += "\n}";
|
||||
return stringToReturn;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user