weather_agent.rb 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. require 'date'
  2. require 'cgi'
  3. module Agents
  4. class WeatherAgent < Agent
  5. cannot_receive_events!
  6. description <<-MD
  7. The WeatherAgent creates an event for the day's weather at a given `location`.
  8. You also must select `which_day` you would like to get the weather for where the number 0 is for today and 1 is for tomorrow and so on. Weather is only returned for 1 week at a time.
  9. The weather can be provided by either Wunderground or ForecastIO. To choose which `service` to use, enter either `forecastio` or `wunderground`.
  10. The `location` can be a US zipcode, or any location that Wunderground supports. To find one, search [wunderground.com](http://wunderground.com) and copy the location part of the URL. For example, a result for San Francisco gives `http://www.wunderground.com/US/CA/San_Francisco.html` and London, England gives `http://www.wunderground.com/q/zmw:00000.1.03772`. The locations in each are `US/CA/San_Francisco` and `zmw:00000.1.03772`, respectively.
  11. If you plan on using ForecastIO, the `location` must be a set of GPS coordinates.
  12. You must setup an [API key for Wunderground](http://www.wunderground.com/weather/api/) in order to use this Agent with Wunderground.
  13. You must setup an [API key for Forecast](https://developer.forecast.io/) in order to use this Agent with ForecastIO.
  14. MD
  15. event_description <<-MD
  16. Events look like this:
  17. {
  18. "location": "12345",
  19. "date": {
  20. "epoch": "1357959600",
  21. "pretty": "10:00 PM EST on January 11, 2013"
  22. },
  23. "high": {
  24. "fahrenheit": "64",
  25. "celsius": "18"
  26. },
  27. "low": {
  28. "fahrenheit": "52",
  29. "celsius": "11"
  30. },
  31. "conditions": "Rain Showers",
  32. "icon": "rain",
  33. "icon_url": "http://icons-ak.wxug.com/i/c/k/rain.gif",
  34. "skyicon": "mostlycloudy",
  35. ...
  36. }
  37. MD
  38. default_schedule "8pm"
  39. def working?
  40. event_created_within?(2) && !recent_error_logs?
  41. end
  42. def key_setup?
  43. options['api_key'].present? && options['api_key'] != "your-key"
  44. end
  45. def default_options
  46. {
  47. 'service' => 'wunderground',
  48. 'api_key' => 'your-key',
  49. 'location' => '94103',
  50. 'which_day' => '1'
  51. }
  52. end
  53. def service
  54. options["service"].presence || "wunderground"
  55. end
  56. def which_day
  57. (options["which_day"].presence || 1).to_i
  58. end
  59. def location
  60. options["location"].presence || options["zipcode"]
  61. end
  62. def validate_options
  63. errors.add(:base, "service is required") unless service.present?
  64. errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(service)
  65. errors.add(:base, "location is required") unless location.present?
  66. errors.add(:base, "api_key is required") unless key_setup?
  67. errors.add(:base, "which_day selection is required") unless which_day.present?
  68. end
  69. def wunderground
  70. Wunderground.new(options['api_key']).forecast_for(location)['forecast']['simpleforecast']['forecastday'] if key_setup?
  71. end
  72. def forecastio
  73. if key_setup?
  74. ForecastIO.api_key = options['api_key']
  75. lat, lng = location.split(',')
  76. ForecastIO.forecast(lat,lng)['daily']['data']
  77. end
  78. end
  79. def model(service,which_day)
  80. if service == "wunderground"
  81. wunderground[which_day]
  82. elsif service == "forecastio"
  83. forecastio.each do |value|
  84. timestamp = Time.at(value.time)
  85. if (timestamp.to_date - Time.now.to_date).to_i == which_day
  86. day = {
  87. 'date' => {
  88. 'epoch' => value.time.to_s,
  89. 'pretty' => timestamp.strftime("%l:%M %p %Z on %B %d, %Y"),
  90. 'day' => timestamp.day,
  91. 'month' => timestamp.month,
  92. 'year' => timestamp.year,
  93. 'yday' => timestamp.yday,
  94. 'hour' => timestamp.hour,
  95. 'min' => timestamp.strftime("%M"),
  96. 'sec' => timestamp.sec,
  97. 'isdst' => timestamp.isdst ? 1 : 0 ,
  98. 'monthname' => timestamp.strftime("%B"),
  99. 'monthname_short' => timestamp.strftime("%b"),
  100. 'weekday_short' => timestamp.strftime("%a"),
  101. 'weekday' => timestamp.strftime("%A"),
  102. 'ampm' => timestamp.strftime("%p"),
  103. 'tz_short' => timestamp.zone
  104. },
  105. 'period' => which_day.to_i,
  106. 'high' => {
  107. 'fahrenheit' => value.temperatureMax.round().to_s,
  108. 'epoch' => value.temperatureMaxTime.to_s,
  109. 'fahrenheit_apparent' => value.apparentTemperatureMax.round().to_s,
  110. 'epoch_apparent' => value.apparentTemperatureMaxTime.to_s,
  111. 'celsius' => ((5*(Float(value.temperatureMax) - 32))/9).round().to_s
  112. },
  113. 'low' => {
  114. 'fahrenheit' => value.temperatureMin.round().to_s,
  115. 'epoch' => value.temperatureMinTime.to_s,
  116. 'fahrenheit_apparent' => value.apparentTemperatureMin.round().to_s,
  117. 'epoch_apparent' => value.apparentTemperatureMinTime.to_s,
  118. 'celsius' => ((5*(Float(value.temperatureMin) - 32))/9).round().to_s
  119. },
  120. 'conditions' => value.summary,
  121. 'icon' => value.icon,
  122. 'avehumidity' => (value.humidity * 100).to_i,
  123. 'sunriseTime' => value.sunriseTime.to_s,
  124. 'sunsetTime' => value.sunsetTime.to_s,
  125. 'moonPhase' => value.moonPhase.to_s,
  126. 'precip' => {
  127. 'intensity' => value.precipIntensity.to_s,
  128. 'intensity_max' => value.precipIntensityMax.to_s,
  129. 'intensity_max_epoch' => value.precipIntensityMaxTime.to_s,
  130. 'probability' => value.precipProbability.to_s,
  131. 'type' => value.precipType
  132. },
  133. 'dewPoint' => value.dewPoint.to_s,
  134. 'avewind' => {
  135. 'mph' => value.windSpeed.round().to_s,
  136. 'kph' => (Float(value.windSpeed) * 1.609344).round().to_s,
  137. 'degrees' => value.windBearing.to_s
  138. },
  139. 'visibility' => value.visibility.to_s,
  140. 'cloudCover' => value.cloudCover.to_s,
  141. 'pressure' => value.pressure.to_s,
  142. 'ozone' => value.ozone.to_s
  143. }
  144. return day
  145. end
  146. end
  147. end
  148. end
  149. def check
  150. if key_setup?
  151. create_event :payload => model(service, which_day).merge('location' => location)
  152. end
  153. end
  154. end
  155. end