SylveonDeko / NHentaiAPI

A (full) nHentai API implementation for .NET
MIT License
33 stars 4 forks source link

Be able to get a book from url #3

Closed sabihoshi closed 2 years ago

sabihoshi commented 5 years ago

Instead of using an id to get request the book, it would be great to just provide the URI or a string to pass to the GetBookAsync method. Instead of having to parse the link manually, I would be able to just pass that string.

andy840119 commented 5 years ago

Do you means using GetBookAsync (string url,int bookId) instead of GetBookAsync (int bookId) ?

sabihoshi commented 5 years ago

GetBookAsync(string url) overload Something that can go along the lines of

Regex.Match(link, @"\b(?:(?:https?:\/\/)?nhentai\.net\/g\/)?(?<id>[0-9]{1,6})\b", RegexOptions.IgnoreCase);
if(!Regex.Success)
    throw new ArgumentException("The url provided is not valid.", link);
andy840119 commented 5 years ago

I have a question, why only GetBookAsync need extra URL ? . Generally, developer using this package don't need to know the n-hentai's API URL.

sabihoshi commented 5 years ago

I'm trying to pull metadata from a given link, but the data are almost always the complete url instead of just the ID.

andy840119 commented 5 years ago

hmm... Do you means this API /api/gallery/{bookId} Can pass more them one parameter ? . If that, Can you show me some example? Thanks : )

sabihoshi commented 5 years ago

Ah no, I just mean that there should be a way to automatically parse the ID from the url link itself, instead of having to get it manually. Simply just NhentaiService.GetBookAsync("https://n...entai.net/g/xxx")

andy840119 commented 5 years ago

Do you means... Somebody might want to throw the whole URL into NhentaiService.GetBookAsync for some reason?

sabihoshi commented 5 years ago

Yes, that's right

andy840119 commented 5 years ago

I need to think about it Because developer can use "https://n...entai.net/g/xxx".Split('/').LastOrDefault() to get the Id number

emmauss commented 5 years ago

Adding an overload for urls isn't a bad thing. it makes it convenient and less error prone for the dev. you could parse the id from it and just call the other overload under the surface

andy840119 commented 4 years ago

Maybe add Task<Book> GetBookFromUrlAsync(string url) to get book info? Also is it need to check base url is same as Client's url because image url will use client's base url, not url from parameter.

Montegro commented 2 years ago

I've got a method to do this already in my project, I know I'm waaay late to this posted issue, and my way is a bit hamfisted, but you're welcome to the code snippet: string GetBookID = URL.Substring(URL.LastIndexOf("/g/") + 3); int EndOfBookID = GetBookID.IndexOf('/'); string TrimTobeSafe = GetBookID.Substring(0, EndOfBookID); I share this moreso cause it's friendlier to someone putting a link in from an individual page, it'll still pull the ID up.

andy840119 commented 2 years ago

Not really what EndOfBookID means 🤔 Is that a new API in the n-hentai? . Also, could you provide the URL that you called?

Montegro commented 2 years ago

Oh no this isn't a new API, these are declared variables that I run before calling on the API to find the book. The string that calls URL is pulled from a textbox in my program, and I can confirm that I've run this with well over a thousand different NHentai galleries by copying their URL successfully. After those three lines, I feed TrimToBeSafe into the NHentaiAPI to pull the gallery in my program.

andy840119 commented 2 years ago

Sorry not really good at reading. I'm still can't understand what those code you paste means?

string GetBookID = URL.Substring(URL.LastIndexOf("/g/") + 3); // what does add 3 means, why not add 4 or other number?
int EndOfBookID = GetBookID.IndexOf('/'); // what does end of book id means?
string TrimTobeSafe = GetBookID.Substring(0, EndOfBookID); // what is tobe?
Montegro commented 2 years ago

Sorry not really good at reading. I'm still can't understand what those code you paste means?

string GetBookID = URL.Substring(URL.LastIndexOf("/g/") + 3); // what does add 3 means, why not add 4 or other number?
int EndOfBookID = GetBookID.IndexOf('/'); // what does end of book id means?
string TrimTobeSafe = GetBookID.Substring(0, EndOfBookID); // what is tobe?

For the first question, add 3 because when creating the substring of URL to get the beginning of the ID from the URL you need to identify when the substring starts, looking at the URL for "/g/" returns the index of the first "/" so what you would wind up with from nhentai.com/g/500/ would be /g/500/, adding 3 to the index it's using to cut the URL makes it return "500/" in my example.

For the second, I just named the variable "EndOfBookID" because it locates the index of the first "/", continuing my example we're working with "500/" so EndOfBookID becomes "4", so we feed that to the next variable TrimTobeSafe which takes the "500/" and creates a substring of it to return "500".

"tobe" is "To Be" for the string variable "TrimTobeSafe" I meant it to be read "Trim To Be Safe" but didn't capitalize the B in Be, woops.

The whole purpose of these three lines of code is so that in my program if someone submits "nhentai.com/g/500" or "nhentai.com/g/500/" or even "nhentai.com/g/500/1" or as far as "nhentai.com/g/500/1/" it all ends up working cause those three cut each instance down to "500" to feed into the NhentaiAPI who's only looking for the gallery ID number, so my users can just copy the URL cause let's face it, people like to be lazy ya know?

SylveonDeko commented 2 years ago

Is this still a wanted feature?