class DManga::SiteParserBase

Constants

USER_AGENT

Attributes

chapters[RW]
manga_name[RW]
manga_url[RW]
verbose[RW]

Public Class Methods

new(argv) click to toggle source
# File lib/dmanga/site_parser_base.rb, line 14
def initialize(argv)
  @options = Options.new(argv)
  @manga_url = nil
  # manga_name is also used as manga directory name
  @manga_name = nil
  @chapters = nil
end

Public Instance Methods

create_dir(relative_path) click to toggle source

check if the directory exists and create a directory relative to downlaod directory

# File lib/dmanga/site_parser_base.rb, line 172
def create_dir(relative_path)
  absolute_path = [@options.download_dir, relative_path].join(File::SEPARATOR)
  DManga::display_feedback "\nCriando diretorio '#{relative_path}' em '#{@options.download_dir}'" if @options.verbose
  unless Dir.exist? absolute_path
    Dir.mkdir(absolute_path)
    puts if @options.verbose ## just a blank line for prettier output
  else
    DManga::display_feedback "'#{relative_path}' directorio ja existe" if @options.verbose
  end
end
download_dir() click to toggle source

Returns the download destination directory

# File lib/dmanga/site_parser_base.rb, line 188
def download_dir
  @options.download_dir
end
get_progressbar() click to toggle source

return a progressbar suitable to the user operating system

# File lib/dmanga/site_parser_base.rb, line 127
def get_progressbar
  if DManga::OS.windows?
    return  ProgressBar.create(:title => 'Baixando',
                               :starting_at => 20,
                               :length => 70,
                               :total => nil)
  else
    return  ProgressBar.create(:title => 'Baixando',
                               :starting_at => 20,
                               :total => nil)
  end
end
imgs_download(chp_path, imgs_urls) click to toggle source

download images to path relative to Downloads directory

# File lib/dmanga/site_parser_base.rb, line 141
def imgs_download(chp_path, imgs_urls)
  imgs_urls.each do |url|
    original_filename =  url.slice(/(?u)(\w|[_-])+\.(png|jpg)/i)

    img_path = [@options.download_dir,
                chp_path,
                original_filename].join(File::SEPARATOR)
    unless File.exist? img_path
      encoded_url = Addressable::URI.encode(url)
      DManga::display_feedback "\n#{encoded_url}"
      pbar = get_progressbar
      URI.open(
        encoded_url, "User-Agent" => USER_AGENT,
        :progress_proc => lambda {|s| pbar.increment }
      ) do |response|
        if response.status[1] == "OK"
          DManga::display_feedback "Salvando imagem em:'#{img_path}'" if @options.verbose
          File.open(img_path, "wb") do |img|
            img.puts response.read
          end
        else
          puts "Error #{reponse.status}"
        end
      end
      puts
    end
  end
end
parse(url, regex) { |result, page| ... } click to toggle source

Parse the html and extract links that match the pattern. Can receive a block.

# File lib/dmanga/site_parser_base.rb, line 24
def parse(url, regex)
  DManga::display_feedback "\nfetching #{url}" if @options.verbose
  result = []
  URI.open(url, "User-Agent" => USER_AGENT) do |response|
    if response.status[1] == "OK"
      DManga::display_feedback "parsing #{url}" if @options.verbose
      page = response.read
      page.scan(regex) do |r| # => try first regex
        if r.length < 2
          result << r[0]
        else
          result << r
        end
      end
      result = yield(result, page) if block_given?
    else
      DManga::display_error("ERRO: Servidor respondeu: #{response.status.inpect}")
    end
  end
  result
end
remove_invalid_simbols(name) click to toggle source
# File lib/dmanga/site_parser_base.rb, line 192
def remove_invalid_simbols(name)
  # windows OS dont accept these simbols in folder name
  name.gsub!(%r{[/\\:*?"<>|]|(\.+$)|(^\.+)}, '_')
end
select_chapters() click to toggle source
# File lib/dmanga/site_parser_base.rb, line 74
    def select_chapters
      DManga::display_feedback "\nCapítulos:"
      @chapters.reverse_each.with_index do |chapter, index|
        DManga::display_feedback "(#{index + 1})\t#{chapter[0]}"
      end
      answer = nil
      DManga::display_feedback "\n#{@chapters.length} capitulos encontrados\n"
      loop do
        DManga::display_prompt("Quais capitulos você quer baixar? ")
        answer = $stdin.gets.chomp
        if answer == "o" || answer.empty?
          DManga::display_feedback(
            <<-EOS
  o                         - exibe opções.
    c                         - cancelar.
    todos                     - baixar todos os capítulos.
    inicio-fim                - baixar intervalo selecionado.
                                    Ex: 0-10 - baixa do 0 ao 10.
    capNum,capNum,capNum      - baixar capitulos selecionados.
                                    Ex: 29,499,1 - baixa capitulos 29, 499 e 1.
            EOS
          )
        elsif answer == "c"
          DManga::display_feedback("Saindo")
          exit true
        elsif answer == "todos"
          DManga::display_feedback "Baixando todos os capítulos" if @options.verbose
          break
        elsif answer =~ /(\d+)-(\d+)/
          b = Integer($2) <= @chapters.length ? Integer($2) * -1 : @chapters.length * -1
          e = Integer($1) * -1
          @chapters = @chapters[b..e]
          DManga::display_feedback "Baixando capítulos do #{$1} ao #{$2}" if @options.verbose
          break
        elsif answer =~ /^(\d+,?)+$/
          answer = answer.split(',')
          aux = []
          # downloads are processed in reverse order (to make
          # it in crescent order)so the answer is reversed too
          answer.reverse_each do |c|
            chp = @chapters[Integer(c) * -1]
            aux << chp unless chp.nil?
          end
          @chapters = aux
          DManga::display_feedback "Baixando capítulos #{answer.to_s}" if @options.verbose
          break
        else
          DManga::display_warn("\tOpção invalida")
        end
      end
    end
select_manga(mangas) click to toggle source
# File lib/dmanga/site_parser_base.rb, line 52
def select_manga(mangas)
  puts # just a new line for better output
  unless mangas.empty?
    mangas.each do |manga|
      DManga::display_prompt("Você quer baixar #{manga[1]} [s/n]? ")
      res = $stdin.gets.chomp
      if res == 's'
        @manga_url = manga[0]
        @manga_name = manga[1]
        break
      elsif res != 'n'
        DManga::display_warn("\tOpção invalida")
        DManga::display_feedback("\tSaindo")
        exit(true)
      end
    end
  else
    raise MangaNotFoundError, "manga não encontrado"
  end
  raise MangaNotFoundError, "manga não encontrado" if @manga_url.nil?
end