jikan-me / jikan-rest

The REST API for Jikan
https://docs.api.jikan.moe/
MIT License
414 stars 268 forks source link

[Search] Add Order By #48

Closed irfan-dahir closed 5 years ago

irfan-dahir commented 5 years ago

https://myanimelist.net/anime.php?q=&letter=&type=0&score=0&status=0&p=0&r=0&sm=0&sd=0&sy=0&em=0&ed=0&ey=0&c[0]=a&c[1]=b&c[2]=c&c[3]=d&c[4]=e&c[5]=f&c[6]=g&gx=0&o=7&w=2&o=7&w=1

It's possible to order by columns available on the search results by passing some extra GET params.

purplepinapples commented 5 years ago

An attempt to understand get parameters in MAL searches:

Param Name Description Example Range
q search query q=Hello Any Alphabetical character
score any score that starts with this int Gintama has a score 9.14, so it should show up on score=9 1 -10, but should be 1 - 9 since nothing will ever be scored 10/10 on MAL
type type, e.g. Movie/TV type=0 0=all, 1=TV, 2=OVA, 3=Movie, 4=Special, 5=ONA, 6=Music
status airing status status=2 3=unaired, 2=finished airing, 1=airing, 0=all
p producer p=11 p=, where producer is the corresponding id in https://myanimelist.net/anime/producer/11
r rating r=2 0=all, 1=G, 2=PG, 3=PG-13, 4=R, 5=R+ (no 6=Rx???)
sm, sd, sy start month/day/year sd=1&sm=5&sy=2015 typical date ranges. If a start date is specified by no end date, this gives you anything after that start date
em, ed, ey end month/day/year analogous to above

I realize that this stuff is already in Jikan, I just wanted to wrap my head around it and thought this might be an alright place to leave this.

Regarding the URL above, I dont know whats going on with the

o=7&w=2&o=7&w=1 at the end.

I'm not super familiar with GET parameters, does one overwrite the other...? o=7 twice?

Also, not sure if its something you'd want to add, but we're missing the letter paramter. E.g.:

https://myanimelist.net/anime.php?letter=. (punctuation) https://myanimelist.net/anime.php?letter=A https://myanimelist.net/anime.php?letter=B

For entries that start with that letter

irfan-dahir commented 5 years ago

I'm not super familiar with GET parameters, does one overwrite the other...? o=7 twice? Yep, that's my bad. I just dumped the URL in a hurry. o=7&w=1 is currently applying to the request, it overwrites the previous o and w params.

irfan-dahir commented 5 years ago

