Advertisement
Namasteh

Untitled

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