Advertisement
Namasteh

Untitled

Feb 8th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.41 KB | None | 0 0
  1.    
  2.  
  3.     require 'cinch'
  4.     require 'ostruct'
  5.     require 'open-uri'
  6.     require 'json'
  7.     require 'cinch/toolbox'
  8.     require 'cinch-storage'
  9.      
  10.     module Cinch
  11.       module Plugins
  12.         class Weather
  13.           include Cinch::Plugin
  14.          
  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 /w set (.+)/i, method: :set_custom
  25.          
  26.           def initialize(*args)
  27.             super
  28.               @storage = CinchStorage.new(config[:filename] || 'weather_loc.yml')
  29.               @storage.data[nick.downcase] = zipcode
  30.             end
  31.        
  32.           def set_custom(m, zipcode)
  33.             @storage.data[nick] = zipcode
  34.             @storage.synced_save(@bot)
  35.           end
  36.        
  37.           def current(m, query)
  38.             query.gsub! /\s/, '+'
  39.             geometry = geolookup(query)
  40.             return m.reply "No results found for #{query}." if geometry.nil?
  41.          
  42.             data = get_current(geometry)
  43.             return m.reply 'Uh oh, there was a problem getting the specified weather data. Try again later.' if data.nil?
  44.          
  45.             locale = location_r(query)
  46.          
  47.             m.reply(current_summary(data, locale))
  48.           end
  49.          
  50.           # Now we introduce a method to get the hourly up to 24hrs.
  51.          
  52.           def hourly(hour, query)
  53.             query.gsub! /\s/, '+'
  54.             geometry = geolookup(query)
  55.             return m.reply "No results found for #{query}." if geometry.nil?
  56.          
  57.             data = get_hourly(hour, geometry)
  58.             return m.reply 'Oh no! There was a problem fetching the specified weather data. Please try again later.' if data.empty?
  59.            
  60.             hourly_summary(hour)
  61.           end
  62.          
  63.           # Fetch the location from Google Maps for the area the user is looking for to be passed into get_current.
  64.          
  65.           def geolookup(zipcode)
  66.             raw_location = JSON.parse(open("http://maps.googleapis.com/maps/api/geocode/json?address=#{zipcode}&sensor=true").read)
  67.             location = raw_location['results'][0]['geometry']['location']
  68.             lat = location['lat']
  69.             lng = location['lng']
  70.            
  71.             geometry = "#{lat},#{lng}"
  72.                     return geometry
  73.           rescue
  74.             nil
  75.           end
  76.          
  77.           # Fetch the current weather data for the location found in geolookup.
  78.          
  79.           def get_current(geometry)
  80.             data = JSON.parse(open("https://api.forecast.io/forecast/9cf5d8e80388a30e7e37b14e43b9707a/#{geometry}").read)
  81.             current = data['currently']
  82.            
  83.             OpenStruct.new(
  84.               conditions:    current['summary'],
  85.               temp:          current['temperature'],
  86.               feels_like:    current['apparentTemperature'],
  87.               wind_speed:    current['windSpeed'],
  88.               wind_bearing:  current['windBearing']
  89.             )
  90.             rescue
  91.               nil
  92.             end
  93.            
  94.           # Now we are going to fetch the hourly data.
  95.          
  96.           def get_hourly(hour, geometry)
  97.             logo = "10Hourly Fore4cast:"
  98.             data = JSON.parse(open("https://api.forecast.io/forecast/9cf5d8e80388a30e7e37b14e43b9707a/#{geometry}").read)
  99.             raw_hourly = data['hourly']['data']
  100.             hourly = []
  101.            
  102.             raw_hourly.each{|i| hourly.push("%s Forecast Predicted in #{hour}(s): %s | Temp: %s - Will Feel Like: %s | Wind: %s - Bearing: %s" % [logo, i['summary'], i['temperature'], i['apparentTemperature'], i['windSpeed'], i['windBearing']])}
  103.            
  104.             return hourly
  105.           end
  106.          
  107.           def hourly_summary(hour)
  108.             hourly[hour.to_i - 1].each{|i| m.reply i}
  109.           end
  110.          
  111.           # We're gonna fetch the location data again so we can pass it into the results. Primitive, but it works.
  112.          
  113.           def location_r(location)
  114.             raw_locale = JSON.parse(open("http://maps.googleapis.com/maps/api/geocode/json?address=#{location}&sensor=true").read)
  115.             locale_data = raw_locale['results'][0]['formatted_address']
  116.            
  117.             locale = "#{locale_data}"
  118.           end
  119.        
  120.           def current_summary(data, locale)
  121.             # Converting Imperial for Metric, because it's sane when using a protocol like IRC to deliver this info
  122.          
  123.             temp_c = (data.temp - 32) * 5 / 9
  124.             feels_temp_c = (data.feels_like - 32) * 5 / 9
  125.             wind_speed_kph = data.wind_speed * 1.6
  126.             temp_c = (temp_c*10).ceil/10.0
  127.             feels_temp_c = (feels_temp_c*10).ceil/10.0
  128.             wind_speed_kph = (wind_speed_kph*10).ceil/10.0
  129.            
  130.             # Now, deliver the info to IRC
  131.            
  132.             %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}}
  133.           end
  134.         end
  135.       end
  136.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement