Created
December 12, 2009 15:40
-
-
Save tily/254940 to your computer and use it in GitHub Desktop.
simple CUI browser with Nokogiri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# browse : browser web sites by xpath or css expression | |
require 'cgi' | |
require 'open-uri' | |
require 'rubygems' | |
require 'nokogiri' | |
config = YAML.load(DATA.read) | |
command = ARGV.shift | |
siteinfo = config['command'][command] || config['command'][config['shortcut'][command]] | |
if !siteinfo || command == 'help' | |
puts 'usage: browse [command|shortcut] [param]' | |
puts 'shortcut/command:' | |
puts config['shortcut'].map {|k,v| " * #{k}/#{v}" } | |
exit | |
end | |
url = sprintf(siteinfo['url'], ARGV.map{|arg| CGI.escape(arg)}) | |
exp = siteinfo['exp'] | |
Nokogiri::HTML.parse(open url).search(exp).to_a.reverse.each do |entry| | |
if sub = siteinfo['sub'] | |
sub.each_with_index do |sub, idx| | |
if sub['join'] | |
text = entry.search(sub['exp']).to_a.join(sub['join']) | |
else | |
text = entry.search(sub['exp']).text.gsub(/(^\s+|\n)/, '') | |
end | |
puts "#{' '*2*idx} * #{text}" | |
end | |
else | |
puts ' * ' + entry.text.gsub(/(^\s+|\n)/, '') | |
end | |
end | |
__END__ | |
shortcut: | |
s: twitter_search | |
u: twitter_user | |
f: favotter | |
command: | |
twitter_search: | |
url: 'http://search.twitter.com/search?q=%s' | |
exp: '.msg' | |
twitter_user: | |
url: 'http://twitter.com/%s' | |
exp: '.entry-content' | |
favotter: | |
url: 'http://favotter.matope.com/user.php?user=%s' | |
exp: '//div[@class="entry xfolkentry hentry "]' | |
sub: | |
- exp: './div/span[@class=" status_text description"]' | |
- exp: './/span[@class="favotters"]/a/img/@title' | |
join: ' | ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment