aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMikkelsv <mikkel.svagard@gmail.com>2017-12-04 21:05:54 +0100
committerMikkelsv <mikkel.svagard@gmail.com>2017-12-04 21:05:54 +0100
commit68575528d2b74eb401265b7f72092241fd572914 (patch)
tree09e0f6c1252787dd0f09bc7c372e291933a41e24
parent7db59deffff7e70d22430e8c16ec8984bd4f2bfe (diff)
downloadsaitama-68575528d2b74eb401265b7f72092241fd572914.tar.gz
Fix repeating message and some remind me parsing
-rw-r--r--Message_parser.py36
-rw-r--r--timeEventQueuer.py47
2 files changed, 55 insertions, 28 deletions
diff --git a/Message_parser.py b/Message_parser.py
index ac52c8d..4542353 100644
--- a/Message_parser.py
+++ b/Message_parser.py
@@ -40,7 +40,19 @@ class Message_parser():
elif text.startswith('!repeat'):
self.parseRepeat(message_data)
- def get_message_time(self,i,c):
+ #Returns timedelta object from text on format ?w?d?h?m?s
+ def get_time_delta_from_text(self,text):
+ time_delta = timedelta(seconds=0)
+ number = "0"
+ for c in text:
+ if(c.isdigit()):
+ number += c
+ elif(c in TIME_KEYES):
+ time_delta += self.get_character_time(number,c)
+ number = "0"
+ return time_delta
+
+ def get_character_time(self,i,c):
if c == 's':
return timedelta(seconds = int(i))
elif c == 'm':
@@ -53,19 +65,18 @@ class Message_parser():
def parseRemindMe(self,message_data):
text = message_data.get_text().split()
- text_time = text[1]
- time_delta = timedelta(seconds=0)
- number = ""
- for c in text_time:
- if(c.isdigit()):
- number += c
- if(c in TIME_KEYES):
- time_delta += self.get_message_time(number,c)
- number = ""
+ if(len(text)>1):
+ text_time = text[1]
+ time_delta = self.get_time_delta_from_text(text_time)
+ else:
+ message_data.set_text("Please Specify Time: !rm ?w?d?h?m?s")
+ Fb_client.post_message_event(self.eb,message_data)
+ return
text_message = "REMINDER"
if(len(text) > 2):
text_message += ": " + ' '.join(text[2:])
+
message_data.set_text(text_message)
send_message_event = event(EVENTID_CLIENT_SEND,message_data)
Time_event_queuer.post_time_event(self.eb,send_message_event,time_delta)
@@ -89,8 +100,9 @@ class Message_parser():
send_message_event = event(EVENTID_CLIENT_SEND,message_data)
start_time = datetime.now() + timedelta(seconds=5)
repeat_delta = timedelta(seconds=5)
- repeat_event = Repeating_event_object(send_message_event,start_time,repeat_delta)
- Time_event_queuer.post_repeating_event(self.eb,repeat_event)
+ repeat_event = Repeating_event_object(self.eb, send_message_event,
+ start_time, repeat_delta)
+ repeat_event.queue()
diff --git a/timeEventQueuer.py b/timeEventQueuer.py
index e89d2f4..99af94b 100644
--- a/timeEventQueuer.py
+++ b/timeEventQueuer.py
@@ -42,7 +42,8 @@ class Time_event_queuer(threading.Thread,subscriber):
repeating_event = new_event.get_data()
self.eb.post(repeating_event.get_event())
print("POSTING NEW TIME EVEEENT")
- Time_event_queuer.post_repeating_event(self.eb,repeating_event)
+ repeating_event.queue()
+
else:
self.eb.post(e[1])
@@ -145,25 +146,39 @@ class Time_event_queuer(threading.Thread,subscriber):
data = (event_time,data)
eb.post(event(EVENTID_TIME,data))
- @staticmethod
- def post_repeating_event(eb,repeating_event_object):
- print("Posting REPEATING event")
- event_time = repeating_event_object.occurence_start
- now = datetime.now()
- while(now > event_time):
- event_time += repeating_event_object.repeat_delta
- delta_time = event_time - datetime.now()
- Time_event_queuer.post_time_event(eb,event(EVENTID_REPEATING,repeating_event_object),
- delta_time)
+"""
+Repeating Event repeats itself on set intervals
+eb - to post the events on
+event2execute - event that is to be executed, could be send message or other.
+occurence_time - start time of first event
+repeat_time - delta between repeats: defaults to two seconds
+n - number of repeats: defaults to 5
+
+to execute object do object.queue()
+"""
class Repeating_event_object():
- def __init__(self,event,occurence_start,repeat_delta):
- self.event = event #Event to be posted
- self.occurence_start = occurence_start
- self.repeat_delta = repeat_delta
+ def __init__(self,eb,event2execute,occurence_time,repeat_time=timedelta(seconds=2),n=5):
+ self.eb = eb
+ self.event2execute = event2execute #Event to be posted
+ self.occurence_time = occurence_time
+ self.repeat_time = repeat_time
+ self.n = n #number of repeats
- def get_event(self): return self.event
+ def get_event(self): return self.event2execute
+ def queue(self):
+ if(self.n==0):
+ print("RepeatingEvent Finished")
+ return
+ self.n-=1
+ print("Posting REPEATING event")
+ now = datetime.now()
+ while(now > self.occurence_time):
+ self.occurence_time += self.repeat_time
+ delta_time = self.occurence_time - datetime.now()
+ time_event = event( EVENTID_REPEATING, self)
+ Time_event_queuer.post_time_event(self.eb, time_event, delta_time)
class Thread_handler():