Tag Archives: command line

Ruby script to show the (Dutch) weather forecasts in your console

Just a quick ruby script to show the weather forecasts for next week using the html screen scraping gem nokogiri and a table formatter gem called hirb.

require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'hirb'

table = []
headers = []
page = Nokogiri::HTML(open("http://knmi.nl/waarschuwingen_en_verwachtingen/"))
page.css(".realtable")[0].xpath("tr").each do |tr|
  row = []
  tr.xpath("th").each do |th|
    headers << th.text
  end
  tr.xpath("td").each do |th|
    row << th.text
  end
  table << row if row.count > 0
end

puts Hirb::Helpers::AutoTable.render table, :headers=>headers, :description=>false

The result in my ZSH (using theme agnoster) looks like this:

knmi

Command line Twitter

Just for the ‘geekiness’ of it I was looking for a way to use Twitter from the Bash shell prompt.

To read your twitter home timeline:

curl -s -u userid:password -g http://api.twitter.com/1/statuses/home_timeline.atom 
| xpath -q -e '/feed/entry/title/text()' 
| tac

Curl is a tool to transfer data from and to a server. The -s option is to keep it silent. -u userid:password is used to send your twitter account/password (warning: this is HTTP basic authentication so not secure). -g is used to get the date from the specified url, in this case the data is in atom format (XML). Pipe the output to the tool xpath (-q to keep it quiet) and select only the title of the entries. Pipe the output to tac (cat in reverse) to reverse the list of lines so that the latest tweets are at the bottom (as this is more natural at the command line, no need to scroll up to see the latest updates), one at a line (every title is prefixed by the sender). The result is extremely readable (at least if the ones you follow know how to express themselves).

And to post a tweet:

curl -u userid:password -d status="Hello world." http://twitter.com/statuses/update.xml

The web interface of Twitter is much more convenient but at least now I know how to use the tools curl and xpath.