From aa15cd33b08483cf4140bec37e50e9afcca80fb2 Mon Sep 17 00:00:00 2001 From: Jacob Henry Date: Tue, 29 Nov 2016 23:51:34 +0000 Subject: [PATCH] Fixed various regular expression issues breaking functionality, as well as added better handling of newlines to the thread loop --- kong.py | 36 ++++++++++++++++++------------------ lyrics.txt | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/kong.py b/kong.py index 6bbad66..75a51cb 100644 --- a/kong.py +++ b/kong.py @@ -7,8 +7,8 @@ DEFAULT_SONG = "dk_rap_classic" #Patterns patterns = {} -patterns['start'] = re.compile(r"#STARTSONG\a+(.*?)") -patterns['time'] = re.compile(r"#TIMEPERLINE\a+(\d*)") +patterns['start'] = re.compile(r"#STARTSONG\s+(.*?)$") +patterns['time'] = re.compile(r"#TIMEPERLINE\s+(\d*)$") patterns['rest'] = re.compile(r"#REST") patterns['end'] = re.compile(r"#ENDSONG") @@ -21,27 +21,27 @@ f.close() class SongThread(threading.Thread): - def __init__(this, replycallback, song_name): - this.slack = slack - this.song_name = song_name - this.song_lines = song_lines - this.delay = 1000 - this.daemon = True + def __init__(self, callback, song_name): + super().__init__() + self.song_name = song_name + self.callback = callback + self.delay = 1000 + self.daemon = True - - def run(this): + def run(self): playing = False for line in lyric_lines: - print(line) + line = line.strip() if line == "": continue #Navigate to song start if not playing: m = patterns['start'].match(line) + print(line) if m: - if m.group(1) == this.song_name: + if m.group(1) == self.song_name: playing = True continue @@ -50,13 +50,13 @@ class SongThread(threading.Thread): #Config m = patterns['time'].match(line) if m: - this.delay = int(m.group(1)) + self.delay = int(m.group(1)) continue #Rest line m = patterns['rest'].match(line) if m: - sleep(this.delay / 1000) + sleep(self.delay / 1000) continue #End song @@ -65,11 +65,11 @@ class SongThread(threading.Thread): return #"sing" line - replycallback(line) - sleep(this.delay / 1000) + self.callback(line) + sleep(self.delay / 1000) if not playing: - replycallback("Could not find song") + self.callback("Could not find song") def getAllTitles(): titles = [] @@ -80,7 +80,7 @@ def getAllTitles(): return titles -request_pattern = re.compile("kong (.*?)$") +request_pattern = re.compile(r"kong\s*(.*?)$") def handleKongMsg(slack, msg): #Get text diff --git a/lyrics.txt b/lyrics.txt index d4b4716..bba4e77 100644 --- a/lyrics.txt +++ b/lyrics.txt @@ -66,4 +66,4 @@ C'mon Cranky, take it to the fridge! Walnuts, peanuts, pineapple smells Grapes, melons, oranges and coconut shells -#ENDSONG \ No newline at end of file +#ENDSONG