Add primitive "Any" search
authorClinton Ebadi <clinton@unknownlamer.org>
Sat, 10 Oct 2020 16:38:46 +0000 (12:38 -0400)
committerClinton Ebadi <clinton@unknownlamer.org>
Sat, 10 Oct 2020 16:38:46 +0000 (12:38 -0400)
Searches album/artist/title, and just cats the results together. Not
super useful in its current form.

party-upload/partyparty.py

index a9ef395..6ac2fd3 100644 (file)
 import cgi, cgitb
 from datetime import datetime
 import hashlib
+import itertools
 import numbers
 import os
 import random
+import re
 import subprocess
-from xbmcjson import XBMC
 import urllib
+from xbmcjson import XBMC
 from yattag import Doc
 import youtube_dl
 
@@ -124,15 +126,30 @@ class SongControls:
       return doc.getvalue ()
 
 class Search:
+   ANY_SEARCH_PROPERTIES = [ 'title', 'album', 'artist' ]
+
    def __init__ (self, term = '', prop = 'title'):
       self.term = term
       self.prop = prop
       if (term != ''):
-         res = xbmc.AudioLibrary.GetSongs (filter={'operator': "contains", 'field': self.prop, 'value': self.term}, properties=SONG_PROPERTIES, sort={'order': 'ascending', 'method': 'artist'})['result']
-         if 'songs' in res:
-            self.results = songs(res['songs'])
-         else:
-            self.results = []
+         self.results = self.get_search_results ()
+
+   def get_search_results (self):
+       if (self.term == ''):
+           return {}
+
+       if (self.prop != 'any'):
+           res = xbmc.AudioLibrary.GetSongs (filter={'operator': "contains", 'field': self.prop, 'value': self.term}, properties=SONG_PROPERTIES, sort={'order': 'ascending', 'method': 'artist'})['result']
+           if 'songs' in res:
+               return songs(res['songs'])
+           else:
+               return []
+       else:
+           all_songs = [xbmc.AudioLibrary.GetSongs (filter={'operator': "contains", 'field': p, 'value': self.term}, properties=SONG_PROPERTIES, sort={'order': 'ascending', 'method': 'artist'})['result']
+                        for p
+                        in self.ANY_SEARCH_PROPERTIES]
+           # does not remove duplicates...
+           return list (itertools.chain.from_iterable ([res['songs'] for res in all_songs if 'songs' in res]))
 
    def show_quick_search (self, thereal=False):
       doc, tag, text = Doc(defaults = {'searchfield': self.prop}).tagtext()
@@ -142,7 +159,7 @@ class Search:
           else:
               doc.stag ('input', type = 'text', name = 'searchterm', value = self.term)
           with doc.select (name = 'searchfield'):
-              for prop in ['title', 'artist', 'album']:
+              for prop in ['title', 'artist', 'album', 'any']:
                   with doc.option (value = prop):
                       text (prop)
           with tag ('button', type = 'submit', name = 'searchgo', value = '1'):