Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AniDB vector search API #551

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Contents/Code/AniDBVectorSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import urllib
import json

def compute_integer_scores_for_vector_search(items):
sorted_items = sorted(items, key=lambda item: item['score'], reverse=True)
previous_score = 101
for item in sorted_items:
raw_score = int(round(item['score'] * 100))

if raw_score >= previous_score:
# Reduce down to previous score less 1 if collision or higher
raw_score = previous_score - 1

item['score'] = raw_score
previous_score = raw_score

return sorted_items

def get_results_from_vector_search(orig_title):
api_url = "%s?name=%s" % (Prefs["vector_search_api"], urllib.quote(orig_title))

try:
response = HTTP.Request(api_url, cacheTime=CACHE_1DAY).content
response_dict = json.loads(response)
return compute_integer_scores_for_vector_search(response_dict)
except Exception as e:
Log.Debug("Got issue querying vector search API: " + str(e))
return None
25 changes: 25 additions & 0 deletions Contents/Code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import re
import os
import datetime
import json
import urllib

# HAMA Modules #
import common # Functions: GetPlexLibraries, write_logs, UpdateMeta Variables: PlexRoot, FieldListMovies, FieldListSeries, FieldListEpisodes, DefaultPrefs, SourceList
Expand All @@ -26,6 +28,7 @@
import AniList # Functions: GetMetadata Variables: None
import Local # Functions: GetMetadata Variables: None
import anidb34 # Functions: AdjustMapping Variables: None
import AniDBVectorSearch

### Variables ###

Expand Down Expand Up @@ -99,6 +102,28 @@ def Search(results, media, lang, manual, movie):
results.Append(MetadataSearchResult(id='clear-cache', name='Plex web cache cleared', year=media.year, lang=lang, score=0))
return

# Shortcut other search methods and use vector search if enabled
if Prefs["vector_search_enabled"] and Prefs["vector_search_api"] is not None:
Log.Debug("Searching for '%s' using vector search API" % (orig_title))

response_content = AniDBVectorSearch.get_results_from_vector_search(orig_title)
if response_content is None:
Log.Debug("Got no result from vector search API.")
elif "error" in response_content:
Log.Debug("Got error result from vector search API: %s" % (response_content["error"]))
else:
for response_entry in response_content:
name = "%s [%s]" % (response_entry["name"], response_entry["id"])
Log.Debug("Got result from vector search API: %s" % name)
results.Append(MetadataSearchResult(id=response_entry["id"],
name=name,
year=media.year,
lang=lang,
score=response_entry["score"]))
Log.Close()
return

# Continue with normal search as we failed to use vector search (or it's not enabled)
### Check if a guid is specified "Show name [anidb-id]" ###
Log.Info('--- force id ---'.ljust(157, '-'))
if orig_title and orig_title.isdigit(): orig_title = "xxx [tvdb-{}]".format(orig_title) #Support tvdbid as title, allow to support Xattr from FileBot with tvdbid filled in
Expand Down
Loading