lib/webradio.rb in rget-2.1.1 vs lib/webradio.rb in rget-2.2.0
- old
+ new
@@ -33,47 +33,102 @@
def initialize(url, options)
raise 'do not instanciate directly, use WebRadio method.' if self.class == WebRadio
@url = url
@options = options
+ if @options.path =~ %r|^dropbox://|
+ require 'dropbox'
+ @dropbox = DropboxAuth.client
+ end
end
def download(name)
raise 'not implemented.'
end
private
def mp3ize(src, dst, delete_src = true)
+ if @options.mp3
+ if exist?(dst)
+ puts "#{dst} is existent, skipped."
+ return
+ end
+ else
+ if exist?(src)
+ puts "#{src} is existent, skipped."
+ return
+ end
+ end
+
+
# download src file
- if !File.exist?(src) && !File.exist?(dst)
+ unless File.exist?(src)
print "getting #{src}..."
begin
yield
puts "done."
rescue DownloadError => e
- puts "failed."
File.delete(src) if File.exist?(src)
+ puts "failed."
$stderr.puts e.message
return
end
end
+ if !@options.mp3 || src == dst
+ move(src) if @options.path
+ return
+ end
# convert to mp3
- return self unless @options.mp3
-
print "converting to mp3..."
- if File.exist? dst
- puts "skipped."
+ result = Open3.capture3("ffmpeg -i #{src} -ab 64k #{dst}")
+ if result[2].to_i == 0
+ File.delete(src) if delete_src
+ puts "done."
else
- result = Open3.capture3("ffmpeg -i #{src} -ab 64k #{dst}")
- if result[2].to_i == 0
- File.delete(src) if delete_src
+ File.delete(dst) if File.exist?(dst)
+ puts "failed."
+ $stderr.puts result[1]
+ return
+ end
+ move(dst) if @options.path
+ end
+
+ def exist?(dst)
+ if @dropbox
+ begin
+ !@dropbox.ls(dropbox_file(dst))[0]['is_deleted']
+ rescue Dropbox::API::Error::NotFound, NoMethodError
+ false
+ end
+ elsif @options.path
+ File.exist?(File.join(@options.path, dst))
+ else
+ File.exist?(dst)
+ end
+ end
+
+ def move(dst)
+ if @options.path
+ print "move to #{@options.path}..."
+ begin
+ if @dropbox
+ @dropbox.chunked_upload(dropbox_file(dst), open(dst))
+ File.delete(dst)
+ elsif @options.path
+ FileUtils.mv(dst, @options.path)
+ end
puts "done."
- else
+ rescue => e
puts "failed."
- $stderr.puts MediaConvertError.new(result[1])
+ $stderr.puts e.message
end
end
+ end
+
+ def dropbox_file(file)
+ path = @options.path.sub(%r|^dropbox://|, '')
+ File.join(path, file)
end
end
def WebRadio(url, options)
radio = WebRadio.instance(url, options)