Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

180 líneas
7.8 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from datetime import datetime
  23. from json import loads
  24. from urllib import quote
  25. from urllib2 import urlopen
  26. from earwigbot.commands import Command
  27. class Weather(Command):
  28. """Get a weather forecast (via http://www.wunderground.com/)."""
  29. name = "weather"
  30. commands = ["weather", "weat", "forecast", "temperature", "temp"]
  31. def setup(self):
  32. self.config.decrypt(self.config.commands, self.name, "apiKey")
  33. try:
  34. self.key = self.config.commands[self.name]["apiKey"]
  35. except KeyError:
  36. self.key = None
  37. addr = "http://wunderground.com/weather/api/"
  38. config = 'config.commands["{0}"]["apiKey"]'.format(self.name)
  39. log = "Cannot use without an API key from {0} stored as {1}"
  40. self.logger.warn(log.format(addr, config))
  41. def process(self, data):
  42. if not self.key:
  43. addr = "http://wunderground.com/weather/api/"
  44. config = 'config.commands["{0}"]["apiKey"]'.format(self.name)
  45. msg = "I need an API key from {0} stored as \x0303{1}\x0F."
  46. log = "Need an API key from {0} stored as {1}"
  47. self.reply(data, msg.format(addr, config))
  48. self.logger.error(log.format(addr, config))
  49. return
  50. permdb = self.config.irc["permissions"]
  51. if not data.args:
  52. if permdb.has_attr(data.host, "weather"):
  53. location = permdb.get_attr(data.host, "weather")
  54. else:
  55. msg = " ".join(("Where do you want the weather of? You can",
  56. "set a default with '!{0} default City,",
  57. "State' (or 'City, Country' if non-US)."))
  58. self.reply(data, msg.format(data.command))
  59. return
  60. elif data.args[0] == "default":
  61. if data.args[1:]:
  62. value = " ".join(data.args[1:])
  63. permdb.set_attr(data.host, "weather", value)
  64. msg = "\x0302{0}\x0F's default set to \x02{1}\x0F."
  65. self.reply(data, msg.format(data.host, value))
  66. else:
  67. if permdb.has_attr(data.host, "weather"):
  68. value = permdb.get_attr(data.host, "weather")
  69. msg = "\x0302{0}\x0F's default is \x02{1}\x0F."
  70. self.reply(data, msg.format(data.host, value))
  71. else:
  72. self.reply(data, "I need a value to set as your default.")
  73. return
  74. else:
  75. location = " ".join(data.args)
  76. url = "http://api.wunderground.com/api/{0}/conditions/astronomy/q/{1}.json"
  77. location = quote(location, safe="")
  78. query = urlopen(url.format(self.key, location)).read()
  79. res = loads(query)
  80. if "error" in res["response"]:
  81. try:
  82. desc = res["response"]["error"]["description"]
  83. desc = desc[0].upper() + desc[1:]
  84. if desc[-1] not in (".", "!", "?"):
  85. desc += "."
  86. except (KeyError, IndexError):
  87. desc = "An unknown error occurred."
  88. self.reply(data, desc)
  89. elif "current_observation" in res:
  90. msg = self.format_weather(res)
  91. self.reply(data, msg)
  92. elif "results" in res["response"]:
  93. msg = self.format_ambiguous_result(res)
  94. self.reply(data, msg)
  95. else:
  96. self.reply(data, "An unknown error occurred.")
  97. def format_weather(self, res):
  98. """Format the weather (as dict *data*) to be sent through IRC."""
  99. data = res["current_observation"]
  100. place = data["display_location"]["full"]
  101. icon = self.get_icon(data["icon"], data["local_time_rfc822"],
  102. res["sun_phase"])
  103. weather = data["weather"]
  104. temp_f, temp_c = data["temp_f"], data["temp_c"]
  105. humidity = data["relative_humidity"]
  106. wind_dir = data["wind_dir"]
  107. if wind_dir in ("North", "South", "East", "West"):
  108. wind_dir = wind_dir.lower()
  109. wind = "{0} {1} mph".format(wind_dir, data["wind_mph"])
  110. if float(data["wind_gust_mph"]) > float(data["wind_mph"]):
  111. wind += " ({0} mph gusts)".format(data["wind_gust_mph"])
  112. msg = "\x02{0}\x0F: {1} {2}; {3}°F ({4}°C); {5} humidity; wind {6}"
  113. msg = msg.format(place, icon, weather, temp_f, temp_c, humidity, wind)
  114. if data["precip_today_in"] and float(data["precip_today_in"]) > 0:
  115. msg += "; {0}″ precipitation today".format(data["precip_today_in"])
  116. if data["precip_1hr_in"] and float(data["precip_1hr_in"]) > 0:
  117. msg += " ({0}″ past hour)".format(data["precip_1hr_in"])
  118. return msg
  119. def get_icon(self, condition, local_time, sun_phase):
  120. """Return a unicode icon to describe the given weather condition."""
  121. icons = {
  122. "chanceflurries" : "☃",
  123. "chancerain" : "☂",
  124. "chancesleet" : "☃",
  125. "chancesnow" : "☃",
  126. "chancetstorms" : "☂",
  127. "clear" : "☽☀",
  128. "cloudy" : "☁",
  129. "flurries" : "☃",
  130. "fog" : "☁",
  131. "hazy" : "☁",
  132. "mostlycloudy" : "☁",
  133. "mostlysunny" : "☽☀",
  134. "partlycloudy" : "☁",
  135. "partlysunny" : "☽☀",
  136. "rain" : "☂",
  137. "sleet" : "☃",
  138. "snow" : "☃",
  139. "sunny" : "☽☀",
  140. "tstorms" : "☂",
  141. }
  142. try:
  143. icon = icons[condition]
  144. if len(icon) == 2:
  145. lt_no_tz = local_time.rsplit(" ", 1)[0]
  146. dt = datetime.strptime(lt_no_tz, "%a, %d %b %Y %H:%M:%S")
  147. srise = datetime(year=dt.year, month=dt.month, day=dt.day,
  148. hour=int(sun_phase["sunrise"]["hour"]),
  149. minute=int(sun_phase["sunrise"]["minute"]))
  150. sset = datetime(year=dt.year, month=dt.month, day=dt.day,
  151. hour=int(sun_phase["sunset"]["hour"]),
  152. minute=int(sun_phase["sunset"]["minute"]))
  153. return icon[int(srise < dt < sset)]
  154. return icon
  155. except KeyError:
  156. return "?"
  157. def format_ambiguous_result(self, res):
  158. """Format a message when there are multiple possible results."""
  159. results = []
  160. for place in res["response"]["results"]:
  161. extra = place["state" if place["state"] else "country"]
  162. results.append("{0}, {1}".format(place["city"], extra))
  163. if len(results) > 21:
  164. extra = len(results) - 20
  165. res = "; ".join(results[:20])
  166. return "Did you mean: {0}... ({1} others)?".format(res, extra)
  167. return "Did you mean: {0}?".format("; ".join(results))