Sha256: d65b36912e902d31863e109d891737e429984fe882a56acc54694f1a390864bc

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

#!/usr/bin/env ruby

require 'rmega'
require 'rmega/cli'

include Rmega::CLI::Helpers

if ARGV.empty?
  ARGV << '--help'
else
  cli_options[:url] = ARGV[0]
end

OptionParser.new do |opts|
  opts.banner = "Usage:\n"
  opts.banner << "\t#{File.basename(__FILE__)} url [options]\n"
  opts.banner << "\t#{File.basename(__FILE__)} path [options]\n"
  opts.banner << "Examples:\n"
  opts.banner << "\t#{File.basename(__FILE__)} 'https://mega.nz/#!aBkHBKLX!n4kqzbJooqc3o_s96PZjN1tEJzQ4QQwskHf7YqKa'\n"
  opts.banner << "\t#{File.basename(__FILE__)} https://www.reddit.com/r/megalinks\n"
  opts.banner << "\t#{File.basename(__FILE__)} /remote/docs/myfile.txt -u email@localhost\n"
  opts.banner << "Options:"

  opts.on("-o PATH", "--output", "Local destination path") { |path|
    cli_options[:output] = path
  }

  apply_opt_parser_options(opts)
end.parse!

cli_rescue do
  if cli_options[:user]
    session = Rmega::Session.new.login(cli_options[:user], cli_options[:pass] || cli_prompt_password)
    root = session.storage.root
    node = traverse_storage(root, cli_options[:url].dup)
    raise("Node not found - #{cli_options[:url]}") unless node
    node.download(cli_options[:output] || Dir.pwd)
  else
    urls = [cli_options[:url]]

    unless mega_url?(cli_options[:url])
      html = Rmega::Session.new.http_get_content(cli_options[:url])
      urls = html.scan(Rmega::Nodes::Factory::URL_REGEXP).flatten.uniq
      raise("Nothing to download") if urls.empty?
    end

    urls.each_with_index do |url, index|
      node = Rmega::Nodes::Factory.build_from_url(url)
      print "[#{index+1}/#{urls.size}] " if urls.size > 1
      node.download(cli_options[:output] || Dir.pwd)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rmega-0.2.2 bin/rmega-dl
rmega-0.2.1 bin/rmega-dl