From 87fb8855b79d22058944385d2c1eec2f364f59e5 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 15 Apr 2018 11:15:43 +0200 Subject: [PATCH] - Show Spotify results first from MPD search, if available - More granular calculation of the match score --- platypush/message/event/__init__.py | 8 ++++---- platypush/plugins/music/mpd/__init__.py | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/platypush/message/event/__init__.py b/platypush/message/event/__init__.py index 17e90f2c..561ab2e8 100644 --- a/platypush/message/event/__init__.py +++ b/platypush/message/event/__init__.py @@ -118,12 +118,12 @@ class Event(Message): if event_token == condition_token: event_tokens.pop(0) condition_tokens.pop(0) - result.score += 1 + result.score += 1.5 elif re.search(condition_token, event_token): m = re.search('({})'.format(condition_token), event_token) if m.group(1): event_tokens.pop(0) - result.score += 1 + result.score += 1.25 condition_tokens.pop(0) else: @@ -132,7 +132,7 @@ class Event(Message): argname = m.group(1) if argname not in result.parsed_args: result.parsed_args[argname] = event_token - result.score += 1 + result.score += 1.0 else: result.parsed_args[argname] += ' ' + event_token @@ -146,7 +146,7 @@ class Event(Message): event_tokens.pop(0) else: - result.score -= 1 + result.score -= 1.0 event_tokens.pop(0) # It's a match if all the tokens in the condition string have been satisfied diff --git a/platypush/plugins/music/mpd/__init__.py b/platypush/plugins/music/mpd/__init__.py index 1528d3ec..e73a3e9e 100644 --- a/platypush/plugins/music/mpd/__init__.py +++ b/platypush/plugins/music/mpd/__init__.py @@ -130,8 +130,13 @@ class MusicMpdPlugin(MusicPlugin): output=self.client.findadd(*filter, *args, **kwargs)) def search(self, filter, *args, **kwargs): - return Response( - output=self.client.search(*filter, *args, **kwargs)) + items = self.client.search(*filter, *args, **kwargs) + + # Spotify results first + items = sorted(items, key=lambda item: + 0 if item['file'].startswith('spotify:') else 1) + + return Response(output=items) def searchadd(self, filter, *args, **kwargs): return Response(