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 stats(self, connection, user): cursor = self.dbconnection.cursor() # I am Lampstand. # I have been given questionable definitions of x things, mostly by x cursor.execute('select count(*) as `total` from `define`') total = cursor.fetchone() cursor.execute( 'select count(*), author from define group by author order by count(*) DESC limit 3' ) topuser = ["A", "B", "C"] topuser[0] = cursor.fetchone()[1] topuser[1] = cursor.fetchone()[1] topuser[2] = cursor.fetchone()[1] connection.msg( user, "I have been given questionable definitions of %s things, mostly by %s, %s & %s" % (total[0], topuser[0], topuser[1], topuser[2])) for module in connection.channelModules: if module.__name == "Items & Inventory": items = module inventory = len(items.items) cursor.execute('select count(*) as `total` from `item`') itemone = cursor.fetchone() connection.msg( user, "I am holding %s things, and know about %s items in total" % (inventory, itemone[0])) # There are X events in the future (X in the past), totaling X hours of things happening. # I was created X, and I've been up since X, which was X created = datetime(2008, 3, 7, 14, 00) now = datetime.now() creatediff = tools.nicedelta(now - created) launchdiff = tools.nicedelta(now - connection.date_started) connection.msg( user, "I was created %s ago, and I've been up since %s, which was %s ago" % (creatediff, connection.date_started, launchdiff)) # I'm listening out for X different public channel responses, X private channeltriggers = 0 for mod in connection.channelModules: print "Chan %s" % mod if hasattr(mod, "channelMatch"): if (isinstance(mod.channelMatch, tuple) or isinstance(mod.channelMatch, list)): channeltriggers += len(mod.channelMatch) else: channeltriggers += 1 privatetriggers = 0 for mod in connection.privateModules: if hasattr(mod, "privateMatch"): if isinstance(mod.privateMatch, tuple) or isinstance( mod.privateMatch, list): privatetriggers += len(mod.privateMatch) else: privatetriggers += 1 connection.msg( user, "I'm listening out for %d different public channel phrases, %d in query" % (channeltriggers, privatetriggers))
def stats(self, connection, user): cursor = self.dbconnection.cursor() # I am Lampstand. # I have been given questionable definitions of x things, mostly by x cursor.execute('select count(*) as `total` from `define`'); total = cursor.fetchone(); cursor.execute('select count(*), author from define group by author order by count(*) DESC limit 3'); topuser = ["A", "B", "C"]; topuser[0] = cursor.fetchone()[1]; topuser[1] = cursor.fetchone()[1]; topuser[2] = cursor.fetchone()[1]; connection.msg(user, "I have been given questionable definitions of %s things, mostly by %s, %s & %s" % (total[0], topuser[0], topuser[1], topuser[2])); for module in connection.channelModules: if module.__name == "Items & Inventory": items = module; inventory = len(items.items) cursor.execute('select count(*) as `total` from `item`'); itemone = cursor.fetchone(); connection.msg(user, "I am holding %s things, and know about %s items in total" % (inventory, itemone[0])); # There are X events in the future (X in the past), totaling X hours of things happening. # I was created X, and I've been up since X, which was X created = datetime(2008,3,7, 14, 00) now = datetime.now(); creatediff = tools.nicedelta(now-created) launchdiff = tools.nicedelta(now-connection.date_started) connection.msg(user, "I was created %s ago, and I've been up since %s, which was %s ago" % (creatediff, connection.date_started, launchdiff)); # I'm listening out for X different public channel responses, X private channeltriggers = 0 for mod in connection.channelModules: print "Chan %s" % mod if hasattr(mod, "channelMatch"): if (isinstance(mod.channelMatch, tuple) or isinstance(mod.channelMatch, list)): channeltriggers += len(mod.channelMatch) else: channeltriggers += 1 privatetriggers = 0 for mod in connection.privateModules: if hasattr(mod, "privateMatch"): if isinstance(mod.privateMatch, tuple) or isinstance(mod.privateMatch, list): privatetriggers += len(mod.privateMatch) else: privatetriggers += 1 connection.msg(user, "I'm listening out for %d different public channel phrases, %d in query" % (channeltriggers, privatetriggers))
def howlong(self, desc, output, direction): request = requests.get( 'http://api.larp.me/events/all?direction=%s&q=%s&count=1' % (direction, desc)) response = request.json() print response if not len(response['events']): try: result = dateutil.parser.parse(desc) event = (result, desc, "The date", None, None) print result except TypeError: 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://larp.me/events" % ( desc, direction) event = response['events'][response['events'].keys()[0]] event_start = dateutil.parser.parse(event['starts']) event_desc = event['name'] event_class = event['system']['name'] event_end = dateutil.parser.parse(event['ends']) event_url = event['website'] #(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 # event_start = event[0] # event_desc = event[1] # event_class = event[2] # event_end = event[3] # event_url = event[4] # Deltas now = datetime.now() if direction == "past" and event_end: deltapoint = event_end else: deltapoint = event_start if direction == "past": delta = now - deltapoint else: delta = deltapoint - now deltastring = tools.nicedelta(delta) if direction == "past": return "%s: %s was %s ago" % (event_class, event_desc, deltastring) else: return "%s: %s is in %s" % (event_class, event_desc, deltastring)
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 findEvents(self, desc, output, direction): request = requests.get( 'http://api.larp.me/events/all?direction=%s&q=%s' % (direction, desc)) response = request.json() self.logger.info('http://api.larp.me/events/all?direction=%s&q=%s' % (direction, desc)) if not len(response['events']): return "I can't see any events tagged '%s' in the %s. Full list of events at http://larp.me/events" % (desc, direction) now = datetime.now() response_events = response['events']; events = [] len_events = len(response_events) if len_events > 5: response_events = {} key = 0 key_list = response['events'].keys() key_list = [int(x) for x in key_list] key_list.sort() key_list = [unicode(x) for x in key_list] if direction == "past": key_list.reverse() while len(response_events) < 5: ident = key_list[key] response_events[ident] = response['events'][ident] key = key + 1 suffix = " (1-5 of {}. More at http://larp.me/events/all?direction={}&q={}".format(len_events, direction, desc) else: suffix = "" key_list = response_events.keys() key_list.reverse() for ident in key_list: event = response_events[ident] event_start = dateutil.parser.parse(event['starts']) event_desc = event['name'] event_class = event['system']['name'] event_end = dateutil.parser.parse(event['ends']) event_url = event['website'] if direction == "past" and event_end: deltapoint = event_end else: deltapoint = event_start if direction == "past": delta = now - deltapoint else: delta = deltapoint - now deltastring = tools.nicedelta(delta) if direction == "past": events.append("%s: %s was %s ago" % (event_class, event_desc, deltastring)) else: events.append("%s: %s is in %s" % (event_class, event_desc, deltastring)) if len_events == 1: return events[0] else: return "{} & {}{}".format("; ".join(events[0:-1]), events[-1], suffix)
def howLong(self, desc, output, direction): request = requests.get( 'http://api.larp.me/events/all?direction=%s&q=%s&count=1' % (direction, desc)) response = request.json() self.logger.info(response) if not len(response['events']): try: result = dateutil.parser.parse(desc) event = { 'starts': desc, 'ends': desc, 'name': desc, 'system': { 'name': 'Date' }, 'website': None } self.logger.info(result) 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://larp.me/events" % ( desc, direction) else: event = response['events'][response['events'].keys()[0]] event_start = dateutil.parser.parse(event['starts']) event_desc = event['name'] event_class = event['system']['name'] event_end = dateutil.parser.parse(event['ends']) event_url = event['website'] #(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 # event_start = event[0] # event_desc = event[1] # event_class = event[2] # event_end = event[3] # event_url = event[4] # Deltas now = datetime.now() if direction == "past" and event_end: deltapoint = event_end else: deltapoint = event_start if direction == "past": delta = now - deltapoint else: delta = deltapoint - now deltastring = tools.nicedelta(delta) if direction == "past": return "%s: %s was %s ago" % (event_class, event_desc, deltastring) else: return "%s: %s is in %s" % (event_class, event_desc, deltastring)
def findEvents(self, desc, output, direction): request = requests.get( 'http://api.larp.me/events/all?direction=%s&q=%s' % (direction, desc)) response = request.json() self.logger.info('http://api.larp.me/events/all?direction=%s&q=%s' % (direction, desc)) if not len(response['events']): return "I can't see any events tagged '%s' in the %s. Full list of events at http://larp.me/events" % ( desc, direction) now = datetime.now() response_events = response['events'] events = [] len_events = len(response_events) if len_events > 5: response_events = {} key = 0 key_list = response['events'].keys() key_list = [int(x) for x in key_list] key_list.sort() key_list = [unicode(x) for x in key_list] if direction == "past": key_list.reverse() while len(response_events) < 5: ident = key_list[key] response_events[ident] = response['events'][ident] key = key + 1 suffix = " (1-5 of {}. More at http://larp.me/events/all?direction={}&q={}".format( len_events, direction, desc) else: suffix = "" key_list = response_events.keys() key_list.reverse() for ident in key_list: event = response_events[ident] event_start = dateutil.parser.parse(event['starts']) event_desc = event['name'] event_class = event['system']['name'] event_end = dateutil.parser.parse(event['ends']) event_url = event['website'] if direction == "past" and event_end: deltapoint = event_end else: deltapoint = event_start if direction == "past": delta = now - deltapoint else: delta = deltapoint - now deltastring = tools.nicedelta(delta) if direction == "past": events.append("%s: %s was %s ago" % (event_class, event_desc, deltastring)) else: events.append("%s: %s is in %s" % (event_class, event_desc, deltastring)) if len_events == 1: return events[0] else: return "{} & {}{}".format("; ".join(events[0:-1]), events[-1], suffix)