class WebRadio

Public Class Methods

instance(params, options) click to toggle source
# File lib/webradio.rb, line 12
def self.instance(params, options)
        case params['url']
        when %r[^https?://hibiki-radio\.jp/]
                require 'hibiki'
                Hibiki.new(params, options)
        when %r[^https?://(www\.)?onsen\.ag/program/]
                require 'onsen'
                Onsen.new(params, options)
        when %r[nicovideo\.jp]
                require 'nicovideo'
                Nicovideo.new(params, options)
        when %r[^http://m\.himalaya\.fm/]
                require 'himalaya'
                Himalaya.new(params, options)
        when %r[^https://asobistore\.jp/]
                require 'asobistore'
                AsobiStore.new(params, options)
        when %r[^https://stand\.fm/channels/]
                require 'standfm'
                StandFm.new(params, options)
        when %r[^https://www\.youtube\.com/playlist]
                require 'youtube'
                YoutubePlaylist.new(params, options)
        when %r[^https://www\.youtube\.com/c/]
                require 'youtube'
                YoutubeChannel.new(params, options)
        else
                raise UnsupportError, "unsupported url: #{params['url']}"
        end
end
new(params, options) click to toggle source
# File lib/webradio.rb, line 43
def initialize(params, options)
        raise 'do not instanciate directly, use WebRadio method.' if self.class == WebRadio
        @url = params['url']
        @label = params['label']
        @cover = params['cover']
        @options = options
        if !@options.dump && @options.path =~ %r|^dropbox://|
                require 'dropbox'
                @dropbox = RGet::Dropbox.client
        end
end

Public Instance Methods

download() click to toggle source
# File lib/webradio.rb, line 55
def download
        raise 'not implemented.'
end
dump() click to toggle source
# File lib/webradio.rb, line 59
def dump
        raise 'not implemented.'
end

Private Instance Methods

add_cover(dst) click to toggle source
# File lib/webradio.rb, line 121
def add_cover(dst)
        begin
                mp3 = Mp3Info.new(dst)
                mp3.tag.title = File.basename(dst, '.mp3')
                mp3.tag2.add_picture(cover_image) if @cover
                mp3.close
        rescue
                $stderr.puts "add mp3 info failed (#$!)"
        end
end
cover_image() click to toggle source
# File lib/webradio.rb, line 132
def cover_image
        if @cover =~ /^https?:/
                cover_image_as_url()
        else # XPath
                cover_image_as_xpath()
        end
end
cover_image_as_url() click to toggle source
# File lib/webradio.rb, line 140
def cover_image_as_url
        URI.open(@cover, 'rb', &:read)
end
cover_image_as_xpath() click to toggle source
# File lib/webradio.rb, line 144
def cover_image_as_xpath
        html = Nokogiri(URI.open(@url, &:read))
        image_url = (URI(@url) + (html.xpath(@cover)[0].text)).to_s
        URI.open(image_url, 'r:ASCII-8BIT', &:read)
end
dropbox_file(file) click to toggle source
# File lib/webradio.rb, line 192
def dropbox_file(file)
        path = @options.path.sub(%r|^dropbox://|, '/')
        File.join(path, file)
end
dropbox_path() click to toggle source
# File lib/webradio.rb, line 188
def dropbox_path
        @options.path.sub(%r|^dropbox://|, '/')
end
exist?(dst) click to toggle source
# File lib/webradio.rb, line 150
def exist?(dst)
        if @dropbox
                @dropbox.exist?(dst, dropbox_path)
        elsif @options.path
                File.exist?(File.join(@options.path, dst))
        else
                File.exist?(dst)
        end
end
move(dst) click to toggle source
# File lib/webradio.rb, line 160
def move(dst)
        if @options.path
                begin
                        print "move to #{@options.path}..."
                        if @dropbox
                                open(dst) do |r|
                                        @dropbox.upload(dropbox_file(dst)) do
                                                print '.'
                                                r.read(10_000_000)
                                        end
                                end
                                File.delete(dst)
                        elsif !(Pathname(@options.path).expand_path == Pathname(dst).expand_path.dirname)
                                FileUtils.mv(dst, @options.path)
                        else
                                puts "skip."
                                return
                        end
                        puts "done."
                rescue ::DropboxApi::Errors::FileConflictError
                        puts "existent, skip."
                rescue => e
                        puts "failed."
                        $stderr.puts e.message
                end
        end
end
mp3nize(src, dst, delete_src = true) { || ... } click to toggle source
# File lib/webradio.rb, line 64
def mp3nize(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
        unless File.exist?(src)
                print "getting #{src}..."
                begin
                        yield
                        puts "done."
                rescue DownloadError => e
                        File.delete(src) if File.exist?(src)
                        puts "failed."
                        $stderr.puts e.message
                        return
                end
        end
        if !@options.mp3 || src == dst
                add_cover(src) if src =~ /\.mp3$/
                move(src) if @options.path
                return
        end

        # convert to mp3
        print "converting to mp3..."
        ffmpeg = (@options['mp3nize'] || "ffmpeg -i '$1' -ab 64k '$2'").gsub(/\$(.)/){|s|
                case $1
                        when '1'; src
                        when '2'; dst
                        when '$'; '$'
                        else; s
                end
        }
        result = Open3.capture3(ffmpeg)
        if result[2].to_i == 0
                File.delete(src) if delete_src
                add_cover(dst)
                puts "done."
        else
                File.delete(dst) if File.exist?(dst)
                puts "failed."
                $stderr.puts result[1]
                return
        end
        move(dst) if @options.path
end