Advertisement
Namasteh

Untitled

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