diff options
| author | Hunternif | 2021-08-31 04:12:30 +0100 |
|---|---|---|
| committer | Hunternif | 2021-08-31 04:20:48 +0100 |
| commit | 52205093803136cb64592cbbe12a2723a910d349 (patch) | |
| tree | 8e12c23dcfebf02ef9fc19e639d9baa0a253ed25 /server/szurubooru/api/post_api.py | |
| parent | deb7ab6ba6249e4ec85f95d0aa3451a20a3818ec (diff) | |
server: implement search for similar posts based on tags
Diffstat (limited to 'server/szurubooru/api/post_api.py')
| -rw-r--r-- | server/szurubooru/api/post_api.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/server/szurubooru/api/post_api.py b/server/szurubooru/api/post_api.py index a83ccc0..edb78fb 100644 --- a/server/szurubooru/api/post_api.py +++ b/server/szurubooru/api/post_api.py @@ -11,6 +11,7 @@ from szurubooru.func import ( posts, scores, serialization, + similar, snapshots, tags, versions, @@ -322,7 +323,8 @@ def get_posts_by_image( @rest.routes.get("/posts/median/?") def get_posts_median( - ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response: + ctx: rest.Context, _params: Dict[str, str] = {} +) -> rest.Response: auth.verify_privilege(ctx.user, "posts:list") _search_executor_config.user = ctx.user query_text = ctx.get_param_as_string("query", default="") @@ -336,3 +338,21 @@ def get_posts_median( "total": len(results), "results": list([_serialize_post(ctx, post) for post in results]) } + + +@rest.routes.get("/post/(?P<post_id>[^/]+)/similar/?") +def get_posts_similar( + ctx: rest.Context, params: Dict[str, str] +) -> rest.Response: + auth.verify_privilege(ctx.user, "posts:view:similar") + _search_executor_config.user = ctx.user + post_id = _get_post_id(params) + post = posts.get_post_by_id(post_id) + limit = ctx.get_param_as_int("limit", default=10, min=1, max=100) + results = similar.find_similar_posts(post, limit) + return { + "limit": limit, + "results": list([ + posts.serialize_micro_post(result, ctx.user) for result in results + ]) + } |