poddb_client 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Daniel Choi, http://danielchoi.com/software/
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/lib/interactive.vim CHANGED
@@ -1,5 +1,10 @@
1
1
  let s:client_cmd = "ruby -Ilib bin/poddb"
2
- let s:base_url = "http://localhost:3000"
2
+ if !exists($PODDB_SERVER)
3
+ let g:poddb_base_url = $PODDB_SERVER
4
+ else
5
+ let g:poddb_base_url = "http://poddb.com"
6
+ endif
7
+
3
8
 
4
9
  let s:download_and_play_cmd = s:client_cmd." --download-and-play "
5
10
 
@@ -31,7 +36,8 @@ function! s:main_window()
31
36
  noremap <buffer> <c-j> :call <SID>show_next_item(0)<CR>
32
37
  noremap <buffer> <c-k> :call <SID>show_next_item(1)<CR>
33
38
  noremap <buffer> f :call <SID>favorite_this_podcast()<CR>
34
- autocmd BufEnter <buffer> :setlocal nowrap
39
+ autocmd BufEnter <buffer> :setlocal nowrap | let s:listbufnr = bufnr('')
40
+ autocmd BufWinEnter <buffer> :only
35
41
  noremap <buffer> <Leader>? :call <SID>help()<CR>
36
42
  endfunction
37
43
 
@@ -42,6 +48,8 @@ function! s:item_window()
42
48
  noremap <buffer> <c-j> :call <SID>show_next_item(0)<CR>
43
49
  noremap <buffer> <c-k> :call <SID>show_next_item(1)<CR>
44
50
  noremap <buffer> d :call <SID>mark_for_download()<cr>
51
+ nnoremap <buffer> o :call <SID>find_next_href_and_open()<CR>
52
+ nnoremap <buffer> O :call <SID>open_all_hrefs()<CR>
45
53
  close
46
54
  endfunction
47
55
 
@@ -56,11 +64,13 @@ function! s:show_item()
56
64
  if (itemId == '')
57
65
  return
58
66
  endif
59
- let command = "curl -s '".s:base_url."/item/".itemId."'"
67
+ let command = "curl -s '".g:poddb_base_url."/item/".itemId."'"
60
68
  let res = system(command)
61
69
  call s:focus_window(s:itembufnr)
70
+ setlocal modifiable
62
71
  silent! 1,$delete
63
72
  silent! put! =res
73
+ setlocal nomodifiable
64
74
  normal 1G
65
75
  wincmd p
66
76
  endfunc
@@ -116,7 +126,7 @@ function! s:show_podcast_items(podcastId)
116
126
  if (podcastId == '')
117
127
  return
118
128
  endif
119
- let command = "curl -s '".s:base_url."/podcast/".podcastId."/items'"
129
+ let command = "curl -s '".g:poddb_base_url."/podcast/".podcastId."/items'"
120
130
  let contents = system(command)
121
131
  if contents =~ 'No matches\c'
122
132
  echom "No items!"
@@ -197,14 +207,7 @@ function! s:show_next_item(previous)
197
207
  if s:is_podcast_list()
198
208
  return
199
209
  end
200
- let origbufnr = bufnr('%')
201
- let fullscreen = (bufwinnr(s:listbufnr) == -1) " we're in full screen message mode
202
- if fullscreen
203
- split
204
- exec 'b'. s:listbufnr
205
- else
206
- call s:focus_window(s:listbufnr)
207
- end
210
+ call s:focus_window(s:listbufnr)
208
211
  if a:previous
209
212
  normal k
210
213
  else
@@ -212,9 +215,6 @@ function! s:show_next_item(previous)
212
215
  endif
213
216
  call s:show_item()
214
217
  normal zz
215
- if origbufnr == s:itembufnr
216
- wincmd p
217
- endif
218
218
  normal 0
219
219
  redraw
220
220
  endfunction
@@ -225,9 +225,53 @@ function! s:help()
225
225
  echo res
226
226
  endfunction
227
227
 
