1
0
Fork 0

Added command MapQuest

Some minor fixes
This commit is contained in:
fanir 2014-03-19 10:20:23 +01:00
parent f7289a1ac4
commit c079093af9
2 changed files with 42 additions and 5 deletions

View File

@ -85,8 +85,11 @@ ParserWaitTime = 0.05
#########################
[DuckDuckGo]
Active = True
Active = 1
[Forecast.io]
Active = False
Active = 0
ApiKey = your_api_key
[MapQuest]
Active = 1

40
main.py
View File

@ -57,6 +57,7 @@ class pircbot():
users = "",
duckduckgo_cfg = {"Active": "0"},
forecast_cfg = {"Active": "0"},
mapquest_cfg = {"Active": "0"},
logger = logging.getLogger(logging.basicConfig())
):
self.log = logger
@ -83,6 +84,7 @@ class pircbot():
self.duckduckgo_cfg = duckduckgo_cfg
self.forecast_cfg = forecast_cfg
self.mapquest_cfg = mapquest_cfg
self.user = {"mask":"", "nick":"", "ident":"", "host":""}
@ -271,11 +273,12 @@ class pircbot():
# checks if a given user may execute a given command
def check_privileges(self, usermask, command):
def check_privileges(self, usermask, command, log=True):
for user, privs in self.users.items():
if re.search(user, usermask, re.IGNORECASE) != None:
if command.lower() in privs or "*" in privs:
return True
if log: self.log.info("Unauthorized call of %s from user %s", command, usermask)
return False
@ -369,6 +372,8 @@ class pircbot():
Command is the command for the bot.
Params contains a list of originally space separated parameters.
"""
self.log.info("Command from %s: %s", origin["mask"], command)
numparams = len(params)
### INTERNAL ###
# help [me]
@ -390,7 +395,7 @@ class pircbot():
self.cmdprefix,
self.user["nick"]
)
if self.check_privileges(origin["mask"], command):
if self.check_privileges(origin["mask"], command, log=False):
rply += " \n\
~ For the aristocrats ~\n\
join <channel>\n\
@ -468,7 +473,7 @@ class pircbot():
self.reply(origin, source,
"Wind Speed: %s m/s" % rj["currently"]["windSpeed"], in_query_type)
self.reply(origin, source,
"Cloud Cover: %s %%" % rj["currently"]["cloudCover"], in_query_type)
"Cloud Cover: %s %%" % (rj["currently"]["cloudCover"]*100), in_query_type)
self.reply(origin, source,
"Precipitation Probability: %s %%" % (rj["currently"]["precipProbability"]*100), in_query_type)
if "precipIntensity" in rj["currently"]: self.reply(origin, source,
@ -492,6 +497,33 @@ class pircbot():
return "Error while querying Forecast.io, got HTTP-Status %i" % rp.getcode()
else:
return "Usage: %s <lat> <lon>" % command
# 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..."
try: rp = urlopen("http://open.mapquestapi.com/nominatim/v1/search?q=%s&format=json"
% quote_plus(" ".join(params)))
except Exception as e:
self.log.error("Error while querying MapQuest: %s" % e)
return "Error while querying MapQuest: %s" % e
if rp.getcode() == 200:
used_fields = (
"display_name",
"lat", "lon"
)
rj = json.loads(str(rp.readall(), "utf-8"))
if len(rj) == 0:
return "No suitable reply from MapQuest for query %s" % " ".join(params)
rj = rj[0]
self.reply(origin, source, "Display Name: %s" % rj["display_name"], in_query_type)
self.reply(origin, source, "Lat: %s" % rj["lat"], in_query_type)
self.reply(origin, source, "Lon: %s" % rj["lon"], in_query_type)
return "(Nominatim Search Courtesy of MapQuest <http://www.mapquest.com/>)"
else:
return "Error while querying MapQuest, got HTTP-Status %i" % rp.getcode()
### IRC ###
# join <channel>
@ -580,6 +612,7 @@ def main():
users = cfg["Permissions"],
duckduckgo_cfg = cfg["DuckDuckGo"],
forecast_cfg = cfg["Forecast.io"],
mapquest_cfg = cfg["MapQuest"],
logger = log,
)
bot.connect()
@ -601,6 +634,7 @@ def main():
bot.disconnect("Ouch! Got shot by Ctrl-C, dying now... See you!")
except Exception as e:
log.exception("Fehler: %s", e)
bot.disconnect("Fehler: %s" % e)
log.debug("--- MAIN EXITING ---")
elif args.action == "stop": print("nope!")
elif args.action == "checkconf":