Advertisement
Namasteh

Untitled

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