diff --git a/NHentaiAPI/Models/Books/Page.cs b/NHentaiAPI/Models/Books/Page.cs new file mode 100644 index 0000000..7c17a80 --- /dev/null +++ b/NHentaiAPI/Models/Books/Page.cs @@ -0,0 +1,13 @@ +using Newtonsoft.Json; + +namespace NHentaiAPI.Models.Books +{ + public class Page + { + [JsonProperty("t")] public string T { get; set; } + + [JsonProperty("w")] public int W { get; set; } + + [JsonProperty("h")] public int H { get; set; } + } +} \ No newline at end of file diff --git a/NHentaiAPI/NHentaiAPI.csproj b/NHentaiAPI/NHentaiAPI.csproj index 821d5d9..ff977c3 100644 --- a/NHentaiAPI/NHentaiAPI.csproj +++ b/NHentaiAPI/NHentaiAPI.csproj @@ -1,22 +1,22 @@  - netstandard2.0;net45;net461 + netstandard2.0;net45;net461;net6.0 true - andy840119 + Sylveon76, andy840119 nHentai A (full?) nHentai API implementation for .NET - https://github.com/andy840119/NHentaiAPI - https://github.com/andy840119/NHentaiAPI.git + https://github.com/Sylveon76/NHentaiAPI + https://github.com/Sylveon76/NHentaiAPI.git git - nhentai n-hentai hentai api csharp andy840119 + nhentai n-hentai hentai api csharp Sylveon76, andy840119 - [1.4.0] -Add get url method. + [1.5.0] +Refactor api client - https://github.com/andy840119/NHentaiAPI - 1.4.0 + https://github.com/Sylveon76/NHentaiAPI/LICENSE + 1.5.0 diff --git a/NHentaiAPI/NHentaiClient.cs b/NHentaiAPI/NHentaiClient.cs index 42644d2..e8121aa 100644 --- a/NHentaiAPI/NHentaiClient.cs +++ b/NHentaiAPI/NHentaiClient.cs @@ -23,113 +23,113 @@ public class NHentaiClient : IDisposable #region Urls - protected virtual string ApiRootUrl => "https://nhentai.net"; + private static string ApiRootUrl => "https://nhentai.net"; - protected virtual string ImageRootUrl => "https://i.nhentai.net"; + private static string ImageRootUrl => "https://i.nhentai.net"; - protected virtual string ThumbnailRootUrl => "https://t.nhentai.net"; + private static string ThumbnailRootUrl => "https://t.nhentai.net"; #endregion #region Data urls - protected virtual string GetHomePageUrl(int pageNum) + private static string GetHomePageUrl(int pageNum) => $"{ApiRootUrl}/api/galleries/all?page={pageNum}"; - protected virtual string GetSearchUrl(string content,int pageNum) + private static string GetSearchUrl(string content, int pageNum) => $"{ApiRootUrl}/api/galleries/search?" + $"query={content.Replace(" ", "+")}&" + $"page={pageNum}"; - protected virtual string GetTagUrl(Tag tag, bool isPopularList, int pageNum) + private static string GetTagUrl(Tag tag, bool isPopularList, int pageNum) => $"{ApiRootUrl}/api/galleries/tagged?" + $"tag_id={tag.Id}" + $"&page={pageNum}" + (isPopularList ? "&sort=popular" : ""); - protected virtual string GetBookDetailsUrl(int bookId) + private static string GetBookDetailsUrl(int bookId) => $"{ApiRootUrl}/api/gallery/{bookId}"; - protected virtual string GetBookRecommendUrl(int bookId) + private static string GetBookRecommendUrl(int bookId) => $"{ApiRootUrl}/api/gallery/{bookId}/related"; - protected virtual string GetGalleryUrl(int galleryId) + private static string GetGalleryUrl(int galleryId) => $"{ImageRootUrl}/galleries/{galleryId}"; - protected virtual string GetThumbGalleryUrl(int galleryId) + private static string GetThumbGalleryUrl(int galleryId) => $"{ThumbnailRootUrl}/galleries/{galleryId}"; #endregion #region Picture urls - public virtual string GetPictureUrl(Book book, int pageNum) + public string GetPictureUrl(Book book, int pageNum) { - var image = GetImage(book, pageNum); + var image = GetImage(book, pageNum-1); var fileType = ConvertType(image.Type); - return GetPictureUrl(book.MediaId, pageNum, fileType); + return GetPictureUrl(book.MediaId, pageNum, fileType); } - public virtual string GetThumbPictureUrl(Book book, int pageNum) + public string GetThumbPictureUrl(Book book, int pageNum) { var image = GetImage(book, pageNum); var fileType = ConvertType(image.Type); return GetThumbPictureUrl(book.MediaId, pageNum, fileType); } - public virtual string GetBigCoverUrl(Book book) + public string GetBigCoverUrl(Book book) => GetBigCoverUrl(book.MediaId); - public virtual string GetOriginPictureUrl(Book book, int pageNum) + public string GetOriginPictureUrl(Book book, int pageNum) => GetOriginPictureUrl(book.MediaId, pageNum); - public virtual string GetBookThumbUrl(Book book) + public string GetBookThumbUrl(Book book) { var fileType = ConvertType(book.Images.Cover.Type); - return GetBookThumbUrl(book.MediaId, fileType); + return GetBookThumbUrl(book.MediaId, fileType); } - protected virtual string GetPictureUrl(int galleryId , int pageNum ,string fileType) + private static string GetPictureUrl(int galleryId, int pageNum, string fileType) => $"{GetGalleryUrl(galleryId)}/{pageNum}.{fileType}"; - protected virtual string GetThumbPictureUrl(int galleryId , int pageNum ,string fileType) + private static string GetThumbPictureUrl(int galleryId, int pageNum, string fileType) => $"{GetThumbGalleryUrl(galleryId)}/{pageNum}t.{fileType}"; - protected virtual string GetBigCoverUrl(int galleryId) + private static string GetBigCoverUrl(int galleryId) => $"{GetThumbGalleryUrl(galleryId)}/cover.jpg"; - protected virtual string GetOriginPictureUrl(int galleryId , int pageNum) + private static string GetOriginPictureUrl(int galleryId, int pageNum) => GetPictureUrl(galleryId, pageNum, "jpg"); - protected virtual string GetBookThumbUrl(int galleryId ,string fileType = "jpg") + private static string GetBookThumbUrl(int galleryId, string fileType = "jpg") => $"{GetThumbGalleryUrl(galleryId)}/thumb.{fileType ?? "jpg"}"; #endregion #region Utilities - protected virtual async Task GetData(string rootUrl) + private async Task GetData(string rootUrl) { var json = await _client.GetStringAsync(rootUrl); return JsonConvert.DeserializeObject(json); } - protected virtual async Task GetByteData(string rootUrl) + private async Task GetByteData(string rootUrl) { var data = await _client.GetByteArrayAsync(rootUrl); return data; } - protected virtual Image GetImage(Book book, int pageNum) + private static Image GetImage(Book book, int pageNum) { if (book == null) throw new ArgumentNullException(nameof(book)); - - var page = book.Images.Pages[pageNum - 1]; + + var page = book.Images.Pages[pageNum]; return page; } - protected virtual string ConvertType(ImageType type) + private static string ConvertType(ImageType type) { switch (type) { @@ -148,19 +148,19 @@ protected virtual string ConvertType(ImageType type) #region Search - public virtual Task GetHomePageListAsync(int pageNum) - { + public Task GetHomePageListAsync(int pageNum) + { var url = GetHomePageUrl(pageNum); return GetData(url); } - public virtual Task GetSearchPageListAsync(string keyword,int pageNum) - { + public Task GetSearchPageListAsync(string keyword, int pageNum) + { var url = GetSearchUrl(keyword, pageNum); return GetData(url); } - public virtual Task GetTagPageListAsync(Tag tag, SortBy sortBy, int pageNum) + public Task GetTagPageListAsync(Tag tag, SortBy sortBy, int pageNum) { var url = GetTagUrl(tag, sortBy == SortBy.Popular, pageNum); return GetData(url); @@ -170,13 +170,13 @@ public virtual Task GetTagPageListAsync(Tag tag, SortBy sortBy, i #region Books - public virtual Task GetBookAsync(int bookId) + public async Task GetBookAsync(int bookId) { var url = GetBookDetailsUrl(bookId); - return GetData(url); + return await GetData(url); } - public virtual async Task GetBookRecommendAsync(int bookId) + public async Task GetBookRecommendAsync(int bookId) { var url = GetBookRecommendUrl(bookId); var book = await GetData(url); @@ -190,31 +190,31 @@ public virtual async Task GetBookRecommendAsync(int bookId) #region Picture - public virtual Task GetPictureAsync(Book book, int pageNum) + public Task GetPictureAsync(Book book, int pageNum) { var url = GetPictureUrl(book, pageNum); return GetByteData(url); } - public virtual Task GetThumbPictureAsync(Book book, int pageNum) + public Task GetThumbPictureAsync(Book book, int pageNum) { var url = GetThumbPictureUrl(book, pageNum); return GetByteData(url); } - public virtual Task GetBigCoverPictureAsync(Book book) + public Task GetBigCoverPictureAsync(Book book) { var url = GetBigCoverUrl(book.MediaId); return GetByteData(url); } - public virtual Task GetOriginPictureAsync(Book book, int pageNum) + public Task GetOriginPictureAsync(Book book, int pageNum) { var url = GetOriginPictureUrl(book.MediaId, pageNum); return GetByteData(url); } - public virtual Task GetBookThumbPictureAsync(Book book) + public Task GetBookThumbPictureAsync(Book book) { var url = GetBookThumbUrl(book); return GetByteData(url); @@ -227,4 +227,4 @@ public void Dispose() _client.Dispose(); } } -} +} \ No newline at end of file diff --git a/README.md b/README.md index d0c9ed3..cbda414 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # HentaiAPI [![Build status](https://ci.appveyor.com/api/projects/status/9u2xoxn47irix7gp?svg=true)](https://ci.appveyor.com/project/andy840119/nhentaiapi) -[![CodeFactor](https://www.codefactor.io/repository/github/andy840119/nhentaiapi/badge)](https://www.codefactor.io/repository/github/andy840119/nhentaiapi) [![NuGet](https://img.shields.io/nuget/v/NHentaiAPI.svg)](https://www.nuget.org/packages/NHentaiAPI) [![NuGet](https://img.shields.io/nuget/dt/NHentaiAPI.svg)](https://www.nuget.org/packages/NHentaiAPI) [![NuGet](https://img.shields.io/badge/月子我婆-passed-ff69b4.svg)](https://github.com/andy840119/NHentaiAPI) @@ -20,6 +19,8 @@ Search: 3. Search result by `tag`, can be sort by popular +4. Search tags can be filtered by putting `-` in front of them + Book detail: 1. Book detail @@ -40,7 +41,7 @@ Search book: var client = new NHentaiClient(); //https://nhentai.net/api/galleries/search?query=school%20swimsuit%20loli%20full%20color&page=2 -var result = await client.GetSearchPageListAsync("school swimsuit loli full color",2); +var result = await client.GetSearchPageListAsync("school swimsuit full color -loli",2); ``` Get book detail: