Advertisement
Namasteh

Untitled

Feb 9th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.27 KB | None | 0 0
  1.    
  2.  
  3.            
  4.      
  5.         require 'cinch'
  6.         require 'ostruct'
  7.         require 'open-uri'
  8.         require 'json'
  9.         require 'cinch/toolbox'
  10.          
  11.         module Cinch
  12.           module Plugins
  13.             class Weather
  14.               include Cinch::Plugin
  15.            
  16.               set :prefix, /^~/
  17.               set :help, <<-USAGE.gsub(/^ {6}/, '')
  18.                 Check the weather right from the IRC channel!
  19.                 Usage:
  20.                 * ~weather <location>: The bot will check the weather for the location you provide!
  21.               USAGE
  22.            
  23.               match /weather (.+)/i, method: :current
  24.               match /f (.+) (.+)/i, method: :hourly
  25.               match /df (.+) (.+)/i, method: :daily
  26.            
  27.               def current(m, query)
  28.                 query.gsub! /\s/, '+'
  29.                 geometry = geolookup(query)
  30.                 return m.reply "No results found for #{query}." if geometry.nil?
  31.              
  32.                 data = get_current(geometry)
  33.                 return m.reply 'Uh oh, there was a problem getting the specified weather data. Try again later.' if data.nil?
  34.              
  35.                 locale = location_r(query)
  36.              
  37.                 m.reply(current_summary(data, locale))
  38.               end
  39.              
  40.               # Now we introduce a method to get the hourly up to 24hrs.
  41.              
  42.               def hourly(m, hour, query)
  43.                 query.gsub! /\s/, '+'
  44.                 geometry = geolookup(query)
  45.                 return m.reply "No results found for #{query}." if geometry.nil?
  46.                
  47.                 locale = location_r(query)
  48.              
  49.                 data = get_hourly(hour, geometry, locale)
  50.                
  51.                 return m.reply 'Oh no! There was a problem fetching the specified weather data. Please try again later.' if data.empty?              
  52.                
  53.                 m.reply(hourly_summary(data,hour))
  54.               end
  55.              
  56.               # Now we introduce a method to get the daily forecast.
  57.              
  58.               def daily(m, day, query)
  59.                 query.gsub! /\s/, '+'
  60.                 geometry = geolookup(query)
  61.                 return m.reply "No results found for #{query}." if geometry.nil?
  62.                
  63.                 locale = location_r(query)
  64.                
  65.                 data = get_daily(day, geometry, locale)
  66.                
  67.                 return m.reply 'Oh no! There was a problem fetching the specified weather data. Please try again later.' if data.empty?
  68.                
  69.                 m.reply(daily_summary(data,day))
  70.               end
  71.              
  72.               # Fetch the location from Google Maps for the area the user is looking for to be passed into get_current.
  73.              
  74.               def geolookup(zipcode)
  75.                 raw_location = JSON.parse(open("http://maps.googleapis.com/maps/api/geocode/json?address=#{zipcode}&sensor=true").read)
  76.                 location = raw_location['results'][0]['geometry']['location']
  77.                 lat = location['lat']
  78.                 lng = location['lng']
  79.                
  80.                 geometry = "#{lat},#{lng}"
  81.                 return geometry
  82.               rescue
  83.                 nil
  84.               end
  85.              
  86.               # Fetch the current weather data for the location found in geolookup.
  87.              
  88.               def get_current(geometry)
  89.                 data = JSON.parse(open("https://api.forecast.io/forecast/9cf5d8e80388a30e7e37b14e43b9707a/#{geometry}").read)
  90.                 current = data['currently']
  91.                
  92.                 OpenStruct.new(
  93.                   conditions:    current['summary'],
  94.                   temp:          current['temperature'],
  95.                   feels_like:    current['apparentTemperature'],
  96.                   wind_speed:    current['windSpeed'],
  97.                   wind_bearing:  current['windBearing']
  98.                 )
  99.                 rescue
  100.                   nil
  101.                 end
  102.                
  103.               # Now we are going to fetch the hourly data.
  104.              
  105.               def get_hourly(hour, geometry, locale)
  106.                 logo = "10Hourly Fore4cast:"
  107.                 data = JSON.parse(open("https://api.forecast.io/forecast/9cf5d8e80388a30e7e37b14e43b9707a/#{geometry}").read)
  108.                 raw_hourly = data['hourly']['data']
  109.                 hourly = []
  110.                
  111.                 raw_hourly.each{|i| hourly.push("%s - #{locale}: Forecast Predicted in #{hour} hour(s): %s | Temp: %s°F - Will Feel Like: %s°F | Wind: [ Speed: %s MPH | Bearing: %s ]" % [logo, i['summary'], i['temperature'], i['apparentTemperature'], i['windSpeed'], i['windBearing']])}
  112.                
  113.                 return hourly
  114.               end
  115.              
  116.               def hourly_summary(data, hour)
  117.                 return data[hour.to_i - 1]
  118.               end
  119.              
  120.               # Now we are going to fetch the daily data.
  121.              
  122.               def get_daily(day, geometry, locale)
  123.                 logo = "10Daily Fore4cast:"
  124.                 data = JSON.parse(open("https://api.forecast.io/forecast/9cf5d8e80388a30e7e37b14e43b9707a/#{geometry}").read)
  125.                 raw_daily = data['daily']['data']
  126.                 daily = []
  127.                
  128.                 raw_daily.each{|i| daily.push("%s - #{locale}: Forecast Predicted in #{day} day(s): %s | Temp: [ 10Min: %s°F | 4Max: %s°F ] - Will Feel Like: [ 10Min: %s°F | 4Max: %s°F ] | Wind: [ Speed: %s MPH | Bearing: %s ]" % [logo, i['summary'], i['temperatureMin'], i['temperatureMax'], i['apparentTemperatureMin'], i['apparentTemperatureMax'], i['windSpeed'], i['windBearing']])}
  129.                
  130.                 return daily
  131.               end
  132.  
  133.               def daily_summary(data, day)
  134.                 return data[day.to_i - 1]
  135.               end
  136.              
  137.               # We're gonna fetch the location data again so we can pass it into the results. Primitive, but it works.
  138.              
  139.               def location_r(location)
  140.                 raw_locale = JSON.parse(open("http://maps.googleapis.com/maps/api/geocode/json?address=#{location}&sensor=true").read)
  141.                 locale_data = raw_locale['results'][0]['formatted_address']
  142.                
  143.                 locale = "#{locale_data}"
  144.               end
  145.            
  146.               def current_summary(data, locale)
  147.                 # Converting Imperial for Metric, because it's sane when using a protocol like IRC to deliver this info
  148.              
  149.                 temp_c = (data.temp - 32) * 5 / 9
  150.                 feels_temp_c = (data.feels_like - 32) * 5 / 9
  151.                 wind_speed_kph = data.wind_speed * 1.6
  152.                 temp_c = (temp_c*10).ceil/10.0
  153.                 feels_temp_c = (feels_temp_c*10).ceil/10.0
  154.                 wind_speed_kph = (wind_speed_kph*10).ceil/10.0
  155.                
  156.                 # Now, deliver the info to IRC
  157.                
  158.                 %Q{10Weather: #{locale} | Current Weather: #{data.conditions} | Temp: #{data.temp} F (#{temp_c} C) - Feels like: #{data.feels_like} F (#{feels_temp_c} C) | Wind: #{data.wind_speed} MPH (#{wind_speed_kph} KPH) - Bearing: #{data.wind_bearing}}
  159.               end
  160.             end
  161.           end
  162.         end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement