-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This plugin allows filtering by a unique tag e.g `@(unique: artist)` which will result only one song showing up per artist. It can be used multiple times e.g if one has mix albums in the library (albums with multiple artists), it's possible to use `&(@(unique: artist), @(unique: album))` to have one song per artist and one song per album
- Loading branch information
1 parent
d34690e
commit 99a8a1e
Showing
3 changed files
with
96 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright (C) 2023 LoveIsGrief | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
|
||
from quodlibet import _, print_d, print_w | ||
from quodlibet.plugins.query import QueryPlugin, QueryPluginError | ||
|
||
|
||
class UniqueQuery(QueryPlugin): | ||
PLUGIN_ID = "unique_query" | ||
PLUGIN_NAME = _("Unique Query") | ||
PLUGIN_DESC = _("Filter search results by unique tags.") | ||
key = 'unique' | ||
query_syntax = _("@(unique: tag)") | ||
query_description = "<tt>tag</tt> can be album, artist, title or any other tag. " \ | ||
"Use multiple <tt>@(unique: tag)</tt> to filter by multiple tags." | ||
|
||
usage = f"{query_syntax}\n\n{query_description}" | ||
|
||
def __init__(self): | ||
print_d("Initialising unique query") | ||
self.unique_tag_values = set() | ||
"""The unique tag values that have been seen in the songs being filtered""" | ||
self._reported = set() | ||
"""Unique errors to counter error log spam""" | ||
|
||
def search(self, data, body): | ||
return_value = False | ||
try: | ||
field_value = data[body] | ||
return_value = field_value not in self.unique_tag_values | ||
self.unique_tag_values.add(field_value) | ||
print_d(f"unique filtering value '{field_value}': {return_value}") | ||
except KeyError: | ||
pass | ||
except Exception as e: | ||
err_str = str(e) | ||
if err_str not in self._reported: | ||
self._reported.add(err_str) | ||
print_w("%s while filtering unique values for '%s': %s " | ||
% (type(e).__name__, body, err_str)) | ||
return return_value | ||
|
||
def parse_body(self, body): | ||
self.unique_tag_values.clear() | ||
if body is None: | ||
raise QueryPluginError | ||
unique_tag = body.strip() | ||
print_d(f"unique filtering tag: {unique_tag}") | ||
self._reported.clear() | ||
return unique_tag |