This repository has been archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
events.py
187 lines (132 loc) · 4.84 KB
/
events.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import lampstand.reactions.base
from lampstand.tools import splitAt
import re, random, sys
from lampstand import tools
from datetime import datetime
import dateutil.parser
def __init__ ():
pass
class Reaction(lampstand.reactions.base.Reaction):
__name = 'Events'
cooldown_number = 3
cooldown_time = 360 # So if 3 requests are made in 360 seconds, it will trigger overuse.
uses = []
def __init__(self, connection):
self.channelMatch = (
re.compile('^%s. when (is|was) (.*?)\?' % connection.nickname, re.IGNORECASE),
re.compile('^%s. how long (until|since) (.*?)\?' % connection.nickname, re.IGNORECASE),
re.compile('^%s. what\'s next\?$' % connection.nickname, re.IGNORECASE)
)
self.privateMatch = (
re.compile('^when (is|was) (.*?)\?', re.IGNORECASE),
re.compile('^how long (until|since) (.*?)\?', re.IGNORECASE),
re.compile('^what\'s next\?$', re.IGNORECASE)
)
#self.privateMatch = re.compile('^%s. ???' % connection.nickname, re.IGNORECASE))
self.dbconnection = connection.dbconnection
def privateAction(self, connection, user, channel, message, matchIndex):
matches = self.privateMatch[matchIndex].findall(message)[0]
if matchIndex == 0:
desc = matches[1]
output = "absolute"
if matches[0].lower() == "is":
direction = "future"
else:
direction = "past"
elif matchIndex == 1:
desc = matches[1]
output = "relative"
if matches[0].lower() == "until":
direction = "future"
else:
direction = "past"
elif matchIndex == 2: # What's next
desc = ""
output = "relative"
direction = "future"
message = self.howlong(desc, output, direction)
connection.message(user, message)
def channelAction(self, connection, user, channel, message, matchIndex):
if self.overUsed():
connection.message(user, "Overuse Triggered" )
return True
matches = self.channelMatch[matchIndex].findall(message)[0]
if matchIndex == 0:
desc = matches[1]
output = "absolute"
if matches[0].lower() == "is":
direction = "future"
else:
direction = "past"
elif matchIndex == 1:
desc = matches[1]
output = "relative"
if matches[0].lower() == "until":
direction = "future"
else:
direction = "past"
elif matchIndex == 2: # What's next
desc = ""
output = "relative"
direction = "future"
message = self.howlong(desc, output, direction)
connection.message(channel, "%s: %s" % (user, message))
def howlong(self, desc, output, direction):
cursor = self.dbconnection.cursor()
if direction == "future":
gtlt = ">"
order = "asc"
else:
gtlt = "<"
order = "desc"
# First, try direct description matches. First in the future (past if it's since)...
query = 'SELECT datetime, description, class, datetime_end, url FROM events WHERE (description LIKE %s or aliases LIKE %s or class like %s) and datetime '+gtlt+' now() ORDER BY datetime '+order
likedesc = "%%%s%%" % desc
cursor.execute(query, (likedesc, likedesc, desc) )
event = cursor.fetchone()
if event == None:
try:
result = dateutil.parser.parse(desc)
event = (result,desc,"The date",None,None)
except ValueError:
return "I can't see any events tagged '%s' in the %s, and it doesn't look like a date. Full list of events at http://www.maelfroth.org/events.php" % (desc, direction)
event_start = event[0]
event_desc = event[1]
event_class = event[2]
event_end = event[3]
event_url = event[4]
#(datetime.datetime(2012, 6, 29, 18, 0), 'Crown of the Sphinx', 'Odyssey', datetime.datetime(2012, 7, 1,
# datetime, description, class, datetime_end FROM events
if output == "absolute":
if direction == "past":
tiswas = "was"
else:
tiswas = "is"
timeformat = "%A %d %B %Y at %H:%M";
if (event_end):
message = "%s: %s %s from %s to %s" % (event_class, event_desc, tiswas, event_start.strftime(timeformat), event_end.strftime(timeformat))
else:
message = "%s: %s %s on %s" % (event_class, event_desc, tiswas, event_start.strftime(timeformat))
if (event_url):
message += ", More info at %s" % event_url
return message
###### Deltas
now = datetime.now();
if direction == "past" and event[3]:
deltapoint = event[3]
else:
deltapoint = event[0]
if direction == "past":
delta = now - deltapoint;
else:
delta = deltapoint - now;
deltastring = tools.nicedelta(delta)
if direction == "past":
return "%s: %s was %s ago" % (event[2], event[1], deltastring )
else:
return "%s: %s is in %s" % (event[2], event[1], deltastring )
#def everyLine(self, connection, user, channel, message)
#def leaveAction(self, connection, user, reason, parameters)
#def nickChangeAction(self, connection, old_nick, new_nick)
#def privateAction(self, connection, user, channel, message, index)
#def scheduleAction(self, connection)