pinboard-cli 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/bin/pinboard +173 -0
  2. metadata +65 -0
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'time'
5
+ require 'yajl/json_gem'
6
+ require 'ostruct'
7
+ require 'optparse'
8
+ require 'pinboard'
9
+
10
+ CACHE_FILE = File.expand_path("~/.pinboard-cache.json")
11
+ TOKEN_FILE = File.expand_path("~/.pinboard-token")
12
+
13
+ Color = OpenStruct.new
14
+ if $stdout.tty?
15
+ COLORS = {
16
+ clear: "\033[0m",
17
+ red: "\033[31m",
18
+ green: "\033[32m",
19
+ yellow: "\033[33m",
20
+ cyan: "\033[36m",
21
+ grey: "\033[1;30m",
22
+ purple: "\033[35m"
23
+ }
24
+ else
25
+ COLORS = {}
26
+ end
27
+
28
+ $options = {
29
+ what_to_search: :description,
30
+ and_search: true,
31
+ color: true,
32
+ update: false
33
+ }
34
+
35
+ optparser = OptionParser.new do |opts|
36
+ opts.banner = "Usage: #{File.basename $0} [options] [keywords...]"
37
+
38
+ opts.separator ""
39
+ opts.separator "Configuration:"
40
+ opts.separator " Put your Pinboard API key into #{TOKEN_FILE}"
41
+ opts.separator " You can find that key here: https://pinboard.in/settings/password"
42
+ opts.separator ""
43
+
44
+ opts.on('-a', '--all', 'Search everything (href, description, extended, tags)') do
45
+ $options[:what_to_search] = :all
46
+ end
47
+
48
+ opts.on('-d', '--description', 'Search only description (default)') do
49
+ $options[:what_to_search] = :description
50
+ end
51
+
52
+ opts.on('-e', '--extended', 'Search only extended') do
53
+ $options[:what_to_search] = :extended
54
+ end
55
+
56
+ opts.on('-t', '--tags', 'Search only tags') do
57
+ $options[:what_to_search] = :tags
58
+ end
59
+
60
+ opts.on('-o', '--or', 'OR keywords') do
61
+ $options[:and_search] = false
62
+ end
63
+
64
+ opts.on('-u', '--update', 'Update if neccessary') do
65
+ $options[:update] = true
66
+ end
67
+
68
+ opts.on('-n', '--[no-]color', 'Suppress color output') do |color|
69
+ $options[:color] = color
70
+ end
71
+ end
72
+
73
+ optparser.parse!(ARGV)
74
+
75
+ def colorize color, word
76
+ if $options[:color]
77
+ "#{COLORS[color]}#{word}#{COLORS[:clear]}"
78
+ else
79
+ word
80
+ end
81
+ end
82
+
83
+ def save_cache file, cache
84
+ File.open(file, "w") { |f|
85
+ f.write cache.to_json
86
+ }
87
+ end
88
+
89
+ cached = JSON.parse(IO.read(CACHE_FILE)) rescue {}
90
+
91
+ token = IO.read(TOKEN_FILE).chomp
92
+
93
+ need_update = $options[:update] || cached.empty?
94
+
95
+ if need_update
96
+ print "Checking for an update... "
97
+ pin = Pinboard::Client.new token: token
98
+ last_check = DateTime.parse(cached['time'] || "01.01.1970").to_time
99
+ last_update = pin.update
100
+
101
+ if last_update > last_check
102
+ puts colorize(:red, 'Data outdated. Start with -u to update.')
103
+ puts colorize(:green, 'Oh, you did. Updating now.')
104
+ cached['time'] = last_update
105
+
106
+ posts = pin.posts
107
+ cached['posts'] = JSON.parse(posts.to_json)
108
+ else
109
+ puts colorize(:green, "not needed")
110
+ end
111
+ puts
112
+ end
113
+
114
+ save_cache(CACHE_FILE, cached)
115
+
116
+ def all args, post, regex
117
+ description(args, post, regex) ||
118
+ extended(args, post, regex) ||
119
+ post.href =~ regex
120
+ end
121
+
122
+ def description args, post, regex
123
+ if $options[:and_search]
124
+ args.all? { |arg|
125
+ post.description =~ /#{arg}/i
126
+ }
127
+ else
128
+ post.description =~ regex
129
+ end
130
+ end
131
+
132
+ def extended args, post, regex
133
+ if $options[:and_search]
134
+ args.all? { |arg|
135
+ post.extended =~ /#{arg}/i
136
+ }
137
+ else
138
+ post.extended =~ regex
139
+ end
140
+ end
141
+
142
+ def tags args, post, regex
143
+ if $options[:and_search]
144
+ args.all? { |arg|
145
+ post.tag.any? { |tag|
146
+ tag =~ /#{arg}/
147
+ }
148
+ }
149
+ else
150
+ post.tag.any? { |tag|
151
+ tag =~ regex
152
+ }
153
+ end
154
+ end
155
+
156
+ def build_regex args
157
+ regex = ARGV.map { |arg| Regexp.escape arg }.join("|")
158
+ regex = /#{regex}/i
159
+ end
160
+
161
+ unless ARGV.empty?
162
+ regex = build_regex(ARGV)
163
+
164
+ cached['posts'].each do |args|
165
+ args = Pinboard::Util.symbolize_keys(args)
166
+ args[:tag] = args[:tag].join(" ")
167
+ post = Pinboard::Post.new args
168
+
169
+ if send($options[:what_to_search], ARGV, post, regex)
170
+ puts "#{colorize :green, post.description}:\n\t#{post.href}"
171
+ end
172
+ end
173
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pinboard-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan-Erik Rediger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pinboard
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.1.1
30
+ description: ! ' Search through your pinboard bookmarks on the command-line."
31
+
32
+ '
33
+ email: badboy@archlinux.us
34
+ executables:
35
+ - pinboard
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - bin/pinboard
40
+ homepage: http://github.com/badboy/pinboard-cli
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.25
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Pinboard on your commandline
64
+ test_files: []
65
+ has_rdoc: false