Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Saturday, March 14, 2009

Read csl_status twitter feeds in ruby (via RSS)

The Cal Poly computer science lab admins started a twitter page with updates about downed servers, etc. The following ruby script grabs the most recent entries (via RSS), and formats the dates purty.


#!/usr/bin/ruby
require 'rss'
require 'open-uri'

$BOLD = "\e[1m"
$NORMAL = "\e[m"

num = ARGV.length > 0 ? ARGV[0].to_i : 1

# Read cls_status twitter RSS feed
source = "http://twitter.com/statuses/user_timeline/19618428.rss"
content = ""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)

(0..(num-1)).each do |i|
diff = Time.now - rss.items[i].date
days = diff.to_i / 86400
hrs = (diff % 86400).to_i / 3600
mins,secs = *((diff % 3600).divmod(60))

print "#{$BOLD}"
print "#{days} days " if (days > 0)
print "#{hrs} hrs " if (hrs > 0)
print "#{mins} mins " if (days == 0 and mins > 0)
print "#{mins} secs " if (mins == 0)
puts "ago"

#puts "#{$BOLD}#{rss.items[i].date}"
puts "#{$NORMAL}#{rss.items[i].title}"
#puts rss.items[i].description
#puts rss.items[i].link
end

Monday, October 20, 2008

Better search script (ruby)

Turns out I can kick off a text based browser from Ruby just fine. The trick is to use sytem("elinks") instead of `elinks`. Here's the Ruby version of my search script (cleaner):


#!/usr/bin/ruby
require 'open-uri'

site = ARGV[0]
query = URI.escape(ARGV[1..-1].join(" "))

case site
when "google": url = "http://www.google.com/search?q=#{query}"
when "wikipedia": url = "http://en.wikipedia.org/wiki/Special\:Search?search=#{query}"
when "torrentz": url = "http://www.torrentz.com/search?q=#{query}"
when "isohunt": url = "http://isohunt.com/torrents/?ihq=#{query}"
when "mininova": url = "http://www.mininova.org/search/?search=#{query}"
end

system("elinks #{url}")

Thursday, August 7, 2008

Updated weather script

Since the weather page I used for my previous weather script changed, reducing half of my scripts output to an ascii-splosion, I decided to take another stab at it. Turns out Google supports weather searching. I rewrote the regular expressions and incorporated some new ruby tricks I've learned since. I'm actually rather pleased with the result: weather.rb

The regular expressions are a bit yucky, but that's to be expected. I think the rest of the code is pretty readable. Cool feature: that bit with the join and URI.escape at the top lets you use it like so:

weather san francisco, ca

"san", "francisco,", and "ca" will be passed as separate arguments, but the line at the top will join them together and convert the resulting string into something you could type in a URL bar. (ie: spaces -> %20).

Makes it easy to be sloppy on the command line. :)

Saturday, March 29, 2008

More ruby: comics

My next foray into Ruby (see previous post) came to me while I was catching up on my favorite web comics over break. While ordinarily not much of a hassle, some of the pages were taking especially long to load all of the images, most of which were not the comic I actually cared about. Wouldn't it be nice to have a script that made me a page with just the comics themselves?

It turned out to be pretty straightforward. Here's the result: comics.rb

I then created a simple bash script to make loading comics.html into a browser cleaner:

#!/bin/bash
# Generate an updated comics page and load it in browser du jour

BROWSER='firefox -new-window'

ruby ~/bin/comics/comics.rb
$BROWSER comics.html &

Friday, March 28, 2008

Some nifty ruby scripts

Update: A better version of the weather script is discussed above

Lately, I've revisited ruby for various small code projects. It's virtues are extolled at length here, so I won't bother other than to say I like it. The following programs are some practical examples of ruby (though not the most elegant ways of doing so), and are just plain useful (at least to me).

Weather
I wanted to fetch weather forecasts for my area (or any other I happened to be visiting). There are several weather reporters available, and I've tried many of them. However, they were all in widget form, and tied to some desktop environment, dock, or some such. I decided to create one I could call from the command line (handy, since I soon moved to awesomewm, which has no desktop or dock to speak of. More on that another time.)

My weather reporter downloads two different pages off of weather.com (the "now" page and the "forecast" page), scans them for the relevant temperatures, and displays them neatly on the screen (with ASCII colors :). It defaults to San Luis Obispo (my current home base), but will take a zip code as an argument.

The code: weather-old.rb

Battery
For similar reasons to the weather, I wanted a script to give me a simple battery readout, perfect for status bars or command lines. After a bit of research, I found that (in Linux) the battery information is stored in various files in /proc/acpi/battery/BAT#. It was just a matter of scanning these files, doing a bit of division, and formatting the output.

Here is the code: battery.rb