Brand new Uproar repository.
[uproar.git] / Uproar.rb
blob97eec56522235c1e43eb80b1785e958d102430af
1 #!/usr/bin/env ruby
2 # uproar.rb - search a YAML-encoded list of links for matching tags
3 # version 0.2
4 # see http://ardekantur.com/misc/uproar for information.
6 %w[ rubygems yaml open-uri hpricot ].each { |x| require x }
8 def set_option hash, name, default
9   i = ARGV.index(name)
10   hash[name] = i ? ARGV[i + 1] : default
11 end
13 def search_tags file, tag
14   links = File.open( file )
15   db = []
16   YAML::load_documents( links ) { |doc| db << doc }
17   
18   matches = []
19   db.each { |l| matches << l if l['tags'].include? tag }
20   matches.each { |l| printf "%s\n %s\n", l['title'], l['link'] }
21 end
23 def add_link file, address, tags
24 begin
25   doc = Hpricot(open(address))
26   title = doc.at("title").inner_html.strip
27   puts "Adding '#{title}' to #{file}..."
28 rescue
29   $stderr.puts "site could not be reached."
30   return
31 end
33   links = File.new( file, "a" )
34   links.puts "---"
35   links.puts "title: '#{title.gsub(/'/, '\'\'')}'"
36   links.puts "link: #{address}"
37   tags = tags ? tags.strip.gsub(/ /, ', ') : ''
38   links.puts "tags: [#{tags}]"
39   links.close
41 end
43 o = {}
44 set_option o, '-s', nil
45 set_option o, '-f', 'Links.yaml'
46 set_option o, '-t', ''
47 set_option o, '-a', nil
49 if (!o['-s'] and !o['-a']) or (o['-s'] and o['-a'])
50   $stderr.puts "please specify either a tag to search for or a link to add."
51   return
52 end
54 search_tags o['-f'], o['-s'] if o['-s']
55 add_link o['-f'], o['-a'], o['-t'] if o['-a']