#!/usr/bin/env ruby # -*- ruby -*- # # Reads an RSS feed from the given website, e.g. # # % read digg # % read digg.com # % read http://digg.com # % read http://digg.com/rss/index.xml # # All the same... # require 'net/http' # -------------------------------------------------- # Classes # -------------------------------------------------- class Entry attr_reader :title, :url, :description def initialize(title,url,description) @title = title @url = url @description = description end end # -------------------------------------------------- # Functions # -------------------------------------------------- def log(msg) STDERR.puts "*** " + msg if $verbose end def display(name,host,page) # EEEEK!!! This is the grossest, nastiest RSS parser ever # (just saying!) # if we start with http:// change the host and page if page.match /^http\:\/\// new_host = page.gsub /^http\:\/\//, "" new_page = new_host.gsub /^[^\/]+\//, "/" new_host = new_host.gsub /\/.*$/, "" host = new_host page = new_page end log host + ":" + page h = Net::HTTP.new host, 80 resp, data = h.get page, nil if resp.message == "OK" data = data.gsub /\n/, "" lines = data.split /(.*)/ do |res| title = res[0] end x.scan /^link>(.*)/ do |res| url = res[0] end x.scan /^description>(.*)/ do |res| description = res[0] end if title and url and description e = Entry.new title, url, description entries.push e title = nil url = nil description = nil end end # Display the results show_entries entries else puts "ERROR: #{resp}" end end def show_entries(entries) done = false while !done i = 1 entries.each do |e| s = "" s += " " if i<10 tab = " " tab += " " if i<10 puts "[" + i.to_s + "] " + e.title puts tab + "( " + e.url + " )" puts i += 1 end entry = nil while true print "Please choose a number between 1 and " + (i-1).to_s + " or 'q' to quit> " STDOUT.flush num = STDIN.readline if num.match /^q/ done = true break end begin n = num.to_i if n<1 STDERR.puts "!!! " + n.to_s + " must be > 0" elsif n>i STDERR.puts "!!! " + n.to_s + " must be < " + i.to_s else entry = entries[n-1] break end rescue end end # open the URL "Openning " + entry.title + "..." exec "open", entry.url end end def process(s) host = s page = "/" # maybe remove http:// host = host.gsub /^http\:\/\//, "" # maybe add an extension host += ".com" if !host.match /\.\w\w\w\/?$/ # maybe add www host = "www." + host if !host.match /^www\./ # try to find the RSS feed log host + ":" + page h = Net::HTTP.new host, 80 resp, data = h.get page, nil if resp.message == "OK" lines = data.split /