manveru (owner)

Revisions

gist: 80970 Download_button fork
public
Public Clone URL: git://gist.github.com/80970.git
Embed All Files: show embed
sm #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby
 
# Small CLI tool to make adding selfmarks easier.
#
# @see http://sm.purepistos.net
 
options = {'host' => 'http://sm.purepistos.net/uri/add_window_add'}
key_file = File.expand_path("~/.config/sm.key")
 
require 'optparse'
 
op = OptionParser.new{|o|
  o.on('-u', '--url STRING', 'URL of the page you want to selfmark'
      ){|url| options['uri'] = url }
  o.on('-m', '--title STRING', 'Page title (automatic if Hpricot available)'
      ){|title| options['title'] = title }
  o.on('-n', '--note STRING', 'Notes to yourself about the page'
      ){|note| options['notes'] = note }
  o.on('-t', '--tags tag1,tag2,tag3', Array, 'Tags associated with the page'
      ){|tags| options['tags'] = tags }
 
  o.separator ' '
 
  o.on('-k', '--key', "API key, else fallback to --file-key"
      ){|key| options['api_key'] = key }
  o.on('-f', '--key-file', "Use contents of #{key_file} as API key"
      ){|file| key_file = file }
  o.on('-H', '--host', 'URL of the uri/add_window_add of your selfmarks'
      ){|host| options['host'] = host }
  o.on('-q', '--quiet', "Only inform about errors"
      ){ options['quiet'] = true }
 
  o.separator ' '
 
  o.on('-h', '--help', 'Display this list'
      ){ puts o; exit }
  o.on('-v', '--version', "I'll tell you my version"
      ){ puts 'sm 2009.03.18 © manveru'; exit }
}
 
if ARGV.empty?; puts op; exit 1; end
op.parse!
 
options['api_key'] ||= File.open(key_file){|io| io.gets.strip }
options['uri'] ||= ARGV.shift
 
fail "No url given" unless options['uri']
fail "No API key found" unless options['api_key']
 
begin # try to get the title of the page via hpricot
  require 'hpricot'
  require 'open-uri'
 
  options['title'] = Hpricot(open(options['uri'])).at(:title).inner_text.strip
rescue LoadError
  fail "Please supply a title or install hpricot"
end unless options['title']
 
host = URI(options.delete('host'))
options['tags'] = [*options['tags']].join(',')
 
response = Net::HTTP.post_form(host, options)
 
message = response.body.tr('{()}"', ' ').strip
fail(message) if message =~ /^error/
puts message unless options['quiet']