Module: Xunlei::Helper::CLIHelper

Includes:
Configurator
Defined in:
lib/xunlei/helpers/cli_helper.rb

Instance Method Summary collapse

Methods included from Configurator

#chromedriver_zip_name, #xunlei_cookies_path, #xunlei_credential_file_path, #xunlei_downloaded_tasks_path, #xunlei_folder_name, #xunlei_folder_path, #xunlei_tasks_path

Instance Method Details

#all_downloaded_tasksObject



61
62
63
64
65
# File 'lib/xunlei/helpers/cli_helper.rb', line 61

def all_downloaded_tasks
  system("touch #{xunlei_downloaded_tasks_path}") # touch it in case it's non-existent

  YAML.load_file(xunlei_downloaded_tasks_path) || []
end

#ask_for_credentialsObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/xunlei/helpers/cli_helper.rb', line 67

def ask_for_credentials
  puts "#{xunlei_credential_file_path} not exists. Now creating one."
  puts "*** WARNING: your USERNAME and PASSWORD will be stored as PLAINTEXT at #{xunlei_credential_file_path} ***"

  username = ask("Username: ")
  password = ask("Password: ") { |q| q.echo = "*" }
  File.open(xunlei_credential_file_path, "w") do |file|
    file.write({ :username => username, :password => password }.to_yaml)
  end

  puts "#{xunlei_credential_file_path} successfully created."
end

#check_for_chromedriverObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/xunlei/helpers/cli_helper.rb', line 96

def check_for_chromedriver
  unless system("which chromedriver > /dev/null 2>&1")
    puts "chromedriver not found in your PATH"
    if agree("Would you like me to try download it for you? (yes or no)")
      if system("wget 'http://chromium.googlecode.com/files/chromedriver_mac_18.0.1022.0.zip' -O #{chromedriver_zip_name}")
        if system("unzip #{chromedriver_zip_name}")
          puts "moving chromedriver to /usr/local/bin ..."
          system("mv -v chromedriver /usr/local/bin")

          puts "deleting temporary files..."
          system("rm -v #{chromedriver_zip_name}")
        else
          puts "`unzip` not found in your PATH. Try manually unzip #{chromedriver_zip_name} and move it to /usr/local/bin"
          exit
        end
      end
    else
      puts "OK. You can download it manually here: http://code.google.com/p/chromium/downloads"
      exit
    end
  end
end

#check_for_config_filesObject



119
120
121
122
# File 'lib/xunlei/helpers/cli_helper.rb', line 119

def check_for_config_files
  create_xunlei_folder unless xunlei_folder_exists?
  check_for_credentials
end

#check_for_credentialsObject



80
81
82
# File 'lib/xunlei/helpers/cli_helper.rb', line 80

def check_for_credentials
  ask_for_credentials unless credential_file_exists?
end

#create_xunlei_folderObject



84
85
86
# File 'lib/xunlei/helpers/cli_helper.rb', line 84

def create_xunlei_folder
  Dir.mkdir(xunlei_folder_path)
end

#credential_file_exists?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/xunlei/helpers/cli_helper.rb', line 92

def credential_file_exists?
  File.exists?(xunlei_credential_file_path)
end

#filter_files(files, options = nil) ⇒ Object



16
17
18
# File 'lib/xunlei/helpers/cli_helper.rb', line 16

def filter_files(files, options=nil)
  files.select { |file| !filtered?(file[:name], options) }.inject([], :<<)
end

#filter_names(names, options = nil) ⇒ Object



20
21
22
# File 'lib/xunlei/helpers/cli_helper.rb', line 20

def filter_names(names, options=nil)
  names.select { |name| !filtered?(name, options) }.inject([], :<<)
end

#filtered?(name, options) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'lib/xunlei/helpers/cli_helper.rb', line 9

def filtered?(name, options)
  return false unless options
  return true if !options.only.nil? and !(name =~ /#{options.only}/i)
  return true if !options.except.nil? and name =~ /#{options.except}/i
  false
end

#mark_as_downloaded(task) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/xunlei/helpers/cli_helper.rb', line 45

def mark_as_downloaded(task)
  puts "#{task[:name]} successfully downloaded."

  downloaded = all_downloaded_tasks
  downloaded << task[:name]
  File.open(xunlei_downloaded_tasks_path, "w") do |file|
    file.write(downloaded.to_yaml)
  end

  all_tasks = YAML.load_file(xunlei_tasks_path)
  all_tasks.delete(task)
  File.open(xunlei_tasks_path, "w") do |file|
    file.write(all_tasks.to_yaml)
  end
end

#show_files(files) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/xunlei/helpers/cli_helper.rb', line 36

def show_files(files)
  files.each do |file|
    puts "#{file[:size]}\t#{file[:name]}"
  end

  puts
  puts "Total: #{files.size} files, #{total_size(files.map { |file| file[:size] })} MB (estimated)"
end

#total_size(file_sizes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xunlei/helpers/cli_helper.rb', line 24

def total_size(file_sizes)
  file_sizes.inject(0) do |total_megabytes, file_size|
    if file_size =~ /G/i
      total_megabytes + 1000 * file_size.to_f
    elsif file_size =~ /M/i
      total_megabytes + file_size.to_f
    else
      total_megabytes + 1
    end
  end
end

#xunlei_folder_exists?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/xunlei/helpers/cli_helper.rb', line 88

def xunlei_folder_exists?
  File.directory?(xunlei_folder_path)
end