You are not logged in.

#1 2008-04-19 01:02:32

freakcode
Member
From: São Paulo - Brazil
Registered: 2007-11-03
Posts: 410
Website

Ruby script for status info in Awesome

Some Ruby code I did to print info to a panel on Awesome. There is a base Widget class you can use to implement your own widgets. Included there is a Clock, a RSS feed and a Yahoo!Weather widget. RSS and Weather widgets will scroll the text like a news ticker.

#!/usr/bin/env ruby


class String # Utility methods
  def stripHtml
    require 'cgi'
    return CGI::unescapeHTML(self.gsub(/[\n\r]/, "").gsub(/<\/?[^>]*>/, ""))
  end
  def scroll
    str = self
    return str[1..str.length-1] + str[0...1]
  end
end

class WidgetClient # Base Widget Class
  protected
  Pipe = "/usr/bin/awesome-client"

  def writePipe(widget, status)
    toWrite = "0 widget_tell #{widget} #{status}\n"  
    pipe = IO.popen(Pipe, "w")
    pipe.write(toWrite)
    pipe.close
  end
  def iterate
  end
end

class ClockClient < WidgetClient
  require 'date'

  def initialize(widget)
    @widget = widget || "clock"
  end

  def getDate
    return DateTime::now.strftime('%d.%m.%y')
  end

  def getTime
    return DateTime::now.strftime('%H:%M:%S')
  end

  def iterate
    writePipe("clock", getTime)
    writePipe("date", getDate)
  end
end

class RSSClient < WidgetClient
  require 'date'
  require 'open-uri'
  require 'rss'

  def initialize(widget, url)
    @widget = widget || "rss"
    @url = url
    @lastUpdate = -1
  end

  def getFeed(url)
    data = nil
    open(url) { |s| data = s.read }
    raise "Unable the read #{url}" if data.nil? 
    return data
  end
  
  def parseFeed(data)
    rss = RSS::Parser.parse(data, true)
    return rss.channel.title.stripHtml + ": " + rss.channel.items[0].title.stripHtml
  end
  
  def iterate
    if @lastUpdate < DateTime::now.hour
      @text = parseFeed(getFeed(@url))
      (@text.length/2).times { @text << " " }
      @lastUpdate = DateTime::now.hour
    end
    writePipe(@widget, @text)
    @text = @text.scroll
  end
end

class YahooWeatherClient < RSSClient
  def initialize(widget, ccode)
    @widget = widget || "weather"
    @url = "http://weather.yahooapis.com/forecastrss?p=#{ccode}&u=c"
    @lastUpdate = -1
  end

  def parseFeed(data)
    rss = RSS::Parser.parse(data, false, false)
    return rss.channel.items[0].title.stripHtml + ":" +  rss.channel.items[0].description.stripHtml.split("Current Conditions:")[1].split("Full Forecast")[0]
  end
end

# Main
if $0 == __FILE__
  clock = ClockClient.new("clock")
  weather = YahooWeatherClient.new("weather", "BRXX0232") # Find your City Code on http://weather.yahoo.com

  while true do
    clock.iterate
    weather.iterate
    sleep 1
  end

end

You call the script from login, and then add some boxes on .awesomerc to where the script will print data. E.g., if on #Main you call a weather widget with:

  weather = YahooWeatherClient.new("weather", "BRXX0232")

Then you need on .awesomerc a texbox with the same name:

    textbox weather {
      align = "left"
      fg = "#c0c0c0"
      font = "Liberation Mono-8"
    }

Have fun!

Offline

Board footer

Powered by FluxBB