lib/Zeta/plugins/weather.rb in zetabot-1.0.2 vs lib/Zeta/plugins/weather.rb in zetabot-1.0.3
- old
+ new
@@ -21,12 +21,10 @@
match /w (.+)/, method: :weather
match 'w', method: :weather
match /setw (.+)/, method: :set_location
match /wx (.+)/, method: :weather
match /weather (.+)/, method: :weather
- match /almanac (.+)/, method: :almanac
- match /hurricane/, method: :hurricane
#####
def initialize(*args)
@api_src = %w{wu noaa darksky owm}
@store = Persist.new(File.join(Dir.home, '.zeta', 'cache', 'weather.pstore'))
@@ -74,95 +72,48 @@
serial_location = "#{query}::#{src}"
@store[msg.user.to_s] = serial_location unless data.nil?
msg.reply "Your location is now set to #{data.ac.name}, #{data.ac.c}!"
end
- # ?hurricane
- def hurricane(msg)
- url = URI.encode "http://api.wunderground.com/api/#{Zsec.wunderground}/currenthurricane/view.json"
- location = JSON.parse(
- # RestClient.get(url)
- open(url).read
- )
- return msg.reply "No results found for #{query}." if location.nil?
- reply_msg = "∴ #{location['currenthurricane'][0]['stormInfo']['stormName_Nice']} " \
- "(#{location['currenthurricane'][0]['stormInfo']['stormNumber']}) "\
- "≈ Category #{location['currenthurricane'][0]['Current']['SaffirSimpsonCategory']} " \
- "≈ Wind #{location['currenthurricane'][0]['Current']['WindSpeed']['Mph']} mph " \
- "(#{location['currenthurricane'][0]['Current']['WindSpeed']['Kph']} kph) " \
- "≈ Wind Gust #{location['currenthurricane'][0]['Current']['WindGust']['Mph']} mph " \
- "(#{location['currenthurricane'][0]['Current']['WindGust']['Kph']} kph) " \
- "≈ #{location['currenthurricane'][0]['Current']['Time']['pretty']} ∴"
- msg.reply(reply_msg)
- end
-
- # ?almanac <location>
- def almanac(msg, locale)
- autocomplete = JSON.parse(open(URI.encode("http://autocomplete.wunderground.com/aq?query=#{locale}")).read)
- url = URI.encode("http://api.wunderground.com/api/#{Config.secrets[:wunderground]}/almanac/#{autocomplete['RESULTS'][0]['l']}.json")
- location = JSON.parse(
- # RestClient.get(url)
- open(url).read
- )
- return msg.reply "No results found for #{query}." if location.nil?
-
- time = Time.now()
-
- data = OpenStruct.new(
- date: time.strftime('%B, %d %Y (%A) '),
- airport: location['almanac']['airport_code'],
- high_norm_f: location['almanac']['temp_high']['normal']['F'],
- high_norm_c: location['almanac']['temp_high']['normal']['C'],
- high_record_y: location['almanac']['temp_high']['recordyear'],
- high_record_f: location['almanac']['temp_high']['record']['F'],
- high_record_c: location['almanac']['temp_high']['normal']['C'],
- low_norm_f: location['almanac']['temp_low']['normal']['F'],
- low_norm_c: location['almanac']['temp_low']['normal']['C'],
- low_record_y: location['almanac']['temp_low']['recordyear'],
- low_record_f: location['almanac']['temp_low']['record']['F'],
- low_record_c: location['almanac']['temp_low']['normal']['C'],
- )
-
- reply_msg = "∴ Almanac #{data.date} ≈ Airport #{data.airport} " \
- "≈ Normal #{data.high_norm_f} F (#{data.high_norm_c} C) | #{data.low_norm_f} F (#{data.low_norm_c} C) " \
- "≈ High #{data.high_record_f} F (#{data.high_record_c} C) [#{data.high_record_y}] " \
- "≈ Low #{data.low_record_f} F (#{data.low_record_c} C) [#{data.low_record_y}] ∴"
-
- msg.reply(reply_msg)
- end
-
-
- # -private
private
#### Weather Sources
# Weather Underground - https://wunderground.com
def wu_src(location)
# Fuzzy location lookup
+ # ac = JSON.parse(
+ # open(URI.encode("https://autocomplete.wunderground.com/aq?query=#{location}")).read,
+ # object_class: OpenStruct
+ # )
+ # return nil if ac.RESULTS.empty?
ac = JSON.parse(
- open(URI.encode("https://autocomplete.wunderground.com/aq?query=#{location}")).read,
+ open(URI.encode("http://maps.googleapis.com/maps/api/geocode/json?address=#{location}")).read,
object_class: OpenStruct
)
- return nil if ac.RESULTS.empty?
+ return nil if ac.results.nil? ## Unable to locate
- ac = ac.RESULTS[0]
+ ac = ac.results[0]
+ lat = ac.geometry.location.lat
+ lon = ac.geometry.location.lng
+
+ # ac = ac.RESULTS[0]
geolookup = JSON.parse(
- open(URI.encode("https://api.wunderground.com/api/#{Config.secrets[:wunderground]}/geolookup/#{ac.l}.json")).read,
+ # open(URI.encode("https://api.wunderground.com/api/#{Config.secrets[:wunderground]}/geolookup/#{ac.l}.json")).read,
+ open(URI.encode("https://api.wunderground.com/api/#{Config.secrets[:wunderground]}/geolookup/q/#{lat},#{lon}.json")).read,
object_class: OpenStruct
).location.l rescue nil
# Get Data
data = JSON.parse(
open("https://api.wunderground.com/api/#{Config.secrets[:wunderground]}/alerts/conditions#{geolookup}.json").read,
object_class: OpenStruct
)
- debug "DATA: #{data}"
data.ac = ac
current = data.current_observation
alerts = data.alerts.empty? ? 'none' : data.alerts.map {|l| l['type']}.join(',')
pressure_si = Unitwise((current.pressure_in.to_f)+14.7, '[psi]').convert_to('kPa').to_f.round(2)
- data.reply = "WU ∴ #{ac.name}, #{ac.c} " \
+ data.reply = "WU ∴ #{ac.formatted_address} " \
"≈ #{current.weather} #{current.temperature_string} " \
"≈ Feels like #{current.feelslike_string} " \
"≈ Humidity: #{current.relative_humidity} " \
"≈ Pressure: #{current.pressure_in} psi (#{pressure_si} kPa) " \
"≈ Wind: #{current.wind_string} ≈ Alerts: #{alerts} ∴"