228
+ " ------------------------------------------------------------------------
229
+ " open links in external browser
230
+
231
+ if !exists("g:Poddb#browser_command")
232
+ for cmd in ["gnome-open", "open"]
233
+ if executable(cmd)
234
+ let g:Poddb#browser_command = cmd
235
+ break
236
+ endif
237
+ endfor
238
+ if !exists("g:Poddb#browser_command")
239
+ echom "Can't find the to open your web browser."
240
+ endif
241
+ endif
242
+
243
+ let s:http_link_pattern = 'https\?:[^ >)\]]\+'
244
+
245
+ func! s:open_href_under_cursor()
246
+ let href = matchstr(expand("<cWORD>") , s:http_link_pattern)
247
+ let command = g:Poddb#browser_command . " '" . shellescape(href) . "' "
248
+ call system(command)
249
+ endfunc
250
+
251
+ func! s:find_next_href_and_open()
252
+ let res = search(s:http_link_pattern, 'cw')
253
+ if res != 0
254
+ call s:open_href_under_cursor()
255
+ endif
256
+ endfunc
257
+
258
+ func! s:open_all_hrefs()
259
+ let n = 0
260
+ let line = search(s:http_link_pattern, 'cw')
261
+ while line
262
+ call s:open_href_under_cursor()
263
+ let n += 1
264
+ let line = search(s:http_link_pattern, 'W')
265
+ endwhile
266
+ echom 'opened '.n.' links'
267
+ endfunc
268
+
269
+ " ------------------------------------------------------------------------
270
+
228
271
 
229
272
  call s:main_window()
230
273
  call s:item_window()
231
274
 
232
275
  normal 3G
233
276
 
277
+
@@ -20,7 +20,7 @@ class PoddbClient
20
20
 
21
21
  filename_suffix = File.extname(URI.parse(enclosure_url).path)
22
22
 
23
- @filename = "%s.%s.poddb%s%s" % [podcast_fragment, title_fragment, item_id.to_s, filename_suffix]
23
+ @filename = "%s.%s.poddb_%d_%d%s" % [podcast_fragment, title_fragment, podcast[:podcast_id], item_id, filename_suffix]
24
24
  puts "Downloading #{enclosure_url} as #{@filename}"
25
25
  cmd = "wget -O #{@filename} '#{enclosure_url}' && touch #{@filename}"
26
26
  `#{cmd}`
@@ -1,3 +1,3 @@
1
1
  class PoddbClient
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/poddb_client.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  require 'poddb_client/downloading'
2
+ require 'poddb_client/version'
2
3
  require 'cgi'
3
4
  require 'optparse'
4
5
  require 'net/http'
5
6
 
6
7
  class PoddbClient
7
8
 
8
- # TODO: change to poddb.com
9
- SERVER = "http://localhost:3000"
9
+ # TODO: set for production
10
+ SERVER = ENV['PODDB_SERVER'] || "http://poddb.com"
10
11
 
11
12
  PODDB_DIR = "%s/.poddb" % ENV['HOME']
12
13
  CACHE_DIR = "%s/.poddb/cache" % ENV['HOME']
@@ -23,7 +24,10 @@ class PoddbClient
23
24
  def initialize(args)
24
25
  @args = args
25
26
  @options = {}
27
+ @params = ["v=#{PoddbClient::VERSION}" ]
26
28
  @outfile = ITEM_LIST_OUTFILE # changed only for podcast list
29
+ @version = PoddbClient::VERSION
30
+ @query = []
27
31
  parse_options
28
32
  end
29
33
 
@@ -41,8 +45,11 @@ class PoddbClient
41
45
  opts.on("-a", "--add PODCAST_URL", "Add podcast with PODCAST_URL to the poddb database") do |podcast_url|
42
46
  @add_podcast = podcast_url
43
47
  end
44
- opts.on("-l", "--list", "List all podcasts in the poddb database", "(If query is supplied, will return matching podcasts)") do
48
+ opts.on("-l", "--list [QUERY]", "List all podcasts in the poddb database", "(If QUERY is supplied, will return matching podcasts)") do |query|
45
49
  @list_podcasts = true
50
+ if query
51
+ @query << query
52
+ end
46
53
  end
47
54
  opts.on("-F", "--favorite-podcasts", "Show favorite podcasts") do
48
55
  if ! File.size?(FAVORITE_PODCASTS_FILE)
@@ -51,8 +58,14 @@ class PoddbClient
51
58
  end
52
59
  @list_favorite_podcasts = true
53
60
  end
