From fc1a9a04d5cb5653275f23e9a97cf76a2bee59f3 Mon Sep 17 00:00:00 2001 From: Fanir Date: Fri, 21 Mar 2014 09:57:47 +0100 Subject: [PATCH] Added helper function msgsplit Added command fefe Added MapQuest and Fefe to .help Minor fix --- main.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index e0ff4e3..3abad1f 100755 --- a/main.py +++ b/main.py @@ -272,6 +272,11 @@ class pircbot(): return textstring.decode(self.encodings[0], 'ignore') + # splits messages into parts with a specific maximal length + def msgsplit(self, msg, length=400): + return [msg[i:i+length] for i in range(0, len(msg), length)] + + # checks if a given user may execute a given command def check_privileges(self, usermask, command, log=True): for user, privs in self.users.items(): @@ -391,6 +396,8 @@ class pircbot(): choose , [, [, ...]] -- Let the bot decide!\n\ DuckDuckGo, ddg -- Ask the DuckDuckGo Instant Answer API\n\ Forecast, fc -- Query Forecast.io\n\ + MapQuest, mq, OpenStreetMap, osm -- Get lat and lon for a place\n\ + Fefe [] -- Show a given or the lastest post in Fefes Blog\n\ " % ( self.cmdprefix, self.user["nick"] @@ -480,7 +487,7 @@ class pircbot(): "Precipitation Intensity: %s mm/h" % rj["currently"]["precipIntensity"], in_query_type) if "precipType" in rj["currently"]: self.reply(origin, source, "Precipitation Type: %s" % rj["currently"]["precipType"], in_query_type) - self.reply(origin, source, + if "visibility" in rj["currently"]: self.reply(origin, source, "Visibility: %s km" % rj["currently"]["visibility"], in_query_type) self.reply(origin, source, "Humidity: %s %%" % (rj["currently"]["humidity"]*100), in_query_type) @@ -497,7 +504,7 @@ class pircbot(): return "Error while querying Forecast.io, got HTTP-Status %i" % rp.getcode() else: return "Usage: %s " % command - # MapQuest, mq, OpenStreetMap, osm + # MapQuest, mq, OpenStreetMap, osm elif command in ("mapquest", "mq", "openstreetmap", "osm") and self.mapquest_cfg["Active"] == "1": if numparams==0: return "You didn't ask anything..." @@ -524,6 +531,33 @@ class pircbot(): return "(Nominatim Search Courtesy of MapQuest )" else: return "Error while querying MapQuest, got HTTP-Status %i" % rp.getcode() + # Fefe + elif command == "fefe": + if numparams>1: + return "Waddayawannasee? (The one and only optional argument needed is the ID of the blog post. If it is omitted, the last post will be shown.)" + else: + try: + if numparams==1: + rp = urlopen("http://blog.fefe.de/?ts=%s" % quote_plus(params[0])) + else: + rp = urlopen("http://blog.fefe.de/") + except Exception as e: + self.log.error("Error while querying Fefe: %s" % e) + return "Error while querying Fefe: %s" % e + if rp.getcode() == 200: + try: + rpd = str(rp.readall(), "utf-8") + m = re.search(r"
  • (.*?)<\/(?:li|ul)>", "".join(rpd.splitlines())).groups()[0] + print(m+"\n\n") + m = " ".join((re.split(r"<.+?>", m)))[6:] + if len(m)>400: + mmlen = 400 + for l in self.msgsplit(m): + print(l) + self.reply(origin, source, l, in_query_type) + except Exception as e: + self.log.warning("Suspectious things happend while handling a response from fefes blog, but it's probably just an incorrect blogpost-id: %s" % e) + return "Nothing matched... (If you're sure your id was correct, see error log)" ### IRC ### # join