Skip to content

Commit 9ddbd54

Browse files
committed
playback: add -rr for re-ranking
1 parent a776d28 commit 9ddbd54

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

library/playback/play_actions.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ def parse_args(action, default_chromecast=None) -> argparse.Namespace:
9494
--fetch-siblings always -O # get 2,000 results per directory
9595
""",
9696
)
97+
ordering.add_argument(
98+
"--re-rank",
99+
"--rerank",
100+
"-rr",
101+
action=argparse_utils.ArgparseDict,
102+
default={},
103+
metavar="COLUMN=WEIGHT",
104+
help="""Add key/value pairs re-rank sorting by multiple attributes (similar to MCDA)
105+
'regex_sort=1 cluster_sort=1 -size=3' will sort the playlist by
106+
taking into account size, regex, and clusters prioritizing size over the other two""",
107+
)
97108

98109
probabling = parser.add_argument_group("Probability")
99110
probabling.add_argument(
@@ -407,7 +418,37 @@ def process_playqueue(args) -> None:
407418
if args.play_in_order:
408419
media = db_media.natsort_media(args, media)
409420

410-
if args.regex_sort:
421+
if args.re_rank:
422+
import pandas
423+
424+
from library.utils import pd_utils
425+
426+
df = pandas.DataFrame(media)
427+
428+
if args.regex_sort:
429+
from library.text import regex_sort
430+
431+
sorted_media = regex_sort.sort_dicts(args, media)
432+
df = pd_utils.from_dict_add_path_rank(df, sorted_media, "regex_sort")
433+
log.debug("regex-sort: %s", t.elapsed())
434+
elif args.cluster_sort:
435+
from library.text import cluster_sort
436+
437+
sorted_media = cluster_sort.sort_dicts(args, media)
438+
df = pd_utils.from_dict_add_path_rank(df, sorted_media, "cluster_sort")
439+
log.debug("cluster-sort: %s", t.elapsed())
440+
441+
column_weights = {
442+
k.lstrip("-"): {
443+
"direction": "desc" if k.startswith("-") else "asc",
444+
"weight": v or 1,
445+
}
446+
for k, v in args.re_rank.items()
447+
}
448+
df = pd_utils.rank_dataframe(df, column_weights)
449+
media = df.to_dict(orient="records")
450+
log.debug("re-rank: %s", t.elapsed())
451+
elif args.regex_sort:
411452
from library.text import regex_sort
412453

413454
media = regex_sort.sort_dicts(args, media)

library/utils/pd_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ def rank_dataframe(df, column_weights):
8585
def count_category(df, key_name):
8686
df[f"{key_name}_count"] = df.groupby(key_name)[key_name].transform("size")
8787
return df
88+
89+
90+
def from_dict_add_path_rank(df, sorted_media, new_rank_column_name):
91+
rank_dict = {item["path"]: rank + 1 for rank, item in enumerate(sorted_media)}
92+
df[new_rank_column_name] = df["path"].map(rank_dict)
93+
return df

tests/utils/test_web.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def test_parent_property():
251251
assert str(web_path.parent.parent.parent.parent.parent.parent.parent) == "https://<netloc>"
252252
assert str(web_path.parent.parent.parent.parent.parent.parent.parent.parent) == "https://<netloc>"
253253

254+
254255
@pytest.mark.parametrize(
255256
"base_url, href, expected",
256257
[
@@ -264,7 +265,7 @@ def test_parent_property():
264265
("https://unli.xyz/", "//example.com/ch/", "https://example.com/ch/"),
265266
("https://unli.xyz/diskprices", "ftp://example.com/ch/", "ftp://example.com/ch/"),
266267
("https://unli.xyz/diskprices", "ssh://example.com/ch/", "ssh://example.com/ch/"),
267-
]
268+
],
268269
)
269270
def test_construct_absolute_url(base_url, href, expected):
270271
result = construct_absolute_url(base_url, href)

0 commit comments

Comments
 (0)