diff --git a/catalog/api.py b/catalog/api.py index a5ebfc29..cd1653df 100644 --- a/catalog/api.py +++ b/catalog/api.py @@ -35,7 +35,7 @@ def search_item( query = query.strip() if not query: return 400, {"message": "Invalid query"} - items, num_pages, count = query_index( + items, num_pages, count, _ = query_index( query, page=page, category=category, prepare_external=False ) return 200, {"data": items, "pages": num_pages, "count": count} diff --git a/catalog/search/models.py b/catalog/search/models.py index 32b9119e..d80a8367 100644 --- a/catalog/search/models.py +++ b/catalog/search/models.py @@ -39,22 +39,38 @@ else: def query_index(keywords, category=None, tag=None, page=1, prepare_external=True): result = Indexer.search(keywords, page=page, category=category, tag=tag) - keys = [] + keys = set() items = [] + duplicated_items = [] urls = [] for i in result.items: - key = ( - i.isbn + if i.is_deleted or i.merged_to_item: # only happen if index is delayed + continue + my_key = ( + [i.isbn] if hasattr(i, "isbn") - else (i.imdb_code if hasattr(i, "imdb_code") else None) + else ([i.imdb_code] if hasattr(i, "imdb_code") else []) ) - if key is None: - items.append(i) - elif key not in keys: # skip dup with same imdb or isbn - keys.append(key) + if hasattr(i, "works"): + my_key += [i[0] for i in i.works.all().values_list("id")] + if len(my_key): + l = len(keys) + len(my_key) + keys.update(my_key) + # check and skip dup with same imdb or isbn or works id + if len(keys) < l: + duplicated_items.append(i) + else: + items.append(i) + else: items.append(i) for res in i.external_resources.all(): urls.append(res.url) + # hide show if its season exists + seasons = [i for i in items if i.__class__ == TVSeason] + for season in seasons: + if season.show in items: + duplicated_items.append(season.show) + items.remove(season.show) if prepare_external: # store site url to avoid dups in external search @@ -62,13 +78,7 @@ def query_index(keywords, category=None, tag=None, page=1, prepare_external=True urls = list(set(cache.get(cache_key, []) + urls)) cache.set(cache_key, urls, timeout=300) - # hide show if its season exists - seasons = [i for i in items if i.__class__ == TVSeason] - for season in seasons: - if season.show in items: - items.remove(season.show) - - return items, result.num_pages, result.count + return items, result.num_pages, result.count, duplicated_items def enqueue_fetch(url, is_refetch): diff --git a/catalog/search/views.py b/catalog/search/views.py index 42bd404e..e4b68da1 100644 --- a/catalog/search/views.py +++ b/catalog/search/views.py @@ -97,12 +97,13 @@ def search(request): if site: return fetch(request, keywords, False, site) - items, num_pages, _ = query_index(keywords, category, tag, p) + items, num_pages, _, dup_items = query_index(keywords, category, tag, p) return render( request, "search_results.html", { "items": items, + "dup_items": dup_items, "pagination": PageLinksGenerator(PAGE_LINK_NUMBER, p, num_pages), "sites": SiteName.labels, "hide_category": category is not None and category != "movietv", diff --git a/catalog/templates/_item_card_metadata.html b/catalog/templates/_item_card_metadata.html index 716b7a51..7bb03f1a 100644 --- a/catalog/templates/_item_card_metadata.html +++ b/catalog/templates/_item_card_metadata.html @@ -53,7 +53,7 @@ {% include '_people.html' with people=item.genre role='' max=2 %} {% include '_people.html' with people=item.troupe role='' max=2 %} {% include '_people.html' with people=item.location role='' max=2 %} - {% include '_people.html' with people=item.language role='' max=5 %} + {% comment %} {% include '_people.html' with people=item.language role='' max=5 %} {% endcomment %} {% include '_people.html' with people=item.platform role='' max=5 %} {% if item.show %} {{ item.show.type.label }}:{{ item.show.title }} diff --git a/catalog/templates/search_results.html b/catalog/templates/search_results.html index 1f41b8a5..7cec4c94 100644 --- a/catalog/templates/search_results.html +++ b/catalog/templates/search_results.html @@ -81,6 +81,20 @@ {% empty %}

{% trans '无站内条目匹配' %}

{% endfor %} + + {% if dup_items %} +

+ 已从结果中略去了来自同一著作或有相同标识号的 {{ dup_items|length }}个条目,点击这里可重新显示 +

+ + {% endif %} +
{% if request.GET.q and user.is_authenticated %}