61
+ opts.on("-o", "--order ORDER", "Sort results by ORDER", "The only option right now is 'popular'. Default order is pubdate.") do |order|
62
+ @params << "o=#{order}"
63
+ end
64
+ opts.on("-d", "--days DAYS", "Limit results to items published since DAYS days ago") do |days|
65
+ @params << "d=#{days}"
66
+ end
54
67
  opts.on("-t", "--type MEDIA_TYPE", "Return items of MEDIA_TYPE only (audio,video)") do |media_type|
55
- @media_type_param = "&media_type=#{media_type}"
68
+ @params << "t=#{media_type}"
56
69
  end
57
70
  opts.on("--download-and-play ITEM_ID", "Download item and play with PODDB_MEDIA_PLAYER") do |item_id|
58
71
  puts "Download and play #{item_id}"
@@ -66,12 +79,16 @@ class PoddbClient
66
79
  exit
67
80
  end
68
81
  opts.on_tail("-v", "--version", "Show version number") do
69
- require 'poddb_client/version'
70
82
  puts "poddb #{VERSION}"
71
83
  exit
72
84
  end
73
85
  end.parse!(@args)
74
- @query = CGI::escape(@args.join(' ').strip)
86
+ @query = @query.concat @args
87
+ q = CGI::escape(@query.join(' ').strip)
88
+ if q != ''
89
+ @params << "q=#{q}"
90
+ end
91
+ @params = @params.empty? ? '' : @params.join('&')
75
92
  end
76
93
 
77
94
  def run
@@ -91,11 +108,14 @@ class PoddbClient
91
108
  end
92
109
 
93
110
  def add_podcast
94
- puts "Adding podcast..."
95
- res = Net::HTTP.post_form(URI.parse("#{SERVER}/podcasts"),
96
- 'url' => @add_podcast)
97
- # TODO improve response
98
- puts res.body
111
+ puts "Adding podcast with url: #{@add_podcast}"
112
+ res = Net::HTTP.post_form(URI.parse("#{SERVER}/podcasts"), 'url' => @add_podcast).body
113
+ if res =~ /^Error/
114
+ puts res
115
+ else
116
+ podcast_id, title = res.split(/\s+/, 2)
117
+ add_to_favorite_podcasts(podcast_id)
118
+ end
99
119
  end
100
120
 
101
121
  def interactive
@@ -103,13 +123,12 @@ class PoddbClient
103
123
  puts @output
104
124
  exit
105
125
  end
106
- if @output =~ /^No matches/i
126
+ if @output =~ /^No matches/i || @output =~ /^Error/
107
127
  puts @output
108
128
  exit
109
129
  end
110
-
111
130
  File.open(@outfile, 'w') {|f| f.puts @output }
112
- cmd = "vim -S #{VIMSCRIPT} #{@outfile} #{@poddb_env}"
131
+ cmd = "export PODDB_SERVER=#{SERVER} && vim -S #{VIMSCRIPT} #{@outfile} "
113
132
  puts cmd
114
133
  system(cmd)
115
134
  if download_and_play?
@@ -123,9 +142,9 @@ class PoddbClient
123
142
  def list_podcasts
124
143
  @outfile = PODCAST_LIST_OUTFILE
125
144
  @output = if @list_podcasts
126
- `curl -s #{SERVER}/podcasts?q=#{@query}`
145
+ `curl -s '#{SERVER}/podcasts?#@params'`
127
146
  elsif @list_favorite_podcasts
128
- `curl -s #{SERVER}/podcasts?podcast_ids=#{favorite_podcast_ids.join(',')}`
147
+ `curl -s '#{SERVER}/podcasts?podcast_ids=#{favorite_podcast_ids.join(',')}'`
129
148
  end
130
149
  if File.size?(FAVORITE_PODCASTS_FILE)