I realized we were missing the letter param. Seems like I forgot to add it then (it's already in the parser lol). I recently made a commit that adds it. Would be there in the next update

purplepinapples commented 5 years ago

Great.

Also, regarding the w parameter (which only reverses order for o=6,7,8,9), they dont use the w parameter for o=0-5 and instead split those into two (e.g. o=0 ascending name, o=1 descending name)

Using o=7 (popularity) as an example

These two order in descending order

https://myanimelist.net/anime.php?o=7 https://myanimelist.net/anime.php?o=7&w=1

Anything else orders in ascending order:

https://myanimelist.net/anime.php?o=7&w=-1 https://myanimelist.net/anime.php?o=7&w=2 https://myanimelist.net/anime.php?o=7&w=0 https://myanimelist.net/anime.php?o=7&w=6023424

purplepinapples commented 5 years ago

Seems the w parameter can be used on o=0-6, but it doesn't offer any additional information.

These sort titles alphabetically

https://myanimelist.net/anime.php?o=0 https://myanimelist.net/anime.php?o=0&w=52345 https://myanimelist.net/anime.php?o=0&w=2

But specifying exactly w=1 to o=0 will sort it in reverse, even though o=1 already sorts it in reverse. i.e. these are equivalent:

https://myanimelist.net/anime.php?o=0&w=1 https://myanimelist.net/anime.php?o=1

I dont think this that important, following this gives you every possibility for ordering.

purplepinapples commented 5 years ago

Oh, jikan is missing the p (producer) param as well, unless you already added that.

In addition, here, it says the q= is required for v3+, but I dont think it has to be.

It would be pretty useful to not require the q as that means the search endpoint can become a way to create a list of every approved entry on MAL, which is a common problem in the discord.

I understand that the MAL anime.php endpoint requires a minimum amount of parameters to work, and just doing https://myanimelist.net/anime.php?q=&type=0&score=0&status=0&p=0&r=0, where the q is empty, and every other basic param is 0, which means 'all', it results in an error.

However, using the order parameter allows you to search for something without a query:

https://myanimelist.net/anime.php?q=&type=0&score=0&status=0&p=0&r=0&o=0

The corresponding request on jikan might be api.jikan.moe/v3/search/anime?page=1

If no orderby is specified in the jikan parameters, it can default to one (e.go=9&w=2, to give low MAL IDs first, as that lowers the chances of the search results changing while you're requesting)), to allow searches without queries.

e.g. this:

api.jikan.moe/v3/search/anime?page=1

would convert to:

https://myanimelist.net/anime.php?q=&o=9&w=2

One could iterate from

api.jikan.moe/v3/search/anime?page=1 (https://myanimelist.net/anime.php?q=&o=9&w=2)

to

api.jikan.moe/v3/search/anime?page=314 (https://myanimelist.net/anime.php?q=&o=9&w=2&show=15650)

to get a list of all entries

irfan-dahir commented 5 years ago

You're right seems like producer is missing from just the REST API as well :fearful: I will add that asap.

It seems like I misphrased the "requirement" of q - what I meant was depreciating the backward compatibility of searching like this in v2 /search/Fate which was causing issues. Hence the usage of q was "required". Sorry about the confusion, I will need to fix that in the docs.

Regarding how the search would work without the q parameter, it's currently possible to run that query: api.jikan.moe/v3/search/anime?page=1 returns empty - as would https://myanimelist.net/anime.php?q=

Here are some examples: /v3/search/anime?genre[]=1&genre[]2 (multiple genre response) /v3/search/anime?start_date=2019-04-29 "upcoming" v3/search/anime?status=upcoming alternative "upcoming" /v3/search/anime?rated=g

api.jikan.moe/v3/search/anime?page=1

would convert to:

https://myanimelist.net/anime.php?q=&o=9&w=2

I feel defaulting these two would be incorrect. The goal is to respond the same stuff MAL would with the same query passed to MAL. i.e the response for https://myanimelist.net/anime.php?q= would be "No titles that matched your query were found." So /v3/search/anime?q= returns with an empty array of results as it does.

One could iterate from

api.jikan.moe/v3/search/anime?page=1
(https://myanimelist.net/anime.php?q=&o=9&w=2)

to

api.jikan.moe/v3/search/anime?page=314
(https://myanimelist.net/anime.php?q=&o=9&w=2&show=15650)

to get a list of all entries

I'm looking to having a different orderby value in order to query that sort of request.

We could possibly do something like this instead: /v3/search/anime?orderby=added&sort=ascending -> https://myanimelist.net/anime.php?q=&o=9&w=2 (from the first added item onwards) /v3/search/anime?orderby=added&sort=descending -> https://myanimelist.net/anime.php?q=&o=9&w=1 (from the last added item)

What do you think?

purplepinapples commented 5 years ago

All sounds reasonable,.

I suppose its fine if it doesnt default to an ordering, I dont think youd want to return an error for it but its a bit confusing that https://api.jikan.moe/v3/search/anime?page=1 returns empty

Perhaps adding an example that doesnt have a search query in the docs would help push people towards knowing you can do one without a q param

irfan-dahir commented 5 years ago

I think an empty array is the correct response. An error would generally be not an option as all keys with array/object values in the API return empty if they're not present on the website. Devs can easily check whether array/object values are empty and respond accordingly in their apps.

Indeed, I will further document search examples. :100:

irfan-dahir commented 5 years ago

So here's my compiled understanding and testing of the ordering and sorting so far:

Note: all these requests are appended with the following GET arguments: c[]=a&c[]=b&c[]=c&c[]=f&c[]=d&c[]=e&c[]=g This ensured the availability of all the available columns as listed below. The o value will change if these arguments are not passed on.

There are two sorting values for w that work for o from 0-9

Sort Direction Value
Ascending 0
Descending 1

Note: However, the behavior for these two value are reversed if a certain o is the reverse of of it's other. See examples below with reverse marked.

Anime

Column Order By Value o=value Default Sort Value on MAL (without passing w)
title 0 0
title (reverse) 1 It's same as passing w=1 to o=0; thus we can ignore this value safely
start date 2 1 (Defaulted by the anime with the most future start date to the oldest)
score 3 1 (high -> low)
episodes 4 1 (high -> low)
end date 5 1 (Defaulted by the anime with the most future end date to the oldest)
type 6 1 (Music -> ONA -> Special -> Movie -> OVA -> TV -> Unknown)
members 7 1
rated 8 1 (Rx -> R+ -> R -> PG13 -> PG -> G)
MAL ID 9 1 (high -> low)

Manga

Column Order By Value o=value Default Sort Value on MAL (without passing w)
title 0 0
title (reverse) 1 It's same as passing w=1 to o=0; thus we can ignore this value safely
start date 2 1 (Defaulted by the manga with the most future start date to the oldest)
score 3 1 (high -> low)
volumes 4 1 (high -> low)
end date 5 1 (Defaulted by the manga with the most future end date to the oldest)
chapters 6 1
members 7 1
type 8 1 (Manhua, Manhwa, Doujinshi, One-shot, Novel, Manga)
MAL ID 9 1 (high -> low)
irfan-dahir commented 5 years ago

Added to the parser, transferring the issue to the jikan-rest repo.