Author Topic: [Ruby] a weather notification script  (Read 536 times)

0 Members and 1 Guest are viewing this topic.

Offline Lenoch

  • EZ's Menstruator
  • VIP
  • Serf
  • *
  • Posts: 49
  • Cookies: 37
    • View Profile
[Ruby] a weather notification script
« on: August 18, 2015, 01:15:54 am »
A little script I made that fits in my desktop workflow

Code: [Select]
#!/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
« Last Edit: August 18, 2015, 01:55:04 pm by Lenoch »


Quote
<m0dem> I find evilzone is a really HQ community

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: [Ruby] a weather notification script
« Reply #1 on: 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.
« Last Edit: August 18, 2015, 02:08:23 am by kenjoe41 »
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline Lenoch

  • EZ's Menstruator
  • VIP
  • Serf
  • *
  • Posts: 49
  • Cookies: 37
    • View Profile
Re: [Ruby] a weather notification script
« Reply #2 on: 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!


Quote
<m0dem> I find evilzone is a really HQ community