Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'cinch'
- require 'ostruct'
- require 'open-uri'
- require 'json'
- require 'cinch/toolbox'
- module Cinch
- module Plugins
- class Weather
- include Cinch::Plugin
- set :prefix, /^~/
- set :help, <<-USAGE.gsub(/^ {6}/, '')
- Check the weather right from the IRC channel!
- Usage:
- * ~weather <location>: The bot will check the weather for the location you provide!
- USAGE
- match /weather (.+)/i, method: :current
- match /f (.+) (.+)/i, method: :hourly
- def current(m, query)
- query.gsub! /\s/, '+'
- geometry = geolookup(query)
- return m.reply "No results found for #{query}." if geometry.nil?
- data = get_current(geometry)
- return m.reply 'Uh oh, there was a problem getting the specified weather data. Try again later.' if data.nil?
- locale = location_r(query)
- m.reply(current_summary(data, locale))
- end
- # Now we introduce a method to get the hourly up to 24hrs.
- def hourly(hour, query)
- query.gsub! /\s/, '+'
- geometry = geolookup(query)
- return m.reply "No results found for #{query}." if geometry.nil?
- data = get_hourly(hour, geometry)
- return m.reply 'Oh no! There was a problem fetching the specified weather data. Please try again later.' if data.empty?
- reply(hourly_summary(hour, data))
- end
- # Fetch the location from Google Maps for the area the user is looking for to be passed into get_current.
- def geolookup(zipcode)
- raw_location = JSON.parse(open("http://maps.googleapis.com/maps/api/geocode/json?address=#{zipcode}&sensor=true").read)
- location = raw_location['results'][0]['geometry']['location']
- lat = location['lat']
- lng = location['lng']
- geometry = "#{lat},#{lng}"
- rescue
- nil
- end
- # Fetch the current weather data for the location found in geolookup.
- def get_current(geometry)
- data = JSON.parse(open("https://api.forecast.io/forecast/9cf5d8e80388a30e7e37b14e43b9707a/#{geometry}").read)
- current = data['currently']
- OpenStruct.new(
- conditions: current['summary'],
- temp: current['temperature'],
- feels_like: current['apparentTemperature'],
- wind_speed: current['windSpeed'],
- wind_bearing: current['windBearing']
- )
- rescue
- nil
- end
- # Now we are going to fetch the hourly data.
- def get_hourly(hour, geometry)
- logo = "10Hourly Fore4cast:"
- data = JSON.parse(open("https://api.forecast.io/forecast/9cf5d8e80388a30e7e37b14e43b9707a/#{geometry}").read)
- raw_hourly = data['hourly']['data']
- hourly = []
- 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']])}
- return hourly
- end
- def hourly_summary(hour, data)
- if hour == "1"
- hourly[0].each{|i| m.reply i}
- return;
- end
- if hour == 2
- hourly[1].each{|i| m.reply i}
- return;
- end
- if hour == 3
- hourly[2].each{|i| m.reply i}
- return;
- end
- if hour == 4
- hourly[3].each{|i| m.reply i}
- return;
- end
- if hour == 5
- hourly[4].each{|i| m.reply i}
- return;
- end
- if hour == 6
- hourly[5].each{|i| m.reply i}
- return;
- end
- if hour == 7
- hourly[6].each{|i| m.reply i}
- return;
- end
- if hour == 8
- hourly[7].each{|i| m.reply i}
- return;
- end
- if hour == 9
- hourly[8].each{|i| m.reply i}
- return;
- end
- if hour == 10
- hourly[9].each{|i| m.reply i}
- return;
- end
- if hour == 11
- hourly[10].each{|i| m.reply i}
- return;
- end
- if hour == 12
- hourly[11].each{|i| m.reply i}
- return;
- end
- if hour == 13
- hourly[12].each{|i| m.reply i}
- return;
- end
- if hour == 14
- hourly[13].each{|i| m.reply i}
- return;
- end
- if hour == 15
- hourly[14].each{|i| m.reply i}
- return;
- end
- if hour == 16
- hourly[15].each{|i| m.reply i}
- return;
- end
- if hour == 17
- hourly[16].each{|i| m.reply i}
- return;
- end
- if hour == 18
- hourly[17].each{|i| m.reply i}
- return;
- end
- if hour == 19
- hourly[18].each{|i| m.reply i}
- return;
- end
- if hour == 20
- hourly[19].each{|i| m.reply i}
- return;
- end
- if hour == 21
- hourly[20].each{|i| m.reply i}
- return;
- end
- if hour == 22
- hourly[21].each{|i| m.reply i}
- return;
- end
- if hour == 23
- hourly[22].each{|i| m.reply i}
- return;
- end
- if hour == 24
- hourly[23].each{|i| m.reply i}
- return;
- end
- end
- # We're gonna fetch the location data again so we can pass it into the results. Primitive, but it works.
- def location_r(location)
- raw_locale = JSON.parse(open("http://maps.googleapis.com/maps/api/geocode/json?address=#{location}&sensor=true").read)
- locale_data = raw_locale['results'][0]['formatted_address']
- locale = "#{locale_data}"
- end
- def current_summary(data, locale)
- # Converting Imperial for Metric, because it's sane when using a protocol like IRC to deliver this info
- temp_c = (data.temp - 32) * 5 / 9
- feels_temp_c = (data.feels_like - 32) * 5 / 9
- wind_speed_kph = data.wind_speed * 1.6
- temp_c = (temp_c*10).ceil/10.0
- feels_temp_c = (feels_temp_c*10).ceil/10.0
- wind_speed_kph = (wind_speed_kph*10).ceil/10.0
- # Now, deliver the info to IRC
- %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} }
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement