EvilZone

Programming and Scripting => Scripting Languages => : Lenoch August 18, 2015, 01:15:54 AM

: [Ruby] a weather notification script
: Lenoch August 18, 2015, 01:15:54 AM
A little script I made that fits in my desktop workflow

:
#!/usr/bin/ruby

require 'rubygems'
require 'json'
require 'etc'
require 'net/http'
require 'fileutils'
require 'configparser'

class Weather
  def initialize(location, link)
    @location = location
    @link = link
    @data = get_data
  end

  def kelvin_to_celcius(kelvin)
    ((kelvin - 273.15) * 1).round.to_s
  end

  def get_data
    uri = URI(@link + @location)
    Net::HTTP.get(uri)
  end

  def get_degrees
    json = JSON.parse( @data )
    kelvin_to_celcius(json["main"]["temp_min"].to_i)
  end

  def to_s
    get_degrees + "°C In " + @location
  end
end

conf_dir = '/home/' + ENV['USER'] + '/.weatherdzen/'

unless File.exists? conf_dir
  FileUtils.mkpath conf_dir
  FileUtils.touch conf_dir + 'config'
 
  File.open(conf_dir + 'config', "w") do |f|
    f.puts('[basic]')
    f.puts('location=Antwerp')
    f.puts('notify=notifysend')
    f.close
  end
end

cfg = ConfigParser.new(conf_dir + 'config')

weather = Weather.new(
  cfg["basic"]["location"],
  "http://api.openweathermap.org/data/2.5/weather?q="
)

notifier = cfg["basic"]["notify"]
wtr =  weather.to_s

if notifier == "dzen"
  exec 'echo ' + wtr + '| dzen2 -title-name "brightness" -l 3 -bg "#263238" -fg "#b1fff8" -x 790 -y 480 -w 270 -h 70 -p 3 -e'
elsif notifier == "notifysend"
  exec 'notify-send "' + wtr + '"'
end


Needs  etc and configparser gems + dzen2 installed.

EDIT:autowrite config, autowrite based on users location coming later
EDIT2: for people with xfce and unity and any other DE that uses notify-send you can now use that and configure in the config file
: Re: [Ruby] a weather notification script
: kenjoe41 August 18, 2015, 02:03:39 AM
Mmmmh, i wonder if we can auto detect our location and auto write it to cfg file. Mmmh

And how about  it runs as a daemon and periodically gets the weather info and......(my thoughts trail of because of the annoyance it would be reporting every after X time) but Ubuntu users can make a widget or something for it.

My thoughts about the notification, it would hook into the [gui client here] shell and  notifies me. This depends on the comments about that trailed of to oblivion.
Aaaah.
: Re: [Ruby] a weather notification script
: Lenoch August 18, 2015, 02:16:21 AM
I don't have a DE with this that's why just use dzen and I just want the weather when I run weather. And yes autowriting is a good idea!