1
0

weather_agent.rb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
  15. MD
  16. event_description <<-MD
  17. Events look like this:
  18. {
  19. "location": "12345",
  20. "date": {
  21. "epoch": "1357959600",
  22. "pretty": "10:00 PM EST on January 11, 2013"
  23. },
  24. "high": {
  25. "fahrenheit": "64",
  26. "celsius": "18"
  27. },
  28. "low": {
  29. "fahrenheit": "52",
  30. "celsius": "11"
  31. },
  32. "conditions": "Rain Showers",
  33. "icon": "rain",
  34. "icon_url": "http://icons-ak.wxug.com/i/c/k/rain.gif",
  35. "skyicon": "mostlycloudy",
  36. ...
  37. }
  38. MD
  39. default_schedule "8pm"
  40. def working?
  41. event_created_within?((interpolated['expected_update_period_in_days'].presence || 2).to_i) && !recent_error_logs?
  42. end
  43. def key_setup?
  44. interpolated['api_key'].present? && interpolated['api_key'] != "your-key"
  45. end
  46. def default_options
  47. {
  48. 'service' => 'wunderground',
  49. 'api_key' => 'your-key',
  50. 'location' => '94103',
  51. 'which_day' => '1',
  52. 'expected_update_period_in_days' => '2'
  53. }
  54. end
  55. def service
  56. interpolated["service"].presence || "wunderground"
  57. end
  58. def which_day
  59. (interpolated["which_day"].presence || 1).to_i
  60. end
  61. def location
  62. interpolated["location"].presence || interpolated["zipcode"]
  63. end
  64. def validate_options
  65. errors.add(:base, "service is required") unless service.present?
  66. errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(service)
  67. errors.add(:base, "location is required") unless location.present?
  68. errors.add(:base, "api_key is required") unless key_setup?
  69. errors.add(:base, "which_day selection is required") unless which_day.present?
  70. end
  71. def wunderground
  72. Wunderground.new(interpolated['api_key']).forecast_for(location)['forecast']['simpleforecast']['forecastday'] if key_setup?
  73. end
  74. def forecastio
  75. if key_setup?
  76. ForecastIO.api_key = interpolated['api_key']
  77. lat, lng = location.split(',')
  78. ForecastIO.forecast(lat,lng)['daily']['data']
  79. end
  80. end
  81. def model(service,which_day)
  82. if service == "wunderground"
  83. wunderground[which_day]
  84. elsif service == "forecastio"
  85. forecastio.each do |value|
  86. timestamp = Time.at(value.time)
  87. if (timestamp.to_date - Time.now.to_date).to_i == which_day
  88. day = {
  89. 'date' => {
  90. 'epoch' => value.time.to_s,
  91. 'pretty' => timestamp.strftime("%l:%M %p %Z on %B %d, %Y"),
  92. 'day' => timestamp.day,
  93. 'month' => timestamp.month,
  94. 'year' => timestamp.year,
  95. 'yday' => timestamp.yday,
  96. 'hour' => timestamp.hour,
  97. 'min' => timestamp.strftime("%M"),
  98. 'sec' => timestamp.sec,
  99. 'isdst' => timestamp.isdst ? 1 : 0 ,
  100. 'monthname' => timestamp.strftime("%B"),
  101. 'monthname_short' => timestamp.strftime("%b"),
  102. 'weekday_short' => timestamp.strftime("%a"),
  103. 'weekday' => timestamp.strftime("%A"),
  104. 'ampm' => timestamp.strftime("%p"),
  105. 'tz_short' => timestamp.zone
  106. },
  107. 'period' => which_day.to_i,
  108. 'high' => {
  109. 'fahrenheit' => value.temperatureMax.round().to_s,
  110. 'epoch' => value.temperatureMaxTime.to_s,
  111. 'fahrenheit_apparent' => value.apparentTemperatureMax.round().to_s,
  112. 'epoch_apparent' => value.apparentTemperatureMaxTime.to_s,
  113. 'celsius' => ((5*(Float(value.temperatureMax) - 32))/9).round().to_s
  114. },
  115. 'low' => {
  116. 'fahrenheit' => value.temperatureMin.round().to_s,
  117. 'epoch' => value.temperatureMinTime.to_s,
  118. 'fahrenheit_apparent' => value.apparentTemperatureMin.round().to_s,
  119. 'epoch_apparent' => value.apparentTemperatureMinTime.to_s,
  120. 'celsius' => ((5*(Float(value.temperatureMin) - 32))/9).round().to_s
  121. },
  122. 'conditions' => value.summary,
  123. 'icon' => value.icon,
  124. 'avehumidity' => (value.humidity * 100).to_i,
  125. 'sunriseTime' => value.sunriseTime.to_s,
  126. 'sunsetTime' => value.sunsetTime.to_s,
  127. 'moonPhase' => value.moonPhase.to_s,
  128. 'precip' => {
  129. 'intensity' => value.precipIntensity.to_s,
  130. 'intensity_max' => value.precipIntensityMax.to_s,
  131. 'intensity_max_epoch' => value.precipIntensityMaxTime.to_s,
  132. 'probability' => value.precipProbability.to_s,
  133. 'type' => value.precipType
  134. },
  135. 'dewPoint' => value.dewPoint.to_s,
  136. 'avewind' => {
  137. 'mph' => value.windSpeed.round().to_s,
  138. 'kph' => (Float(value.windSpeed) * 1.609344).round().to_s,
  139. 'degrees' => value.windBearing.to_s
  140. },
  141. 'visibility' => value.visibility.to_s,
  142. 'cloudCover' => value.cloudCover.to_s,
  143. 'pressure' => value.pressure.to_s,
  144. 'ozone' => value.ozone.to_s
  145. }
  146. return day
  147. end
  148. end
  149. end
  150. end
  151. def check
  152. if key_setup?
  153. create_event :payload => model(service, which_day).merge('location' => location)
  154. end
  155. end
  156. end
  157. end