This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
weatherbot [2014/08/30 10:37] sekula [Source Code] |
weatherbot [2014/08/30 23:23] (current) sekula [Talking to WeatherBot] |
||
---|---|---|---|
Line 45: | Line 45: | ||
import subprocess | import subprocess | ||
import os | import os | ||
+ | import unicodedata | ||
from datetime import datetime | from datetime import datetime | ||
Line 122: | Line 123: | ||
def CelsiusToFarenheit(temp_celsius): | def CelsiusToFarenheit(temp_celsius): | ||
return temp_celsius*1.8 + 32.0 | return temp_celsius*1.8 + 32.0 | ||
+ | |||
+ | def FarenheitToCelsius(temp_farenheit): | ||
+ | return (temp_farenheit - 32.0)/1.8 | ||
def KPHToMPH(speed_kph): | def KPHToMPH(speed_kph): | ||
Line 135: | Line 139: | ||
wind_speed_kmh = weather_data['wind']['speed'] | wind_speed_kmh = weather_data['wind']['speed'] | ||
wind_speed_mph = KPHToMPH(wind_speed_kmh) | wind_speed_mph = KPHToMPH(wind_speed_kmh) | ||
+ | | ||
+ | heat_index_farenheit = -42.379 + 2.04901523*temp_farenheit + 10.14333127*humidity - .22475541*temp_farenheit*humidity - .00683783*temp_farenheit*temp_farenheit - .05481717*humidity*humidity + .00122874*temp_farenheit*temp_farenheit*humidity + .00085282*temp_farenheit*humidity*humidity - .00000199*temp_farenheit*temp_farenheit*humidity*humidity | ||
+ | heat_index_celsius = FarenheitToCelsius(heat_index_farenheit) | ||
| | ||
response = "" | response = "" | ||
Line 143: | Line 150: | ||
response += "<li> Temperature: %0.1fC (%0.1fF) </li>\n" % (temp_celsius, temp_farenheit) | response += "<li> Temperature: %0.1fC (%0.1fF) </li>\n" % (temp_celsius, temp_farenheit) | ||
response += "<li> Humidity: %0.1f%% </li> \n" % (humidity) | response += "<li> Humidity: %0.1f%% </li> \n" % (humidity) | ||
+ | response += "<li> Heat Index: %0.1fC (%0.1fF) </li>\n" % (heat_index_celsius,heat_index_farenheit) | ||
response += "<li> Wind Speed: %.1fkm/h (%.1fmph) </li>\n" % (wind_speed_kmh,wind_speed_mph) | response += "<li> Wind Speed: %.1fkm/h (%.1fmph) </li>\n" % (wind_speed_kmh,wind_speed_mph) | ||
response += "<li> Conditions: %s - %s</li> \n" % (weather_data['weather'][0]['main'], weather_data['weather'][0]['description']) | response += "<li> Conditions: %s - %s</li> \n" % (weather_data['weather'][0]['main'], weather_data['weather'][0]['description']) | ||
Line 276: | Line 284: | ||
pass | pass | ||
| | ||
- | print "%s sent a weather request for the %s for %s, which I will now process..." % (activity.obj.author, query_type, cityrequest) | + | author = "" |
+ | if type(activity.obj.author) == Person: | ||
+ | author = activity.obj.author.display_name | ||
+ | elif type(activity.obj.author) == unicode: | ||
+ | author = unicodedata.normalize('NFKD', activity.obj.author).encode('ascii','ignore') | ||
+ | else: | ||
+ | author = activity.obj.author | ||
+ | pass | ||
+ | |||
+ | print "%s sent a weather request for the %s for %s, which I will now process..." % (author, query_type, cityrequest) | ||
| | ||
# clean up poor formatting in content | # clean up poor formatting in content | ||
Line 386: | Line 403: | ||
</code> | </code> | ||
+ | ===== Talking to WeatherBot ===== | ||
+ | |||
+ | WeatherBot (e.g. [[https://hub.polari.us/weather]]) can now take various request forms. Sending these: | ||
+ | |||
+ | <code> | ||
+ | New York,NY,US | ||
+ | or | ||
+ | Stockholm,SE | ||
+ | </code> | ||
+ | |||
+ | results in the original behavior - you get the current conditions. | ||
+ | |||
+ | Sending any of these: | ||
+ | |||
+ | <code> | ||
+ | Current conditions in Jefferson City,MO,US | ||
+ | Currently Uppsala,SE | ||
+ | Current in London,UK | ||
+ | Conditions for Moscow,RU | ||
+ | etc. | ||
+ | </code> | ||
+ | |||
+ | also results in the current conditions. | ||
+ | |||
+ | But sending any of these: | ||
+ | |||
+ | <code> | ||
+ | Forecast for London,UK | ||
+ | Forecast in Dallas,TX,US | ||
+ | </code> | ||
+ | |||
+ | will return a table of forecast data for the city in question, going out 3 days. This is just a first attempt to do simple request processing based on language, and, yes, it's only in English for now. But now you can at least get a forecast! | ||
+ | |||
+ | {{ :weatherbot_forecast.png?direct&300 |}} | ||