class MyLocalPutio::Configuration

Attributes

debug[R]
delete_remote[R]
detailed_progress[R]
disk_threshold[R]
local_destination[R]
silent[R]
socks_host[R]
socks_port[R]
temp_destination[R]
token[R]
with_subtitles[R]

Public Class Methods

new() click to toggle source
# File lib/my-local-putio/configuration.rb, line 7
def initialize
  read_args_from_envs!
  parse_args!
  set_defaults!
  validate_args!
end

Public Instance Methods

disk_manager() click to toggle source
# File lib/my-local-putio/configuration.rb, line 14
def disk_manager
  @disk_manager ||= DiskManager.new(self)
end
logger() click to toggle source
# File lib/my-local-putio/configuration.rb, line 22
def logger
  @logger ||= Logger.new(self)
end
socks_enabled?() click to toggle source
# File lib/my-local-putio/configuration.rb, line 18
def socks_enabled?
  @socks_host || @socks_port
end

Private Instance Methods

folder_checks!(folder) click to toggle source
# File lib/my-local-putio/configuration.rb, line 80
def folder_checks!(folder)
  return if File.exists?(folder) && File.writable?(folder)
  FileUtils.mkdir_p(folder)
rescue Errno::EACCES
  puts "Cannot write on the local destination path '#{folder}'"
  exit
end
parse_args!() click to toggle source
# File lib/my-local-putio/configuration.rb, line 27
def parse_args!
  OptionParser.new do |opt|
    opt.on("-t", "--token TOKEN", "Put.io access token [REQUIRED]") { |v| @token = v }
    opt.on("-l", "--local-destination FULLPATH", "Local destination path [REQUIRED]") { |v| @local_destination = v }
    opt.on("-d", "--delete-remote", "Delete remote file/folder after the download") { |v| @delete_remote = true }
    opt.on("-s", "--with-subtitles", "Fetch subtitles from Put.io api") { |v| @with_subtitles = true }
    opt.on("-v", "--version", "Print my-local-putio version") do
      puts MyLocalPutio::VERSION
      exit
    end

    opt.on("--debug", "Debug mode [Developer mode]") { |v| @debug = true }

    opt.on("--disk-threshold size", "Stops the downloads when the disk space threshold is reached. (Size in MB, e.g: 2000 for 2GB)") do |v|
      @disk_threshold = v.to_i
    end

    opt.on("--detailed-progress", "Detailed download information instead a progress bar") { |v| @detailed_progress = true }

    opt.on("--temp-destination FULLPATH", "Temporary destination for the incomplete downloads (Default: 'local_destination'/incomplete_downloads)") { |v| @temp_destination = v }

    opt.on("--silent", "Hide all messages and progress bar") { |v| @silent = true }

    opt.on("--socks5-proxy hostname:port", "SOCKS5 hostname and port for proxy. Format: 127.0.0.1:1234") do |v|
      @socks_host, @socks_port = v.to_s.split(":")
    end
  end.parse!
end
port_is_open?(host, port) click to toggle source
# File lib/my-local-putio/configuration.rb, line 92
def port_is_open?(host, port)
  Socket.tcp(host, port, connect_timeout: 5) { true } rescue false
end
read_args_from_envs!() click to toggle source
# File lib/my-local-putio/configuration.rb, line 88
def read_args_from_envs!
  @token ||= ENV["PUTIO_TOKEN"]
end
set_defaults!() click to toggle source
# File lib/my-local-putio/configuration.rb, line 56
def set_defaults!
  @temp_destination ||= File.join(@local_destination, "incomplete_downloads")
end
validate_args!() click to toggle source
# File lib/my-local-putio/configuration.rb, line 60
def validate_args!
  unless @token
    puts "Missing token"
    exit
  end

  unless @local_destination
    puts "Missing local destination"
    exit
  end

  folder_checks!(@local_destination)
  folder_checks!(@temp_destination)

  if socks_enabled? && !port_is_open?(@socks_host, @socks_port)
    puts "Cannot connect to socks using '#{@socks_host}:#{@socks_port}'"
    exit
  end
end