Last active
March 13, 2020 09:13
-
-
Save celediel/332e64e7a0c9a42778e0 to your computer and use it in GitHub Desktop.
Cinch Wunderground plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding=utf-8 | |
# require 'cinch' | |
require 'wunderground' | |
require 'mash' | |
# These are all very annoying. | |
# rubocop:disable Metrics/LineLength, Metrics/ClassLength, Metrics/CyclomaticComplexity | |
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize | |
module Cinch | |
module Plugins | |
# Weather n shit | |
class Wunder | |
include Cinch::Plugin | |
set :prefix, /^\./ | |
def initialize(*args) | |
super | |
@api_key = config[:api_key] | |
@w = Wunderground.new(@api_key) | |
end | |
match(/we(?: (.+))?/, method: :current_conditions) | |
def current_conditions(m, loc) | |
m.reply('Enter a location, silly!') && return if loc.empty? | |
now = @w.conditions_for(loc) | |
mnow = Mash.new(now) | |
puts mnow | |
begin | |
m.reply(mnow.response.error.description) | |
return | |
rescue NoMethodError | |
puts 'carry on' | |
end | |
begin | |
here = mnow.current_observation.display_location.full | |
rescue NoMethodError | |
output = 'Pick one :: ' | |
results = [] | |
mnow.response.results.each do |hi| | |
if hi.state.empty? | |
results << "#{hi.city}, #{hi.country_name}" | |
else | |
results << "#{hi.city}, #{hi.state}" | |
end | |
end | |
results.each { |hi| output << "#{hi} :: " } | |
m.reply(output) | |
return | |
end | |
temp = mnow.current_observation.feelslike_string | |
cond = mnow.current_observation.weather | |
wind_dir = mnow.current_observation.wind_dir | |
wind_mph = mnow.current_observation.wind_mph | |
wind_kph = mnow.current_observation.wind_kph | |
precip = mnow.current_observation.precip_today_string | |
cond = 'Dunno' if cond.empty? | |
case | |
when cond.downcase.include?('partly cloud') | |
icon = '⛅' | |
when cond.downcase.include?('cloud') | |
icon = '☁' | |
when cond.downcase.include?('rain') || cond.downcase.include?('shower') | |
icon = '☔' | |
when cond.downcase.include?('sun') || cond.downcase.include?('clear') | |
icon = '☀' | |
when cond.downcase.include?('snow') | |
icon = '☃' | |
when cond.downcase.include?('smoke') | |
icon = '♨' | |
else | |
icon = '⛱' | |
end | |
output = "#{icon} #{here} :: #{temp} :: #{cond}" | |
output << " :: #{wind_mph}mph (#{wind_kph}kph) wind from the #{wind_dir}." unless wind_mph.zero? | |
m.reply(output) | |
end | |
end | |
end | |
end | |
# vim:tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab foldmethod=syntax: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment