Class: PoddbClient
- Inherits:
-
Object
- Object
- PoddbClient
- Includes:
- Downloading
- Defined in:
- lib/poddb_client.rb,
lib/poddb_client/version.rb,
lib/poddb_client/downloading.rb
Defined Under Namespace
Modules: Downloading
Constant Summary collapse
- SERVER =
"http://96.126.108.206"
- PODDB_DIR =
"%s/.poddb" % ENV['HOME']
- CACHE_DIR =
"%s/.poddb/cache" % ENV['HOME']
- VIMSCRIPT =
File.join(File.(File.dirname(__FILE__)), 'interactive.vim')
- ITEM_LIST_OUTFILE =
"#{CACHE_DIR}/main.itemlist"
- PODCAST_LIST_OUTFILE =
"#{CACHE_DIR}/main.podcastlist"
- FAVORITE_PODCASTS_FILE =
"#{PODDB_DIR}/favorites"
- DOWNLOAD_AND_PLAY_FILE =
"#{CACHE_DIR}/download_and_play"
- VERSION =
'0.2.8'
Constants included from Downloading
Instance Method Summary collapse
- #add_podcast ⇒ Object
- #cleanup ⇒ Object
- #curl(path) ⇒ Object
-
#initialize(args) ⇒ PoddbClient
constructor
A new instance of PoddbClient.
- #interactive ⇒ Object
- #items_from_favorites ⇒ Object
- #list_podcasts ⇒ Object
- #parse_options ⇒ Object
- #run ⇒ Object
- #search ⇒ Object
Methods included from Downloading
#download, #download_and_play, #download_and_play?, #download_marked_items, #titleize
Constructor Details
#initialize(args) ⇒ PoddbClient
Returns a new instance of PoddbClient.
30 31 32 33 34 35 36 37 38 |
# File 'lib/poddb_client.rb', line 30 def initialize(args) @args = args @options = {} @params = ["v=#{PoddbClient::VERSION}" ] @outfile = ITEM_LIST_OUTFILE # changed only for podcast list @version = PoddbClient::VERSION @query = [] end |
Instance Method Details
#add_podcast ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/poddb_client.rb', line 121 def add_podcast puts "Adding podcast with url: #{@add_podcast}" res = Net::HTTP.post_form(URI.parse("#{SERVER}/podcasts"), 'url' => @add_podcast).body if res =~ /^Error/ puts res else podcast_id, title = res.split(/\s+/, 2) add_to_favorite_podcasts(podcast_id, title) end end |
#cleanup ⇒ Object
181 182 183 |
# File 'lib/poddb_client.rb', line 181 def cleanup `rm -rf #{CACHE_DIR}/*` end |
#curl(path) ⇒ Object
186 187 188 189 190 |
# File 'lib/poddb_client.rb', line 186 def curl(path) @output = `curl -s '#{SERVER}/#{path}'` @output = @output.force_encoding("UTF-8") @output = @output.encode("UTF-8", undef: :replace, invalid: :replace) end |
#interactive ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/poddb_client.rb', line 132 def interactive if !STDOUT.tty? puts @output exit end if @output =~ /^No matches/i || @output =~ /^Error/ puts @output exit end File.open(@outfile, 'w') {|f| f.puts @output } cmd = "export PODDB_SERVER=#{SERVER} && vim -S #{VIMSCRIPT} #{@outfile} " system(cmd) if download_and_play? download_and_play else download_marked_items end cleanup end |
#items_from_favorites ⇒ Object
171 172 173 174 |
# File 'lib/poddb_client.rb', line 171 def items_from_favorites @output = curl "/items?podcast_ids=#{favorite_podcast_ids.join(',')}&#@params" mark_already_downloaded end |
#list_podcasts ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/poddb_client.rb', line 152 def list_podcasts @outfile = PODCAST_LIST_OUTFILE @output = if @list_podcasts curl "/podcasts?#@params" elsif @list_favorite_podcasts curl "/podcasts?podcast_ids=#{favorite_podcast_ids.join(',')}" end if File.size?(FAVORITE_PODCASTS_FILE) @output = @output.split("\n").map {|line| # podcast_ids here are strings if (podcast_id = line[/\d+$/,0]) && favorite_podcast_ids.detect{|i| i.to_s == podcast_id} line.sub(/^ /, "@") else line end }.join("\n") end end |
#parse_options ⇒ Object
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/poddb_client.rb', line 40 def OptionParser.new do |opts| opts. = "Usage: poddb [options] [query]" opts.separator "" opts.on("-f", "--from-favorites", "Show all recent items from favorite podcasts") do if ! File.size?(FAVORITE_PODCASTS_FILE) puts "No podcasts found in #{FAVORITE_PODCASTS_FILE}" exit end @items_from_favorites = true end opts.on("-a", "--add PODCAST_URL", "Add podcast with PODCAST_URL to the poddb database") do |podcast_url| @add_podcast = podcast_url end opts.on("-l", "--list [QUERY]", "List all podcasts in the poddb database", "(If QUERY is supplied, will return matching podcasts)") do |query| @list_podcasts = true if query @query << query end end opts.on("-F", "--favorite-podcasts", "Show favorite podcasts") do if ! File.size?(FAVORITE_PODCASTS_FILE) puts "No podcasts found in #{FAVORITE_PODCASTS_FILE}" exit end @list_favorite_podcasts = true end opts.on("-o", "--order ORDER", "Sort results by ORDER", "The only option right now is 'popular'. Default order is pubdate.") do |order| @params << "o=#{order}" end opts.on("-d", "--days DAYS", "Limit results to items published since DAYS days ago") do |days| @params << "d=#{days}" end opts.on("-t", "--type MEDIA_TYPE", "Return items of MEDIA_TYPE only (audio,video)") do |media_type| @params << "t=#{media_type}" end opts.on("--download-and-play ITEM_ID", "Download item and play with PODDB_MEDIA_PLAYER") do |item_id| puts "Download and play #{item_id}" end opts.on("--readme", "Show README") do readme_file = File.("../../README.markdown", __FILE__) system("less #{readme_file}") exit end opts.on_tail("-h", "--help", "Show this message") do puts opts puts puts "For more detailed help, type `poddb --readme` or visit http://danielchoi.com/software/poddb_client.html" puts "Copyright 2011 Daniel Choi <[email protected]> MIT License" exit end opts.on_tail("-v", "--version", "Show version number") do puts "poddb #{VERSION}" exit end end.parse!(@args) @query = @query.concat @args q = CGI::escape(@query.join(' ').strip) if q != '' @params << "q=#{q}" end @params = @params.empty? ? '' : @params.join('&') end |
#run ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/poddb_client.rb', line 105 def run cleanup if @add_podcast add_podcast elsif @list_podcasts || @list_favorite_podcasts list_podcasts interactive elsif @items_from_favorites items_from_favorites interactive else search interactive end end |
#search ⇒ Object
176 177 178 179 |
# File 'lib/poddb_client.rb', line 176 def search @output = curl "/search?#@params" mark_already_downloaded end |