131
150
  @output = @output.split("\n").map {|line|
@@ -140,12 +159,12 @@ class PoddbClient
140
159
  end
141
160
 
142
161
  def items_from_favorites
143
- @output = `curl -s '#{SERVER}/items?podcast_ids=#{favorite_podcast_ids.join(',')}#{@media_type_param}'`
162
+ @output = `curl -s '#{SERVER}/items?podcast_ids=#{favorite_podcast_ids.join(',')}&#@params'`
144
163
  mark_already_downloaded
145
164
  end
146
165
 
147
166
  def search
148
- @output = `curl -s '#{SERVER}/search?q=#{@query}#{@media_type_param}'`
167
+ @output = `curl -s '#{SERVER}/search?#@params'`
149
168
  mark_already_downloaded
150
169
  end
151
170
 
@@ -157,14 +176,28 @@ private
157
176
 
158
177
  def favorite_podcast_ids
159
178
  if File.size?(FAVORITE_PODCASTS_FILE)
160
- File.readlines(FAVORITE_PODCASTS_FILE).map(&:strip).select {|x| x != ''}
179
+ File.readlines(FAVORITE_PODCASTS_FILE).map(&:strip).select {|x| x =~ /\d+/}.map {|x| x.to_i}
161
180
  else
162
181
  []
163
182
  end
164
183
  end
165
184
 
185
+ def add_to_favorite_podcasts(podcast_id)
186
+ if File.size?(FAVORITE_PODCASTS_FILE) && File.read(FAVORITE_PODCASTS_FILE).split("\n").any? {|line| line.strip == podcast_id.to_s}
187
+ return
188
+ end
189
+ if podcast_id !~ /\d/
190
+ return
191
+ end
192
+ podcast_id = podcast_id.to_i
193
+ File.open(FAVORITE_PODCASTS_FILE, 'a') {|f|
194
+ f.puts podcast_id
195
+ }
196
+ puts "Added '#{title}' [##{podcast_id}] to your favorite podcasts. Type `poddb -F` to show your favorites."
197
+ end
198
+
166
199
  def mark_already_downloaded
167
- @downloaded_item_ids = Dir['*poddb*'].map { |f| f[/poddb(\d+)/,1] }.compact
200
+ @downloaded_item_ids = Dir['*poddb_*'].map { |f| f[/poddb_\d+_(\d+)/,1] }.compact
168
201
  @output = @output.split(/\n/).map {|line|
169
202
  item_id = line[/\d+$/,0]
170
203
  if @downloaded_item_ids.include?(item_id)
metadata CHANGED
@@ -1,29 +1,25 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: poddb_client
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
4
5
  prerelease:
5
- version: 0.1.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Daniel Choi
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-09-13 00:00:00 -04:00
14
- default_executable:
12
+ date: 2011-09-15 00:00:00.000000000Z
15
13
  dependencies: []
16
-
17
14
  description: Podcast aggregation from the command line
18
- email:
15
+ email:
19
16
  - dhchoi@gmail.com
20
- executables:
17
+ executables:
21
18
  - poddb
22
19
  extensions: []
23
-
24
20
  extra_rdoc_files: []
25
-
26
- files:
21
+ files:
22
+ - MIT-LICENSE.txt
27
23
  - NOTES
28
24
  - README.markdown
29
25
  - Rakefile
@@ -33,36 +29,32 @@ files:
33
29
  - lib/poddb_client/downloading.rb
34
30
  - lib/poddb_client/version.rb
35
31
  - poddb_client.gemspec
36
- has_rdoc: true
37
32
  homepage: http://danielchoi.com/software/poddb_client.html
38
33
  licenses: []
34
+ post_install_message: ! '*******************************************************************
39
35
 
40
- post_install_message: |-
41
- *******************************************************************
42
36
  ** Now please run poddb_client-install to install the Vim plugin **
43
- *******************************************************************
44
- rdoc_options: []
45
37
 
46
- require_paths:
38
+ *******************************************************************'
39
+ rdoc_options: []
40
+ require_paths:
47
41
  - lib
48
- required_ruby_version: !ruby/object:Gem::Requirement
42
+ required_ruby_version: !ruby/object:Gem::Requirement
49
43
  none: false
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
53
47
  version: 1.8.6
54
- required_rubygems_version: !ruby/object:Gem::Requirement
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
49
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- version: "0"
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
60
54
  requirements: []
61
-
62
55
  rubyforge_project: poddb_client
63
- rubygems_version: 1.6.1
56
+ rubygems_version: 1.8.10
64
57
  signing_key:
65
58
  specification_version: 3
66
59
  summary: Podcast aggregation from the command line
67
60
  test_files: []
68
-