Added command MapQuest
Some minor fixes
This commit is contained in:
parent
f7289a1ac4
commit
c079093af9
2 changed files with 42 additions and 5 deletions
|
@ -85,8 +85,11 @@ ParserWaitTime = 0.05
|
||||||
#########################
|
#########################
|
||||||
|
|
||||||
[DuckDuckGo]
|
[DuckDuckGo]
|
||||||
Active = True
|
Active = 1
|
||||||
|
|
||||||
[Forecast.io]
|
[Forecast.io]
|
||||||
Active = False
|
Active = 0
|
||||||
ApiKey = your_api_key
|
ApiKey = your_api_key
|
||||||
|
|
||||||
|
[MapQuest]
|
||||||
|
Active = 1
|
||||||
|
|
40
main.py
40
main.py
|
@ -57,6 +57,7 @@ class pircbot():
|
||||||
users = "",
|
users = "",
|
||||||
duckduckgo_cfg = {"Active": "0"},
|
duckduckgo_cfg = {"Active": "0"},
|
||||||
forecast_cfg = {"Active": "0"},
|
forecast_cfg = {"Active": "0"},
|
||||||
|
mapquest_cfg = {"Active": "0"},
|
||||||
logger = logging.getLogger(logging.basicConfig())
|
logger = logging.getLogger(logging.basicConfig())
|
||||||
):
|
):
|
||||||
self.log = logger
|
self.log = logger
|
||||||
|
@ -83,6 +84,7 @@ class pircbot():
|
||||||
|
|
||||||
self.duckduckgo_cfg = duckduckgo_cfg
|
self.duckduckgo_cfg = duckduckgo_cfg
|
||||||
self.forecast_cfg = forecast_cfg
|
self.forecast_cfg = forecast_cfg
|
||||||
|
self.mapquest_cfg = mapquest_cfg
|
||||||
|
|
||||||
|
|
||||||
self.user = {"mask":"", "nick":"", "ident":"", "host":""}
|
self.user = {"mask":"", "nick":"", "ident":"", "host":""}
|
||||||
|
@ -271,11 +273,12 @@ class pircbot():
|
||||||
|
|
||||||
|
|
||||||
# checks if a given user may execute a given command
|
# 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():
|
for user, privs in self.users.items():
|
||||||
if re.search(user, usermask, re.IGNORECASE) != None:
|
if re.search(user, usermask, re.IGNORECASE) != None:
|
||||||
if command.lower() in privs or "*" in privs:
|
if command.lower() in privs or "*" in privs:
|
||||||
return True
|
return True
|
||||||
|
if log: self.log.info("Unauthorized call of %s from user %s", command, usermask)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@ -369,6 +372,8 @@ class pircbot():
|
||||||
Command is the command for the bot.
|
Command is the command for the bot.
|
||||||
Params contains a list of originally space separated parameters.
|
Params contains a list of originally space separated parameters.
|
||||||
"""
|
"""
|
||||||
|
self.log.info("Command from %s: %s", origin["mask"], command)
|
||||||
|
|
||||||
numparams = len(params)
|
numparams = len(params)
|
||||||
### INTERNAL ###
|
### INTERNAL ###
|
||||||
# help [me]
|
# help [me]
|
||||||
|
@ -390,7 +395,7 @@ class pircbot():
|
||||||
self.cmdprefix,
|
self.cmdprefix,
|
||||||
self.user["nick"]
|
self.user["nick"]
|
||||||
)
|
)
|
||||||
if self.check_privileges(origin["mask"], command):
|
if self.check_privileges(origin["mask"], command, log=False):
|
||||||
rply += " \n\
|
rply += " \n\
|
||||||
~ For the aristocrats ~\n\
|
~ For the aristocrats ~\n\
|
||||||
join <channel>\n\
|
join <channel>\n\
|
||||||
|
@ -468,7 +473,7 @@ class pircbot():
|
||||||
self.reply(origin, source,
|
self.reply(origin, source,
|
||||||
"Wind Speed: %s m/s" % rj["currently"]["windSpeed"], in_query_type)
|
"Wind Speed: %s m/s" % rj["currently"]["windSpeed"], in_query_type)
|
||||||
self.reply(origin, source,
|
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,
|
self.reply(origin, source,
|
||||||
"Precipitation Probability: %s %%" % (rj["currently"]["precipProbability"]*100), in_query_type)
|
"Precipitation Probability: %s %%" % (rj["currently"]["precipProbability"]*100), in_query_type)
|
||||||
if "precipIntensity" in rj["currently"]: self.reply(origin, source,
|
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()
|
return "Error while querying Forecast.io, got HTTP-Status %i" % rp.getcode()
|
||||||
else:
|
else:
|
||||||
return "Usage: %s <lat> <lon>" % command
|
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 ###
|
### IRC ###
|
||||||
# join <channel>
|
# join <channel>
|
||||||
|
@ -580,6 +612,7 @@ def main():
|
||||||
users = cfg["Permissions"],
|
users = cfg["Permissions"],
|
||||||
duckduckgo_cfg = cfg["DuckDuckGo"],
|
duckduckgo_cfg = cfg["DuckDuckGo"],
|
||||||
forecast_cfg = cfg["Forecast.io"],
|
forecast_cfg = cfg["Forecast.io"],
|
||||||
|
mapquest_cfg = cfg["MapQuest"],
|
||||||
logger = log,
|
logger = log,
|
||||||
)
|
)
|
||||||
bot.connect()
|
bot.connect()
|
||||||
|
@ -601,6 +634,7 @@ def main():
|
||||||
bot.disconnect("Ouch! Got shot by Ctrl-C, dying now... See you!")
|
bot.disconnect("Ouch! Got shot by Ctrl-C, dying now... See you!")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception("Fehler: %s", e)
|
log.exception("Fehler: %s", e)
|
||||||
|
bot.disconnect("Fehler: %s" % e)
|
||||||
log.debug("--- MAIN EXITING ---")
|
log.debug("--- MAIN EXITING ---")
|
||||||
elif args.action == "stop": print("nope!")
|
elif args.action == "stop": print("nope!")
|
||||||
elif args.action == "checkconf":
|
elif args.action == "checkconf":
|
||||||
|
|
Loading…
Reference in a new issue