Skip to content

Commit

Permalink
Pages are now gotten properly, tests don't throw collection errors an…
Browse files Browse the repository at this point in the history
…d actually complete
  • Loading branch information
sylveonnotdeko committed May 7, 2022
1 parent 9d1793c commit 4751ef1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 54 deletions.
13 changes: 13 additions & 0 deletions NHentaiAPI/Models/Books/Page.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
18 changes: 9 additions & 9 deletions NHentaiAPI/NHentaiAPI.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net45;net461</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net45;net461;net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>andy840119</Authors>
<Authors>Sylveon76, andy840119</Authors>
<Product>nHentai</Product>
<Description>A (full?) nHentai API implementation for .NET</Description>
<Copyright />
<PackageProjectUrl>https://github.com/andy840119/NHentaiAPI</PackageProjectUrl>
<RepositoryUrl>https://github.com/andy840119/NHentaiAPI.git</RepositoryUrl>
<PackageProjectUrl>https://github.com/Sylveon76/NHentaiAPI</PackageProjectUrl>
<RepositoryUrl>https://github.com/Sylveon76/NHentaiAPI.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>nhentai n-hentai hentai api csharp andy840119</PackageTags>
<PackageTags>nhentai n-hentai hentai api csharp Sylveon76, andy840119</PackageTags>
<PackageReleaseNotes>
[1.4.0]
Add get url method.
[1.5.0]
Refactor api client
</PackageReleaseNotes>
<PackageLicenseUrl>https://github.com/andy840119/NHentaiAPI</PackageLicenseUrl>
<Version>1.4.0</Version>
<PackageLicenseUrl>https://github.com/Sylveon76/NHentaiAPI/LICENSE</PackageLicenseUrl>
<Version>1.5.0</Version>
</PropertyGroup>

<!-- Conditionally obtain references for the .NET Framework 4.5 target -->
Expand Down
86 changes: 43 additions & 43 deletions NHentaiAPI/NHentaiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TOutput> GetData<TOutput>(string rootUrl)
private async Task<TOutput> GetData<TOutput>(string rootUrl)
{
var json = await _client.GetStringAsync(rootUrl);
return JsonConvert.DeserializeObject<TOutput>(json);
}

protected virtual async Task<byte[]> GetByteData(string rootUrl)
private async Task<byte[]> 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)
{
Expand All @@ -148,19 +148,19 @@ protected virtual string ConvertType(ImageType type)

#region Search

public virtual Task<SearchResults> GetHomePageListAsync(int pageNum)
{
public Task<SearchResults> GetHomePageListAsync(int pageNum)
{
var url = GetHomePageUrl(pageNum);
return GetData<SearchResults>(url);
}

public virtual Task<SearchResults> GetSearchPageListAsync(string keyword,int pageNum)
{
public Task<SearchResults> GetSearchPageListAsync(string keyword, int pageNum)
{
var url = GetSearchUrl(keyword, pageNum);
return GetData<SearchResults>(url);
}

public virtual Task<SearchResults> GetTagPageListAsync(Tag tag, SortBy sortBy, int pageNum)
public Task<SearchResults> GetTagPageListAsync(Tag tag, SortBy sortBy, int pageNum)
{
var url = GetTagUrl(tag, sortBy == SortBy.Popular, pageNum);
return GetData<SearchResults>(url);
Expand All @@ -170,13 +170,13 @@ public virtual Task<SearchResults> GetTagPageListAsync(Tag tag, SortBy sortBy, i

#region Books

public virtual Task<Book> GetBookAsync(int bookId)
public async Task<Book> GetBookAsync(int bookId)
{
var url = GetBookDetailsUrl(bookId);
return GetData<Book>(url);
return await GetData<Book>(url);
}

public virtual async Task<BookRecommend> GetBookRecommendAsync(int bookId)
public async Task<BookRecommend> GetBookRecommendAsync(int bookId)
{
var url = GetBookRecommendUrl(bookId);
var book = await GetData<Book>(url);
Expand All @@ -190,31 +190,31 @@ public virtual async Task<BookRecommend> GetBookRecommendAsync(int bookId)

#region Picture

public virtual Task<byte[]> GetPictureAsync(Book book, int pageNum)
public Task<byte[]> GetPictureAsync(Book book, int pageNum)
{
var url = GetPictureUrl(book, pageNum);
return GetByteData(url);
}

public virtual Task<byte[]> GetThumbPictureAsync(Book book, int pageNum)
public Task<byte[]> GetThumbPictureAsync(Book book, int pageNum)
{
var url = GetThumbPictureUrl(book, pageNum);
return GetByteData(url);
}

public virtual Task<byte[]> GetBigCoverPictureAsync(Book book)
public Task<byte[]> GetBigCoverPictureAsync(Book book)
{
var url = GetBigCoverUrl(book.MediaId);
return GetByteData(url);
}

public virtual Task<byte[]> GetOriginPictureAsync(Book book, int pageNum)
public Task<byte[]> GetOriginPictureAsync(Book book, int pageNum)
{
var url = GetOriginPictureUrl(book.MediaId, pageNum);
return GetByteData(url);
}

public virtual Task<byte[]> GetBookThumbPictureAsync(Book book)
public Task<byte[]> GetBookThumbPictureAsync(Book book)
{
var url = GetBookThumbUrl(book);
return GetByteData(url);
Expand All @@ -227,4 +227,4 @@ public void Dispose()
_client.Dispose();
}
}
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 4751ef1

Please sign in to comment.