Skip to content

Commit

Permalink
added text filter support
Browse files Browse the repository at this point in the history
  • Loading branch information
plamere committed Feb 27, 2016
1 parent 497de22 commit 764ed7b
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 3 deletions.
46 changes: 46 additions & 0 deletions server/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,52 @@
}
}
},
{
"name" : "TextFilter",
"class": plugs.TextFilter,
"type" : "filter",
"title" : "title $?invert:not-matching:matching $text",
"display": "title filter",
"description": "filters tracks with titles that match a given string",

"help" : """ This component will pass through tracks that have
a title that matches the given text string. The sense of the filter
can be inverted by settting the <b>invert</b> parameter. To ignore
case when matching set the <b>ignore case</b> flag. Note that
the matching text is a <a
href="https://www.regular-expressions.info">regular expression</a>
allowing for very sophisticated matching logic.""",

"params": {
"source": {
"type" : "port",
"optional" : False,
"port": "green",
"max_inputs": 1,
"description": "the source of the tracks",
},
"text": {
"type" : "string",
"optional" : False,
"display": "match",
"default" : "",
"description": "regular expression to match track title"
},
"ignore_case": {
"type" : "bool",
"optional" : False,
"display": "ignore case",
"default" : True,
"description": "if true, matches ignore letter case"
},
"invert": {
"type" : "bool",
"optional" : False,
"default" : False,
"description": "if true, the filter is reversed"
}
}
},
{
"name" : "Spoken Word",
"class": plugs.SpokenWord,
Expand Down
74 changes: 73 additions & 1 deletion server/plugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import simplejson as json
import time
import reltime
import re

cache = SimpleCache()

Expand Down Expand Up @@ -183,6 +184,35 @@ def next_track(self):
break
return None

class TextFilter(object):
'''
produces tracks from the stream based on track title match
'''
def __init__(self, source, text, ignore_case=False, invert=False):
prep = ' that do not match ' if invert else ' that match '
self.name = 'tracks in ' + source.name + prep + '"' + text + '"'
self.source = source
self.invert = invert
flags = re.UNICODE
if ignore_case:
flags |= re.IGNORECASE
self.regex = re.compile(text, flags)

def next_track(self):
while True:
track = self.source.next_track()
if track:
tinfo = pbl.tlib.get_track(track)
title = tinfo['title']
does_match = self.regex.search(title) != None
if does_match == self.invert:
continue
else:
return track
else:
break
return None


MOST = 0
MORE = 1
Expand Down Expand Up @@ -1212,6 +1242,7 @@ def next_track(self):
else:
return None


def now():
return datetime.datetime.now()

Expand Down Expand Up @@ -1287,7 +1318,7 @@ def parse_date(sdate):
src = WeightedShuffler(src, .01)
pbl.show_source(src)

if True:
if False:
sixmonths = 60 * 60 * 24 * 30 * 6
onemonth = 60 * 60 * 24 * 30 * 1

Expand All @@ -1308,3 +1339,44 @@ def parse_date(sdate):
order_by_date_added=False,
tracks_added_since="six months", tracks_added_before="1 month")
pbl.show_source(src)

if False:
print 'std'
src = pbl.PlaylistSource("extender test", None, 'plamere')
pbl.show_source(src)

src = pbl.PlaylistSource("extender test", None, 'plamere')
src = TextFilter(src, 'the', True, False)
print src.name
pbl.show_source(src)

src = pbl.PlaylistSource("extender test", None, 'plamere')
src = TextFilter(src, 'the', False, False)
print src.name
pbl.show_source(src)

src = pbl.PlaylistSource("extender test", None, 'plamere')
src = TextFilter(src, 'the', True, True)
print src.name
pbl.show_source(src)

if True:
print 'std'
src = pbl.PlaylistSource('trap music', uri = 'spotify:user:spotify:playlist:4Ha7Qja6HY3AgvNBgWz87p')
pbl.show_source(src)

src = pbl.PlaylistSource('trap music', uri = 'spotify:user:spotify:playlist:4Ha7Qja6HY3AgvNBgWz87p')
src = TextFilter(src, 'mix', True, False)
pbl.show_source(src)

src = pbl.PlaylistSource('trap music', uri = 'spotify:user:spotify:playlist:4Ha7Qja6HY3AgvNBgWz87p')
src = TextFilter(src, '^M', True, False)
pbl.show_source(src)

src = pbl.PlaylistSource('trap music', uri = 'spotify:user:spotify:playlist:4Ha7Qja6HY3AgvNBgWz87p')
src = TextFilter(src, 'mix|the', True, False)
pbl.show_source(src)

src = pbl.PlaylistSource('trap music', uri = 'spotify:user:spotify:playlist:4Ha7Qja6HY3AgvNBgWz87p')
src = TextFilter(src, '-', True, False)
pbl.show_source(src)
25 changes: 23 additions & 2 deletions web/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,21 @@ var createEditor = function(canvasElem, inventory, types, isReadOnly) {
function makeSub(word, component) {
var key = word.replace('$', '');
var showNegBool = false;
if (key.indexOf('!!') == 0) {
var flexiState = false;
var trueVal = '';
var falseVal = '';
if (key.indexOf('?') == 0) {
//?val:true-text:false-text
var vals = key.split(':')
if (vals.length == 3) {
trueVal = vals[1].replace('-', ' ')
falseVal = vals[2].replace('-', ' ')
key = vals[0].replace('?', '')
flexiState = true;
console.log('flexi', key, trueVal, falseVal);
}
}
else if (key.indexOf('!!') == 0) {
showNegBool = true;
key = key.replace('!!', '');
}
Expand All @@ -794,7 +808,14 @@ var createEditor = function(canvasElem, inventory, types, isReadOnly) {
key = findTypeName(component.params[key], types[keyType]);
return key;
} else if (keyType == 'bool') {
if (component.params[key]) {
if (flexiState) {
if (component.params[key]) {
return trueVal;
} else {
return falseVal;
}
}
else if (component.params[key]) {
return key;
} else {
return showNegBool ? "not " + key : "";
Expand Down

0 comments on commit 764ed7b

Please sign in to comment.