diff --git a/NHentaiAPI.Tests/BaseUnitTest.cs b/NHentaiAPI.Tests/BaseUnitTest.cs index aa106b4..188832a 100644 --- a/NHentaiAPI.Tests/BaseUnitTest.cs +++ b/NHentaiAPI.Tests/BaseUnitTest.cs @@ -1,4 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace NHentaiAPI.Tests { @@ -9,13 +10,7 @@ public class BaseUnitTest [TestInitialize] public void InitializeTest() { - NHentaiClient = new TestNHentaiClient(); - } - - [TestCleanup] - public void CleanUp() - { - NHentaiClient.Dispose(); + NHentaiClient = new TestNHentaiClient("a", new Dictionary()); } } @@ -26,5 +21,9 @@ public class TestNHentaiClient : NHentaiClient //protected override string ApiRootUrl => "https://nhent.ai"; #endregion + + public TestNHentaiClient(string userAgent, Dictionary cookies = null) : base(userAgent, cookies) + { + } } } \ No newline at end of file diff --git a/NHentaiAPI.Tests/NHentaiAPI.Tests.csproj b/NHentaiAPI.Tests/NHentaiAPI.Tests.csproj index 5b37f60..7085b7a 100644 --- a/NHentaiAPI.Tests/NHentaiAPI.Tests.csproj +++ b/NHentaiAPI.Tests/NHentaiAPI.Tests.csproj @@ -7,9 +7,12 @@ + + + diff --git a/NHentaiAPI.Tests/NHentaiPictureUnitTest.cs b/NHentaiAPI.Tests/NHentaiPictureUnitTest.cs index b36e379..cd37553 100644 --- a/NHentaiAPI.Tests/NHentaiPictureUnitTest.cs +++ b/NHentaiAPI.Tests/NHentaiPictureUnitTest.cs @@ -45,7 +45,7 @@ public async Task TestGetGifPictureResult() // Check url var imageUrl = NHentaiClient.GetPictureUrl(book, 22); - Assert.AreEqual(imageUrl.AbsoluteUri, "https://i.nhentai.net/galleries/1504878/22.gif"); + Assert.AreEqual(imageUrl, "https://i.nhentai.net/galleries/1504878/22.gif"); // Make sure image is downloaded var result = await NHentaiClient.GetPictureAsync(book, 22); diff --git a/NHentaiAPI/NHentaiAPI.csproj b/NHentaiAPI/NHentaiAPI.csproj index 53e278e..abc2628 100644 --- a/NHentaiAPI/NHentaiAPI.csproj +++ b/NHentaiAPI/NHentaiAPI.csproj @@ -1,7 +1,7 @@  - netstandard2.0;net45;net461;net6.0 + net5.0;net6.0 true Sylveon76, andy840119 nHentai @@ -19,24 +19,11 @@ Refactor api client 1.5.0 - - - - - - - - - - - - - - + - + diff --git a/NHentaiAPI/NHentaiClient.cs b/NHentaiAPI/NHentaiClient.cs index 3cd065a..0e414fc 100644 --- a/NHentaiAPI/NHentaiClient.cs +++ b/NHentaiAPI/NHentaiClient.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Net; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json; @@ -17,112 +18,136 @@ public class NHentaiClient : IDisposable { #region Client - private readonly HttpClient _client = new HttpClient(); + private readonly HttpClient _client; + + public NHentaiClient(string userAgent, Dictionary cookies = null) + { + var cookies1 = new CookieContainer(); + var handler = new HttpClientHandler { CookieContainer = cookies1 }; + _client = new HttpClient(handler); + + _client.DefaultRequestHeaders.Add("User-Agent", userAgent); + + if (cookies == null) return; + var cookieUri = new Uri(ApiRootUrl); + + foreach (var cookie in cookies) + { + cookies1.Add(cookieUri, new Cookie(cookie.Key, cookie.Value)); + } + } #endregion #region Urls - private const string ApiRootUrl = "https://nhentai.net"; - private const string ImageRootUrl = "https://i.nhentai.net"; - private const string ThumbnailRootUrl = "https://t.nhentai.net"; + protected virtual string ApiRootUrl => "https://nhentai.net"; + + protected virtual string ImageRootUrl => "https://i.nhentai.net"; + + protected virtual string ThumbnailRootUrl => "https://t.nhentai.net"; #endregion #region Data urls - private static Uri GetHomePageUrl(int pageNum) - => new Uri($"{ApiRootUrl}/api/galleries/all?page={pageNum}"); + protected virtual string GetHomePageUrl(int pageNum) + => $"{ApiRootUrl}/api/galleries/all?page={pageNum}"; - private static Uri GetSearchUrl(string content, int pageNum) - => new Uri($"{ApiRootUrl}/api/galleries/search?query={content.Replace(" ", "+")}&page={pageNum}"); + protected virtual string GetSearchUrl(string content,int pageNum) + => $"{ApiRootUrl}/api/galleries/search?" + + $"query={content.Replace(" ", "+")}&" + + $"page={pageNum}"; - private static Uri GetTagUrl(Tag tag, bool isPopularList, int pageNum) - => new Uri($"{ApiRootUrl}/api/galleries/tagged?tag_id={tag.Id}&page={pageNum}{(isPopularList ? "&sort=popular" : "")}"); + protected virtual string GetTagUrl(Tag tag, bool isPopularList, int pageNum) + => $"{ApiRootUrl}/api/galleries/tagged?" + + $"tag_id={tag.Id}" + + $"&page={pageNum}" + + (isPopularList ? "&sort=popular" : ""); - private static Uri GetBookDetailsUrl(int bookId) - => new Uri($"{ApiRootUrl}/api/gallery/{bookId}"); + protected virtual string GetBookDetailsUrl(int bookId) + => $"{ApiRootUrl}/api/gallery/{bookId}"; - private static Uri GetBookRecommendUrl(int bookId) - => new Uri($"{ApiRootUrl}/api/gallery/{bookId}/related"); + protected virtual string GetBookRecommendUrl(int bookId) + => $"{ApiRootUrl}/api/gallery/{bookId}/related"; - private static Uri GetGalleryUrl(int galleryId) - => new Uri($"{ImageRootUrl}/galleries/{galleryId}"); + protected virtual string GetGalleryUrl(int galleryId) + => $"{ImageRootUrl}/galleries/{galleryId}"; - private static Uri GetThumbGalleryUrl(int galleryId) - => new Uri($"{ThumbnailRootUrl}/galleries/{galleryId}"); + protected virtual string GetThumbGalleryUrl(int galleryId) + => $"{ThumbnailRootUrl}/galleries/{galleryId}"; #endregion #region Picture urls - public static Uri GetPictureUrl(Book book, int pageNum) + public virtual string GetPictureUrl(Book book, int pageNum) { - var image = GetImage(book, pageNum-1); + var image = GetImage(book, pageNum); var fileType = ConvertType(image.Type); - return GetPictureUrl(book.MediaId, pageNum, fileType); + return GetPictureUrl(book.MediaId, pageNum, fileType); } - public static Uri GetThumbPictureUrl(Book book, int pageNum) + public virtual string GetThumbPictureUrl(Book book, int pageNum) { var image = GetImage(book, pageNum); var fileType = ConvertType(image.Type); return GetThumbPictureUrl(book.MediaId, pageNum, fileType); } - public Uri GetBigCoverUrl(Book book) + public virtual string GetBigCoverUrl(Book book) => GetBigCoverUrl(book.MediaId); - public Uri GetOriginPictureUrl(Book book, int pageNum) + public virtual string GetOriginPictureUrl(Book book, int pageNum) => GetOriginPictureUrl(book.MediaId, pageNum); - public static Uri GetBookThumbUrl(Book book) + public virtual string GetBookThumbUrl(Book book) { var fileType = ConvertType(book.Images.Cover.Type); - return GetBookThumbUrl(book.MediaId, fileType); + return GetBookThumbUrl(book.MediaId, fileType); } - private static Uri GetPictureUrl(int galleryId, int pageNum, string fileType) - => new Uri($"{GetGalleryUrl(galleryId)}/{pageNum}.{fileType}"); + protected virtual string GetPictureUrl(int galleryId , int pageNum ,string fileType) + => $"{GetGalleryUrl(galleryId)}/{pageNum}.{fileType}"; - private static Uri GetThumbPictureUrl(int galleryId, int pageNum, string fileType) - => new Uri($"{GetThumbGalleryUrl(galleryId)}/{pageNum}t.{fileType}"); + protected virtual string GetThumbPictureUrl(int galleryId , int pageNum ,string fileType) + => $"{GetThumbGalleryUrl(galleryId)}/{pageNum}t.{fileType}"; - private static Uri GetBigCoverUrl(int galleryId) - => new Uri($"{GetThumbGalleryUrl(galleryId)}/cover.jpg"); + protected virtual string GetBigCoverUrl(int galleryId) + => $"{GetThumbGalleryUrl(galleryId)}/cover.jpg"; - private static Uri GetOriginPictureUrl(int galleryId, int pageNum) + protected virtual string GetOriginPictureUrl(int galleryId , int pageNum) => GetPictureUrl(galleryId, pageNum, "jpg"); - private static Uri GetBookThumbUrl(int galleryId, string fileType = "jpg") - => new Uri($"{GetThumbGalleryUrl(galleryId)}/thumb.{fileType ?? "jpg"}"); + protected virtual string GetBookThumbUrl(int galleryId ,string fileType = "jpg") + => $"{GetThumbGalleryUrl(galleryId)}/thumb.{fileType ?? "jpg"}"; #endregion #region Utilities - private async Task GetData(string rootUrl) + protected virtual async Task GetData(string rootUrl) { var json = await _client.GetStringAsync(rootUrl); return JsonConvert.DeserializeObject(json); } - private async Task GetByteData(string rootUrl) + protected virtual async Task GetByteData(string rootUrl) { var data = await _client.GetByteArrayAsync(rootUrl); return data; } - private static Image GetImage(Book book, int pageNum) + protected virtual Image GetImage(Book book, int pageNum) { if (book == null) throw new ArgumentNullException(nameof(book)); - - var page = book.Images.Pages[pageNum]; + + var page = book.Images.Pages[pageNum - 1]; return page; } - private static string ConvertType(ImageType type) + protected virtual string ConvertType(ImageType type) { switch (type) { @@ -141,38 +166,38 @@ private static string ConvertType(ImageType type) #region Search - public Task GetHomePageListAsync(int pageNum) - { + public virtual Task GetHomePageListAsync(int pageNum) + { var url = GetHomePageUrl(pageNum); - return GetData(url.AbsoluteUri); + return GetData(url); } - public Task GetSearchPageListAsync(string keyword, int pageNum) - { + public virtual Task GetSearchPageListAsync(string keyword,int pageNum) + { var url = GetSearchUrl(keyword, pageNum); - return GetData(url.AbsoluteUri); + return GetData(url); } - public Task GetTagPageListAsync(Tag tag, SortBy sortBy, int pageNum) + public virtual Task GetTagPageListAsync(Tag tag, SortBy sortBy, int pageNum) { var url = GetTagUrl(tag, sortBy == SortBy.Popular, pageNum); - return GetData(url.AbsoluteUri); + return GetData(url); } #endregion #region Books - public async Task GetBookAsync(int bookId) + public virtual Task GetBookAsync(int bookId) { var url = GetBookDetailsUrl(bookId); - return await GetData(url.AbsoluteUri); + return GetData(url); } - public async Task GetBookRecommendAsync(int bookId) + public virtual async Task GetBookRecommendAsync(int bookId) { var url = GetBookRecommendUrl(bookId); - var book = await GetData(url.AbsoluteUri); + var book = await GetData(url); return new BookRecommend { Result = new List { book } @@ -183,34 +208,34 @@ public async Task GetBookRecommendAsync(int bookId) #region Picture - public Task GetPictureAsync(Book book, int pageNum) + public virtual Task GetPictureAsync(Book book, int pageNum) { var url = GetPictureUrl(book, pageNum); - return GetByteData(url.AbsoluteUri); + return GetByteData(url); } - public Task GetThumbPictureAsync(Book book, int pageNum) + public virtual Task GetThumbPictureAsync(Book book, int pageNum) { var url = GetThumbPictureUrl(book, pageNum); - return GetByteData(url.AbsoluteUri); + return GetByteData(url); } - public Task GetBigCoverPictureAsync(Book book) + public virtual Task GetBigCoverPictureAsync(Book book) { var url = GetBigCoverUrl(book.MediaId); - return GetByteData(url.AbsoluteUri); + return GetByteData(url); } - public Task GetOriginPictureAsync(Book book, int pageNum) + public virtual Task GetOriginPictureAsync(Book book, int pageNum) { var url = GetOriginPictureUrl(book.MediaId, pageNum); - return GetByteData(url.AbsoluteUri); + return GetByteData(url); } - public Task GetBookThumbPictureAsync(Book book) + public virtual Task GetBookThumbPictureAsync(Book book) { var url = GetBookThumbUrl(book); - return GetByteData(url.AbsoluteUri); + return GetByteData(url); } #endregion