2021-08-04 21:51:31 +08:00

370 lines
14 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Gallery.Illust;
namespace Gallery.Utils
{
public class IllustRankingData
{
[JsonInclude]
public Content[] contents;
[JsonInclude]
public string mode;
[JsonInclude]
public string content;
[JsonInclude]
public int page;
[JsonInclude]
[JsonConverter(typeof(Int32Converter))]
public int? prev;
[JsonInclude]
[JsonConverter(typeof(Int32Converter))]
public int? next;
[JsonInclude]
public string date;
[JsonInclude]
[JsonConverter(typeof(StringConverter))]
public string prev_date;
[JsonInclude]
[JsonConverter(typeof(StringConverter))]
public string next_date;
[JsonInclude]
public int rank_total;
public class Content
{
[JsonInclude]
public string title;
[JsonInclude]
public string date;
[JsonInclude]
public string[] tags;
[JsonInclude]
public string url;
[JsonInclude]
public string illust_type;
[JsonInclude]
public string illust_book_style;
[JsonInclude]
public string illust_page_count;
[JsonInclude]
public string user_name;
[JsonInclude]
public string profile_img;
[JsonInclude]
public ContentType illust_content_type;
[JsonInclude]
public object illust_series; // bool, Series
[JsonInclude]
public long illust_id;
[JsonInclude]
public int width;
[JsonInclude]
public int height;
[JsonInclude]
public long user_id;
[JsonInclude]
public int rank;
[JsonInclude]
public int yes_rank;
[JsonInclude]
public int rating_count;
[JsonInclude]
public int view_count;
[JsonInclude]
public long illust_upload_timestamp;
[JsonInclude]
public string attr;
[JsonInclude]
public bool is_bookmarked;
[JsonInclude]
public bool bookmarkable;
[JsonInclude]
public string bookmark_id;
[JsonInclude]
public string bookmark_illust_restrict;
public class ContentType
{
[JsonInclude]
public int sexual;
[JsonInclude]
public bool lo;
[JsonInclude]
public bool grotesque;
[JsonInclude]
public bool violent;
[JsonInclude]
public bool homosexual;
[JsonInclude]
public bool drug;
[JsonInclude]
public bool thoughts;
[JsonInclude]
public bool antisocial;
[JsonInclude]
public bool religion;
[JsonInclude]
public bool original;
[JsonInclude]
public bool furry;
[JsonInclude]
public bool bl;
[JsonInclude]
public bool yuri;
}
public class Series
{
[JsonInclude]
public string illust_series_caption;
[JsonInclude]
public string illust_series_content_count;
[JsonInclude]
public string illust_series_content_illust_id;
[JsonInclude]
public string illust_series_content_order;
[JsonInclude]
public string illust_series_create_datetime;
[JsonInclude]
public string illust_series_id;
[JsonInclude]
public string illust_series_title;
[JsonInclude]
public string illust_series_user_id;
[JsonInclude]
public string page_url;
}
public IllustItem ConvertToItem()
{
if (!int.TryParse(illust_page_count, out int count))
{
count = 1;
}
if (!int.TryParse(illust_type, out int type))
{
type = 0;
}
bool restrict;
if (tags != null && tags.Contains("R-18"))
{
restrict = true;
}
else
{
restrict = false;
}
return new IllustItem
{
Id = illust_id.ToString(),
BookmarkId = bookmark_id,
Title = title,
Rank = rank,
IllustType = (IllustType)type,
ImageUrl = url,
IsRestrict = restrict,
Tags = tags ?? new string[0],
ProfileUrl = profile_img,
UserId = user_id.ToString(),
UserName = user_name,
Width = width,
Height = height,
PageCount = count,
YesRank = yes_rank,
RatingCount = rating_count,
ViewCount = view_count,
UploadTimestamp = illust_upload_timestamp
};
}
}
}
public class IllustGlobalData
{
[JsonInclude]
public string token;
[JsonInclude]
public string oneSignalAppId;
[JsonInclude]
public UserData userData;
public class UserData
{
[JsonInclude]
public string id;
[JsonInclude]
public string pixivId;
[JsonInclude]
public string name;
[JsonInclude]
public string profileImg;
[JsonInclude]
public string profileImgBig;
[JsonInclude]
public bool premium;
[JsonInclude]
public int xRestrict;
[JsonInclude]
public bool adult;
[JsonInclude]
public bool safeMode;
}
}
public class Int32Converter : JsonConverter<int?>
{
public override int? Read(ref Utf8JsonReader reader, System.Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt32();
}
return null;
}
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
{
throw new System.NotImplementedException();
}
}
public class StringConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, System.Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString();
}
else if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt64().ToString();
}
return null;
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
throw new System.NotImplementedException();
}
}
public class FavoriteListConverter : JsonConverter<FavoriteList>
{
public override FavoriteList Read(ref Utf8JsonReader reader, System.Type typeToConvert, JsonSerializerOptions options)
{
var list = new FavoriteList();
if (reader.TokenType != JsonTokenType.StartArray)
{
return list;
}
var item = new IllustItem();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndArray)
{
return list;
}
if (reader.TokenType == JsonTokenType.EndObject)
{
list.Add(item);
item = new IllustItem();
}
else if (reader.TokenType == JsonTokenType.PropertyName)
{
var name = reader.GetString();
reader.Read();
switch (name)
{
case nameof(IllustItem.Title): item.Title = reader.GetString(); break;
case nameof(IllustItem.Id): item.Id = reader.GetString(); break;
case nameof(IllustItem.BookmarkId): item.BookmarkId = reader.GetString(); break;
case nameof(IllustItem.FavoriteDateUtc): item.FavoriteDateUtc = reader.GetDateTime(); break;
case nameof(IllustItem.ImageUrl): item.ImageUrl = reader.GetString(); break;
case nameof(IllustItem.IsRestrict): item.IsRestrict = reader.GetBoolean(); break;
case nameof(IllustItem.Tags):
if (reader.TokenType == JsonTokenType.StartArray)
{
var tags = new List<string>();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndArray)
{
break;
}
if (reader.TokenType == JsonTokenType.String)
{
tags.Add(reader.GetString());
}
}
item.Tags = tags.ToArray();
}
break;
case nameof(IllustItem.ProfileUrl): item.ProfileUrl = reader.GetString(); break;
case nameof(IllustItem.UserId): item.UserId = reader.GetString(); break;
case nameof(IllustItem.UserName): item.UserName = reader.GetString(); break;
case nameof(IllustItem.Width): item.Width = reader.GetInt32(); break;
case nameof(IllustItem.Height): item.Height = reader.GetInt32(); break;
case nameof(IllustItem.PageCount): item.PageCount = reader.GetInt32(); break;
case nameof(IllustItem.ImageHeightValue): item.ImageHeightValue = reader.GetDouble(); break;
case nameof(IllustItem.YesRank): item.YesRank = reader.GetInt32(); break;
case nameof(IllustItem.RatingCount): item.RatingCount = reader.GetInt32(); break;
case nameof(IllustItem.ViewCount): item.ViewCount = reader.GetInt32(); break;
case nameof(IllustItem.UploadTimestamp): item.UploadTimestamp = reader.GetInt64(); break;
case nameof(IllustItem.IllustType): item.IllustType = (IllustType)reader.GetInt32(); break;
}
}
}
throw new JsonException("unexpected favorite list ending");
}
public override void Write(Utf8JsonWriter writer, FavoriteList value, JsonSerializerOptions options)
{
writer.WriteStartArray();
if (value != null)
{
for (var i = 0; i < value.Count; i++)
{
var item = value[i];
writer.WriteStartObject();
writer.WriteString(nameof(IllustItem.Title), item.Title);
writer.WriteString(nameof(IllustItem.Id), item.Id);
writer.WriteString(nameof(IllustItem.BookmarkId), item.BookmarkId);
writer.WriteString(nameof(IllustItem.FavoriteDateUtc), item.FavoriteDateUtc);
writer.WriteString(nameof(IllustItem.ImageUrl), item.ImageUrl);
writer.WriteBoolean(nameof(IllustItem.IsRestrict), item.IsRestrict);
if (item.Tags != null)
{
writer.WritePropertyName(nameof(IllustItem.Tags));
writer.WriteStartArray();
for (var n = 0; n < item.Tags.Length; n++)
{
writer.WriteStringValue(item.Tags[n]);
}
writer.WriteEndArray();
}
writer.WriteString(nameof(IllustItem.ProfileUrl), item.ProfileUrl);
writer.WriteString(nameof(IllustItem.UserId), item.UserId);
writer.WriteString(nameof(IllustItem.UserName), item.UserName);
writer.WriteNumber(nameof(IllustItem.Width), item.Width);
writer.WriteNumber(nameof(IllustItem.Height), item.Height);
writer.WriteNumber(nameof(IllustItem.PageCount), item.PageCount);
writer.WriteNumber(nameof(IllustItem.ImageHeightValue), item.ImageHeightValue);
writer.WriteNumber(nameof(IllustItem.YesRank), item.YesRank);
writer.WriteNumber(nameof(IllustItem.RatingCount), item.RatingCount);
writer.WriteNumber(nameof(IllustItem.ViewCount), item.ViewCount);
writer.WriteNumber(nameof(IllustItem.UploadTimestamp), item.UploadTimestamp);
writer.WriteNumber(nameof(IllustItem.IllustType), (int)item.IllustType);
writer.WriteEndObject();
}
}
writer.WriteEndArray();
}
}
}