/usr/share/pyshared/gplugs/weather.py is in gozerbot 0.99.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """ show weather based on Google's weather API """
__copyright__ = 'this file is in the public domain'
__author__ = 'Landon Fowles'
from gozerbot.generic import geturl, waitforuser, getwho, rlog
from gozerbot.commands import cmnds
from gozerbot.datadir import datadir
from gozerbot.persist.persist import Persist
from gozerbot.users import users
from gozerbot.plughelp import plughelp
from gozerbot.examples import examples
from gozerbot.persist.persiststate import UserState
from gozerbot.aliases import aliasset
import time
from xml.dom import minidom
from urllib import urlencode
plughelp.add('weather', 'show weather by zipcode/city name')
def handle_weather(bot, ievent):
""" show weather using Google's weather API """
userhost = ""
loc = ""
try:
nick = ievent.options['--u']
if nick:
userhost = getwho(bot, nick)
if not userhost:
ievent.reply("can't determine username for %s" % nick)
return
else:
try:
name = users.getname(userhost)
if not name:
ievent.reply("%s is not known with the bot" % nick)
return
us = UserState(name)
loc = us['location']
except KeyError:
ievent.reply("%s doesn't have his location set in \
userstate" % nick)
return
except KeyError:
pass
if not loc:
if ievent.rest:
loc = ievent.rest
else:
ievent.missing('--u <nick> or <location>')
return
query = urlencode({'weather':loc})
weathertxt = geturl('http://www.google.ca/ig/api?%s' % query)
if 'problem_cause' in weathertxt:
rlog(10, 'weather', 'ERROR: %s' % weathertxt)
ievent.reply('an error occured looking up data for %s' % loc)
return
resultstr = ""
if len(weathertxt) > 135:
gweather = minidom.parseString(weathertxt)
gweather = gweather.getElementsByTagName('weather')[0]
if ievent.command == "weather":
info = gweather.getElementsByTagName('forecast_information')[0]
if info:
city = info.getElementsByTagName('city')[0].attributes["data"].value
zip = info.getElementsByTagName('postal_code')[0].attributes["data"].value
time = info.getElementsByTagName('current_date_time')[0].attributes["data"].value
weather = gweather.getElementsByTagName('current_conditions')[0]
condition = weather.getElementsByTagName('condition')[0].attributes["data"].value
temp_f = weather.getElementsByTagName('temp_f')[0].attributes["data"].value
temp_c = weather.getElementsByTagName('temp_c')[0].attributes["data"].value
humidity = weather.getElementsByTagName('humidity')[0].attributes["data"].value
wind = weather.getElementsByTagName('wind_condition')[0].attributes["data"].value
try:
wind_km = round(int(wind[-6:-4]) * 1.609344)
except ValueError:
wind_km = ""
if (not condition == ""):
condition = " Oh, and it's " + condition + "."
resultstr = "As of %s, %s (%s) has a temperature of %sC/%sF with %s. %s (%s km/h).%s" % (time, city, zip, temp_c, temp_f, humidity, wind, wind_km, condition)
elif ievent.command == "forecast":
forecasts = gweather.getElementsByTagName('forecast_conditions')
for forecast in forecasts:
condition = forecast.getElementsByTagName('condition')[0].attributes["data"].value
low_f = forecast.getElementsByTagName('low')[0].attributes["data"].value
high_f = forecast.getElementsByTagName('high')[0].attributes["data"].value
day = forecast.getElementsByTagName('day_of_week')[0].attributes["data"].value
low_c = round((int(low_f) - 32) * 5.0 / 9.0)
high_c = round((int(high_f) - 32) * 5.0 / 9.0)
resultstr += "[%s: F(%sl/%sh) C(%sl/%sh) %s]" % (day, low_f, high_f, low_c, high_c, condition)
if not resultstr:
ievent.reply('%s not found!' % loc)
return
else:
ievent.reply(resultstr)
cmnds.add('weather', handle_weather, 'USER', options={'--u': ''})
aliasset('forecast', 'weather')
examples.add('weather', 'get weather for <LOCATION> or <nick>', '1) weather London, \
England 2) weather dunker')
|