diff options
| author | Hunternif | 2022-10-02 03:25:10 +0100 |
|---|---|---|
| committer | Hunternif | 2022-10-02 04:18:27 +0100 |
| commit | a82fa8940158784285872434659b5cb7fb9b817a (patch) | |
| tree | d0ffd2795ef2bca181801f04aa83aef9f8031527 /server/szurubooru/api | |
| parent | bc44d8055bf3b65365bf5329a8a4ad4ac4177cfc (diff) | |
client,server: reverse search post by signature
Diffstat (limited to 'server/szurubooru/api')
| -rw-r--r-- | server/szurubooru/api/post_api.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/server/szurubooru/api/post_api.py b/server/szurubooru/api/post_api.py index 2134b3b..3a35bf8 100644 --- a/server/szurubooru/api/post_api.py +++ b/server/szurubooru/api/post_api.py @@ -14,7 +14,7 @@ from szurubooru.func import ( similar, snapshots, tags, - versions, + versions, image_hash, ) _search_executor_config = search.configs.PostSearchConfig() @@ -321,6 +321,31 @@ def get_posts_by_image( } +@rest.routes.get("/post/(?P<post_id>[^/]+)/reverse-search/?") +def get_posts_lookalikes( + ctx: rest.Context, params: Dict[str, str] = {} +) -> rest.Response: + auth.verify_privilege(ctx.user, "posts:reverse_search") + limit = ctx.get_param_as_int("limit", default=10, min=1, max=100) + threshold = ctx.get_param_as_float("threshold", default=1, min=0, max=100) + post_id = _get_post_id(params) + post = posts.get_post_by_id(post_id) + sig = image_hash.unpack_signature(post.signature.signature) + lookalikes = posts.search_by_signature(sig, limit, threshold) + # exclude the same post: + lookalikes = filter(lambda la: la[1].post_id != post_id, lookalikes) + lookalikes = sorted(lookalikes, key=lambda la: la[0]) + return { + "similarPosts": [ + { + "distance": distance, + "post": _serialize_post(ctx, post), + } + for distance, post in lookalikes + ], + } + + @rest.routes.get("/posts/median/?") def get_posts_median( ctx: rest.Context, _params: Dict[str, str] = {} |