Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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